-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
structurally equivalent namedtuples can confuse mypy in unions in some cases #18520
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
Comments
breaking the structural equivalence "fixes" the type errors -- but can break code class B(NamedTuple):
key: BKey
s: str
+ b_hax: bool = True |
#15600 seems related -- but I don't understand type algebra enough to know if this is the same as what's explained here: #15600 (comment) |
a more clever workaround which preserves the runtime behaviour is to duplicate the definition in/out of a if TYPE_CHECKING:
class B(NamedTuple):
key: BKey
s: str
b_hax: bool = True
else: # real implementation here
class B(NamedTuple):
key: BKey
s: str |
We create an "impossible" meet of tuples with different fallbacks here (TODO is apparently referring to this issue): Lines 987 to 992 in 4a76a1a
|
The monster produced from meeting A and B looks like
which looks suspicious: the cross-bred types cannot exist. |
…ython#18564) Fixes python#18562. Fixes python#6623. Fixes python#18520. (Only incidentally and I'm not exactly sure why.) I was investigating what input mypy thought satisfied both overloads and I found that mypy is missing a check on the fallback type.
Bug Report
some narrowing goes wrong and mypy ends up getting confused with a bunch of self-union types (
A | A
) and spurious errorsI'm not entirely sure the cause however
To Reproduce
Expected Behavior
no errors from the given snippet
Actual Behavior
Your Environment
mypy.ini
(and other config files): n/aThe text was updated successfully, but these errors were encountered: