Skip to content

Bounded generic type parameter not narrowed past its upper bound, regardless of argument value #19285

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
alythobani opened this issue Jun 12, 2025 · 4 comments
Labels
bug mypy got something wrong topic-join-v-union Using join vs. using unions

Comments

@alythobani
Copy link

Bug Report

If a generic type variable _E has an upper bound B, the upper bound B seems to be used as the inferred type of _E when inferring other type variables that depend on _E, even if there is a more specific type that _E could be narrowed to.

This becomes an issue when trying to infer error types returned by poltergeist (a simple library that provides a generic Result = Ok[_T] | Err[_E] utility type).

To Reproduce

from poltergeist import catch

@catch(TypeError, ValueError)
def validate_positive_number(int_or_str: int | str) -> int:
    if isinstance(int_or_str, str):
        raise TypeError("Input must be an integer")
    if int_or_str <= 0:
        raise ValueError("Input must be positive")
    return int_or_str


def do_something_with_positive_number(int_or_str: int | str) -> None:
    validated_number_result = validate_positive_number(int_or_str)
    reveal_type(validated_number_result)  # Revealed type is "Union[poltergeist.result.Ok[builtins.int], poltergeist.result.Err[builtins.Exception]]" Mypy

The implementation of catch can be found here:

Source code for the `catch` decorator
import functools
from collections.abc import Awaitable
from typing import Callable, ParamSpec, TypeVar, reveal_type

from poltergeist.result import Err, Ok, Result

_T = TypeVar("_T")
_E = TypeVar("_E", bound=BaseException)
_P = ParamSpec("_P")


def catch(
    *errors: type[_E],
) -> Callable[[Callable[_P, _T]], Callable[_P, Result[_T, _E]]]:
    def decorator(func: Callable[_P, _T]) -> Callable[_P, Result[_T, _E]]:
        @functools.wraps(func)
        def wrapper(*args: _P.args, **kwargs: _P.kwargs) -> Result[_T, _E]:
            try:
                result = func(*args, **kwargs)
            except errors as e:
                return Err(e)
            return Ok(result)

        return wrapper

    return decorator


def catch_async(
    *errors: type[_E],
) -> Callable[[Callable[_P, Awaitable[_T]]], Callable[_P, Awaitable[Result[_T, _E]]]]:
    def decorator(
        func: Callable[_P, Awaitable[_T]]
    ) -> Callable[_P, Awaitable[Result[_T, _E]]]:
        @functools.wraps(func)
        async def wrapper(*args: _P.args, **kwargs: _P.kwargs) -> Result[_T, _E]:
            try:
                result = await func(*args, **kwargs)
            except errors as e:
                return Err(e)
            return Ok(result)

        return wrapper

    return decorator

Expected Behavior

mypy should be able to accurately use the value of errors to narrow the inferred type of _E within catch, and thus infer that validated_number_result is of type Result[int, TypeError | ValueError].

Actual Behavior

Mypy keeps _E inferred to its upper bound BaseException, thus inferring validated_number_result as Result[int, BaseException].

By contrast, pylance's type checker is able to narrow validated_number_result as expected:

Image

Your Environment

  • Mypy version used: 1.15.0
  • Mypy configuration options from mypy.ini (and other config files):
mypy.ini
[mypy]
python_version = 3.13
mypy_path = typings
ignore_missing_imports = True
check_untyped_defs = True
disallow_untyped_defs = True
disallow_untyped_calls = True
strict_equality = True
disallow_any_unimported = True
warn_return_any = True
no_implicit_optional = True
pretty = True
show_error_context = True
show_error_codes = True
show_error_code_links = True
no_namespace_packages = True
  • Python version used: 3.13.0

Unsure if related to #19081 , but it's possible, given that both these issues are related to retaining information via generic type parameters.

@alythobani alythobani added the bug mypy got something wrong label Jun 12, 2025
@alythobani alythobani changed the title Generic type value not narrowed past its upper bound, based on passed-in arguments Bounded generic type parameter not narrowed past its upper bound, regardless of argument value Jun 12, 2025
@brianschubert brianschubert added the topic-join-v-union Using join vs. using unions label Jun 12, 2025
@brianschubert
Copy link
Collaborator

This boils down to

def foo[T](*x: T) -> T: ...

reveal_type(foo(1, "a"))  # N: Revealed type is "builtins.object"

@alythobani
Copy link
Author

@brianschubert Hmm but interestingly if I add an upper bound there, mypy is suddenly able to narrow:

def foo[T: int | str | bool](*x: T) -> T: ...


reveal_type(foo(1, "a"))  # Revealed type is "Union[builtins.int, builtins.str]"Mypy

@brianschubert
Copy link
Collaborator

brianschubert commented Jun 12, 2025

Yeah, specifics matter for the inference rules. Closer to this case would be

def foo[T: BaseException](*x: T) -> T: ...

reveal_type(foo(ValueError(), TypeError()))  # N: Revealed type is "builtins.Exception"

In your case the joined type (object) isn't a subtype of the upper bound. That causes mypy to fall back to the upper bound.

Note by the way that there isn't actually any narrowing happening in your case. bool is a subtype of int, so int | str | bool is equivalent to int | str. The inferred type is just the upper bound verbatim:

def foo[T: int | str | bytes](*x: T) -> T: ...

reveal_type(foo(1, "a"))  # N: Revealed type is "builtins.int | builtins.str | builtins.bytes"

@alythobani
Copy link
Author

alythobani commented Jun 12, 2025

Ah right yea. Equivalently this

def foo[B: BaseException](*x: type[B]) -> B:
    return x[0]()


reveal_type(foo(ValueError, TypeError))  # N: Revealed type is "builtins.Exception"

and confirming that things are narrowed correctly if there's just a single parameter, although mypy has a different weird issue here with the implementation if the upper bound is a union:

def bar[B: ValueError | TypeError](x: type[B]) -> B:
    return x()  # Incompatible return value type (got "ValueError | TypeError", expected "B")Mypyreturn-value


reveal_type(bar(ValueError))  # Revealed type is "builtins.ValueError"Mypy
reveal_type(bar(TypeError))  # Revealed type is "builtins.TypeError"Mypy

And oh right lol good point on bool being a subtype of int.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong topic-join-v-union Using join vs. using unions
Projects
None yet
Development

No branches or pull requests

2 participants