-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Closed
Labels
bugmypy got something wrongmypy got something wrongtopic-join-v-unionUsing join vs. using unionsUsing join vs. using unions
Description
Related: #7738
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
class A:
def __enter__(self) -> 'A':
return self
def __exit__(self, *args) -> None:
pass
class B:
def __enter__(self) -> 'B':
return self
def __exit__(self, *args) -> None:
pass
def foo() -> bool:
return True
a_or_b = A() if foo() else B()
reveal_type(a_or_b) # Revealed type is "builtins.object"
with a_or_b: # "object" has no attribute "__enter__" & "object" has no attribute "__exit__"
...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.
Metadata
Metadata
Assignees
Labels
bugmypy got something wrongmypy got something wrongtopic-join-v-unionUsing join vs. using unionsUsing join vs. using unions