Consider this example:
fromtypingimportGeneric, TypeVarT=TypeVar('T')
G=TypeVar('G', bound='Y')
classX(Generic[T]): passclassY: pass# Consider this function:deff(a: X[T] |G) ->X[T] |G:
ifisinstance(a, X):
reveal_type(a) # X[T`-1]returnaifisinstance(a, Y):
reveal_type(a) # G`-2returnaprint() # error: unreachable. It is ok, because all possibilities checked.# The same function, but if's swappeddefg(a: X[T] |G) ->X[T] |G: # error: Missing return statementifisinstance(a, Y):
reveal_type(a) # Yreturna# error: Incompatible return value type (got "Y", expected "Union[X[T], G]")ifisinstance(a, X):
reveal_type(a) # X[T`-1]returnaprint() # no "error: unreachable", because it is reachable and mypy complains about "missing return"# I expect to get no errors here, because it isnt reachableExpected Behavior
First function is fine. I expect to get no errors in second function. Also first reveal_type in second function is IMO invalid, it should be G.
Your Environment
- Mypy version used:
mypy 0.971 (compiled: yes) - Mypy command-line flags: none
- Mypy configuration options from
mypy.ini (and other config files):
disallow_any_unimported = True
disallow_any_generics = True
disallow_subclassing_any = True
disallow_untyped_defs = True
disallow_incomplete_defs = True
disallow_untyped_decorators = True
warn_unreachable = True
warn_redundant_casts = True
warn_unused_ignores = True
# warn_return_any = True
strict_equality = True
show_column_numbers = True
- Python version used: 3.10.6
- Operating system and version: Win10
PS: pls give better title to this issue, idk how to properly describe this issue.
Consider this example:
Expected Behavior
First function is fine. I expect to get no errors in second function. Also first
reveal_typein second function is IMO invalid, it should beG.Your Environment
mypy 0.971 (compiled: yes)mypy.ini(and other config files):PS: pls give better title to this issue, idk how to properly describe this issue.