You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
A ternary expression of context managers is inferred as object, leading to an erroneous attr-defined errors about object not defining __enter__ and __exit__.
To Reproduce
classA:
def__enter__(self) ->'A':
returnselfdef__exit__(self, *args) ->None:
passclassB:
def__enter__(self) ->'B':
returnselfdef__exit__(self, *args) ->None:
passdeffoo() ->bool:
returnTruea_or_b=A() iffoo() elseB()
reveal_type(a_or_b) # Revealed type is "builtins.object"witha_or_b: # "object" has no attribute "__enter__" & "object" has no attribute "__exit__"
...
This is because mypy uses a join operation rather than a union when unifying types. Here is the master bug for this issue, and you can search for all related issues using the topic-join-v-union tag.
Related: #7738
A ternary expression of context managers is inferred as
object
, leading to an erroneousattr-defined
errors aboutobject
not defining__enter__
and__exit__
.To Reproduce
Gist URL: https://gist.github.com/mypy-play/8e363dcee24e9fab1c3abd89d1539635
Playground URL: https://mypy-play.net/?mypy=master&python=3.11&gist=8e363dcee24e9fab1c3abd89d1539635
Expected Behavior
I would expect that the type of "a_or_b" to be "A | B" and not raise an
attr-defined
error.The text was updated successfully, but these errors were encountered: