-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
nullcontext treated as having no __enter__ or __exit__ method #10109
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
The issue isn't with |
By randomly using one or two assets we test both branches *eventually,* and since this is a particularly expensive test the tradeoff between learning about any issues early and a faster run is probably worth it at least in the short term. Had to make `S3Object` a proper `ContextManager` subclass after working around a `mypy` limitation related to grouping brackets <python/mypy#10109 (comment)>.
Thank you @JelleZijlstra, is there an overarching issue I can follow? That way we can also close this as a duplicate. |
By randomly using one or two assets we test both branches *eventually,* and since this is a particularly expensive test the tradeoff between learning about any issues early and a faster run is probably worth it at least in the short term. Had to make `S3Object` a proper `ContextManager` subclass after working around a `mypy` limitation related to grouping brackets <python/mypy#10109 (comment)>.
By randomly using one or two assets we test both branches *eventually,* and since this is a particularly expensive test the tradeoff between learning about any issues early and a faster run is probably worth it at least in the short term. Had to make `S3Object` a proper `ContextManager` subclass after working around a `mypy` limitation related to grouping brackets <python/mypy#10109 (comment)>.
By randomly using one or two assets we test both branches *eventually,* and since this is a particularly expensive test the tradeoff between learning about any issues early and a faster run is probably worth it at least in the short term. Had to make `S3Object` a proper `ContextManager` subclass after working around a `mypy` limitation related to grouping brackets <python/mypy#10109 (comment)>.
The workaround described doesn't seem to work for me. With: from contextlib import nullcontext
from random import getrandbits
from typing import ContextManager
example: ContextManager[None] = (open("path") if bool(getrandbits(1)) else nullcontext())
with example:
pass I still get:
|
I have to use a from contextlib import nullcontext
from random import getrandbits
from typing import ContextManager, TextIO
example: TextIO | ContextManager[None] = (open("path") if bool(getrandbits(1)) else nullcontext())
with example:
pass https://mypy-play.net/?mypy=latest&python=3.10&flags=strict&gist=d3c744b47fe06aba0782a8b0672d0240 |
Thanks Tom, that works for me also! |
Seems like a duplicate of #5512. |
Yes, one of many in the #join-v-union genre. |
Bug Report
Using
nullcontext
to randomly get a specific context manager results inmypy
errors.To Reproduce
Run
mypy
on the following code:Expected Behavior
As far as I can tell from looking at contextlib.py it does have
__enter__
and__exit__
, so this error should not happen.Actual Behavior
Your Environment
Mypy version used: 0.800
Mypy command-line flags: None
Mypy configuration options from
mypy.ini
(and other config files):Python version used: 3.8.6
Operating system and version: Arch Linux
The text was updated successfully, but these errors were encountered: