-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Open
Labels
Description
Feature
Unreachability should be checked for constrained type vars.
Pitch
It would be nice if this raised an error when run with --warn-unreachable
:
from typing import TypeVar
U = TypeVar('U', int, str)
def f(u: U) -> U:
if u is None:
print("whoa!!")
return u
To implement this feature, I think we need to store the spans of unreachability for each run, then find the intersection. I thought for a while that we could just treat constrained type variables as if they were bound to the union of constraints, but things like #9424 will happen for things that aren't final (unlike None
).
Metadata
Metadata
Assignees
Labels
Projects
Milestone
Relationships
Development
Select code repository
Activity
tyralla commentedon Jun 12, 2025
I investigated a little, and this limitation seems to have been known for a while:
mypy/test-data/unit/check-unreachable-code.test
Lines 1036 to 1049 in 8241059
mypy/mypy/checker.py
Lines 1467 to 1475 in 8241059
I experimented with the
IterationErrorWatcher
proposed in #19270 and was successful in the first simple attempt. However, as many existing tests would require adjustment and some new ones should be introduced (which might reveal the need to adjust the method eventually), I will wait to work on it until #19270 is accepted.By the way, aren't classes with type variables with value constraints a problem, too?