Skip to content

Wrong return type from ExitStack.enter_context when given a child of ExitStack #7961

@thatguystone

Description

@thatguystone

Bug Report

The return type from enter_context() is incorrect when the argument is a child of ExitStack.

To Reproduce

# ctx.py

from contextlib import ExitStack

class Thing(ExitStack):
    def foo(self) -> None:
        print("foo")

stack = ExitStack()
thing = stack.enter_context(Thing())
reveal_type(thing)
thing.foo()

Expected Behavior

The type of thing should be Thing, not ExitStack.

Actual Behavior

$ mypy ctx.py 
ctx.py:11: note: Revealed type is "contextlib.ExitStack"
ctx.py:12: error: "ExitStack" has no attribute "foo"
Found 1 error in 1 file (checked 1 source file)

Your Environment

$ mypy --version 
mypy 0.960 (compiled: yes)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions