Skip to content
This repository was archived by the owner on Apr 14, 2022. It is now read-only.
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

Closed

Description

@willfrey

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.

import contextlib

with contextlib.ExitStack() as stack:
    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.

Any idea what's going on here?

Thanks!

Activity

  1. CTrando commented on Sep 4, 2019

    @CTrando
    Contributor

    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.

  2. willfrey commented on Sep 4, 2019

    @willfrey
    Author

    Thanks for your response and pending fix!

  3. CTrando commented on Sep 4, 2019

    @CTrando
    Contributor

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions