-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Attribute of type A | B | None
becomes object
after narrowing checks
#12009
Comments
This is yet another case where mypy's use of join rather than union produces undesirable results. Here are some of the other bugs that would be fixed if this change were made: #11934 I'm sorry to sound like a broken record, but I really think there should be a serious discussion about moving from join to union. Pyright uses only union, never join, and it avoids all of these issues. |
I agree with Eric that we should strongly consider moving away from join. I am going to open a new issue to propose that. |
Fixed by #18138, which should be released with mypy v1.14.0. The revealed type is now |
Bug Report
When doing narrowing of a class attribute with a particular type shape
A | B | None
, the type of the attribute becomesobject
after the narrowing condition chain.To Reproduce
The following is the most minimal reproduction I was able to create.
Expected Behavior
Revealed type is
A | B | None
.Actual Behavior
Revealed type is
object
.I made some additional tests:
pyright
givesA | B | None
(good).x = c.x
and then doing the narrowing andreveal_type
onattr
givesA | B | None
(good).None
withA | B | D
givesA | B | D
(good). Also if I replaceif isinstance(c.attr, D)
withif type(c.attr) is D
(good). So theNone
seems essential.attr: A | None
, givesA | None
(good). So more than one type besides theNone
seems essential.elif
->if
givesA | B | None
(good). So theelif
seems essential.Your Environment
--strict
.mypy.ini
(and other config files):The text was updated successfully, but these errors were encountered: