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
context_expr = Bound() if flag else EnterUnbound()
103
+
104
+
# error: [invalid-context-manager] "Object of type `Bound | EnterUnbound` cannot be used with `async with` because the method `__aenter__` of `EnterUnbound` is possibly unbound"
# error: [invalid-context-manager] "Object of type `Bound | ExitUnbound` cannot be used with `async with` because the method `__aexit__` of `ExitUnbound` is possibly unbound"
# error: [invalid-context-manager] "Object of type `Bound | Unbound` cannot be used with `async with` because the methods `__aenter__` and `__aexit__` of `Unbound` are possibly unbound"
# error: [invalid-context-manager] "Object of type `Bound | EnterUnbound | ExitUnbound` cannot be used with `async with` because the method `__aenter__` of `EnterUnbound` is possibly unbound, and the method `__aexit__` of `ExitUnbound` is possibly unbound"
### Union with multiple members missing the same methods
176
+
177
+
```py
178
+
asyncdef_(flag: int):
179
+
classEnterUnbound:
180
+
asyncdef__aexit__(self): ...
181
+
182
+
classExitUnbound:
183
+
asyncdef__aenter__(self): ...
184
+
185
+
classUnbound: ...
186
+
if flag ==0:
187
+
context_expr = EnterUnbound()
188
+
elif flag ==1:
189
+
context_expr = ExitUnbound()
190
+
else:
191
+
context_expr = Unbound()
192
+
193
+
# error: [invalid-context-manager] "Object of type `EnterUnbound | ExitUnbound | Unbound` cannot be used with `async with` because the method `__aenter__` of `EnterUnbound` and `Unbound` are possibly unbound, and the method `__aexit__` of `ExitUnbound` and `Unbound` are possibly unbound"
194
+
asyncwith context_expr:
195
+
...
196
+
```
197
+
87
198
## Context manager with non-callable `__aexit__` attribute
88
199
89
200
```py
@@ -113,7 +224,7 @@ async def _(flag: bool):
113
224
classNotAContextManager: ...
114
225
context_expr = Manager1() if flag else NotAContextManager()
115
226
116
-
# error: [invalid-context-manager] "Object of type `Manager1 | NotAContextManager` cannot be used with `async with` because the methods `__aenter__` and `__aexit__` are possibly unbound"
227
+
# error: [invalid-context-manager] "Object of type `Manager1 | NotAContextManager` cannot be used with `async with` because the methods `__aenter__` and `__aexit__` of `NotAContextManager` are possibly unbound"
context_expr = Bound() if flag else EnterUnbound()
104
+
105
+
# error: [invalid-context-manager] "Object of type `Bound | EnterUnbound` cannot be used with `with` because the method `__enter__` of `EnterUnbound` is possibly unbound"
# error: [invalid-context-manager] "Object of type `Bound | ExitUnbound` cannot be used with `with` because the method `__exit__` of `ExitUnbound` is possibly unbound"
# error: [invalid-context-manager] "Object of type `Bound | Unbound` cannot be used with `with` because the methods `__enter__` and `__exit__` of `Unbound` are possibly unbound"
# error: [invalid-context-manager] "Object of type `Bound | EnterUnbound | ExitUnbound` cannot be used with `with` because the method `__enter__` of `EnterUnbound` is possibly unbound, and the method `__exit__` of `ExitUnbound` is possibly unbound"
172
+
with context_expr as f:
173
+
reveal_type(f) # revealed: str | Unknown
174
+
```
175
+
176
+
### Union with multiple members missing the same methods
177
+
178
+
```py
179
+
def_(flag: int):
180
+
classEnterUnbound:
181
+
def__exit__(self): ...
182
+
183
+
classExitUnbound:
184
+
def__enter__(self): ...
185
+
186
+
classUnbound: ...
187
+
if flag ==0:
188
+
context_expr = EnterUnbound()
189
+
elif flag ==1:
190
+
context_expr = ExitUnbound()
191
+
else:
192
+
context_expr = Unbound()
193
+
194
+
# error: [invalid-context-manager] "Object of type `EnterUnbound | ExitUnbound | Unbound` cannot be used with `with` because the method `__enter__` of `EnterUnbound` and `Unbound` are possibly unbound, and the method `__exit__` of `ExitUnbound` and `Unbound` are possibly unbound"
195
+
with context_expr:
196
+
...
197
+
```
198
+
88
199
## Context manager with non-callable `__exit__` attribute
89
200
90
201
```py
@@ -113,7 +224,7 @@ def _(flag: bool):
113
224
classNotAContextManager: ...
114
225
context_expr = Manager1() if flag else NotAContextManager()
115
226
116
-
# error: [invalid-context-manager] "Object of type `Manager1 | NotAContextManager` cannot be used with `with` because the methods `__enter__` and `__exit__` are possibly unbound"
227
+
# error: [invalid-context-manager] "Object of type `Manager1 | NotAContextManager` cannot be used with `with` because the methods `__enter__` and `__exit__` of `NotAContextManager` are possibly unbound"
0 commit comments