8000 generics inferred differently when assigning to variable even though the variable's type is the same · Issue #11455 · python/mypy · GitHub
[go: up one dir, main page]

Skip to content

generics inferred differently when assigning to variable even though the variable's type is the same #11455

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.

8000

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
DetachHead opened this issue Nov 4, 2021 · 1 comment
Labels
bug mypy got something wrong

Comments

@DetachHead
Copy link
Contributor
DetachHead commented Nov 4, 2021
from typing import Callable, TypeVar

A = TypeVar("A")
B = TypeVar("B")


def func(a: A, b: B, fn: Callable[[A], A]) -> A | B:
    ...


inferred = func(
    1,
    None,
    lambda value: reveal_type(value),  # Revealed type is "builtins.int"
)
reveal_type(inferred)  # "Union[builtins.int, None]"

explicit: int | None
explicit = func(
    1,
    None,
    lambda value: reveal_type(value), # Revealed type is "Union[builtins.int, None]"
)
reveal_type(explicit)  # "Union[builtins.int, None]"

https://mypy-play.net/?mypy=latest&python=3.10&gist=7c2af9e77a6246ee6fb3e5052722eec1

hauntsaninja pushed a commit that referenced this issue May 15, 2025
…ions in return position (#18976)

* Fixes #17221. 
* Fixes #17654. 
* Fixes #17553. 
* Fixes #17536. 
* Fixes #16659. 
* Fixes #16267. 
* Fixes #15755. 
* Fixes #15150. 
* Fixes #14664. 
* Incidentally improves error message in #12156. 
* Fixes #12092. 
* Fixes #11985. 
* Improves #11455 (but the problem with union `TypeVar | SomeFixedType`
reported in comments there remains).
* Fixes #10426.

When using context, we can perform some overly optimistic inference when
return type is `T1 | T2`. This breaks in important case of
`builtins.min` when `default` and `key` are passed, essentially making
them always incompatible. This is not the most principled approach, but
let's see the primer results.

This resolves quite a few issues (some of them duplicates, but some -
substantially different), `min` problem was a very popular one... Diff
run: sterliakov/mypy-issues#30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong
Projects
None yet
Development

No branches or pull requests

1 participant
0