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
{{ message }}
This repository was archived by the owner on Apr 14, 2022. It is now read-only.
Repository navigation
This repository was archived by the owner on Apr 14, 2022. It is now read-only.
Value returned by enter_context method of contextlib.ExitStack appears as __NoneType__ #1499
I'm having an issue with the returned value of the enter_context(...) instance method of contextlib.ExitStack.
If you enter the following snippet into VSCode and mouse-over the file variable, the tool-tip says that the inferred type is __NoneType__ but it should be TextIOWrapper.
importcontextlibwithcontextlib.ExitStack() asstack:
file=stack.enter_context(open("temporary-file.txt", mode="w"))
# reveal_type(file) # Revealed type is 'typing.TextIO*'
You can uncomment the reveal_type(file) line with mypy enabled and it will tell you that the revealed type is typing.TextIO*, which is fine for this case.
I've also observed this behavior with tempfile.NamedTemporaryFile.
After looking at contextlib.pyi, we haven't specialized ContextManager, which is part of typing. That's ongoing in #535.
Your issue in particular however is from my PR #1463. What happened is that according to the typing module in cpython, the default parameter for TypeVar.bound is None, so I used that as the upper bound for the type. It should more realistically be object, or even perhaps just return the type variable itself instead of using None. I'll have a fix out soon, sorry for the error.
Activity
CTrando commentedon Sep 4, 2019
After looking at
contextlib.pyi, we haven't specializedContextManager, which is part of typing. That's ongoing in #535.Your issue in particular however is from my PR #1463. What happened is that according to the typing module in cpython, the default parameter for
TypeVar.boundisNone, so I used that as the upper bound for the type. It should more realistically be object, or even perhaps just return the type variable itself instead of usingNone. I'll have a fix out soon, sorry for the error.willfrey commentedon Sep 4, 2019
Thanks for your response and pending fix!
CTrando commentedon Sep 4, 2019
I've made a PR which will fix returning None; however it will take much longer to specialize
ContextManager, hopefully this is enough for now.