python / mypy

Optional static typing for Python
https://www.mypy-lang.org/
Other
18.5k stars 2.83k forks source link

generic with union value inferred as common base #12327

Open DetachHead opened 2 years ago

DetachHead commented 2 years ago
from typing import _T

class A:...
class B(A):
    a: int
class C(A):
    a: int

def safe(it: _T | None) -> _T:...

b_or_c: B | C

value = safe(b_or_c)

reveal_type(value) #A, should be B | C
value.a # error

https://mypy-play.net/?mypy=master&python=3.10&flags=show-error-codes%2Callow-redefinition%2Cstrict%2Ccheck-untyped-defs%2Cdisallow-any-decorated%2Cdisallow-any-expr%2Cdisallow-any-explicit%2Cdisallow-any-generics%2Cdisallow-any-unimported%2Cdisallow-incomplete-defs%2Cdisallow-subclassing-any%2Cdisallow-untyped-calls%2Cdisallow-untyped-decorators%2Cdisallow-untyped-defs%2Cno-implicit-optional%2Cno-implicit-reexport%2Cstrict-equality%2Cwarn-incomplete-stub%2Cwarn-redundant-casts%2Cwarn-return-any%2Cwarn-unreachable%2Cwarn-unused-configs%2Cwarn-unused-ignores&gist=c0de78dd9b545fc94ea9469f55af933a

erictraut commented 2 years ago

This is because mypy uses join instead of union. See #12056.