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

@willfrey

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!

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