Skip to content
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

Mypy should allow for Self in parametrized parent class definition — issues with contextlib.AbstractContextManager #17758

Open
Hnasar opened this issue Sep 11, 2024 · 0 comments
Labels
bug mypy got something wrong

Comments

@Hnasar
Copy link
Contributor

Hnasar commented Sep 11, 2024

Bug Report

AbstractContextManager defines a __enter__ that returns self

But it's impossible to just inherit from contextlib.AbstractContextManager and have the type of __enter__ be correctly assumed to be Self, without a bit of boilerplate.

To Reproduce

class A(contextlib.AbstractContextManager):
    pass

reveal_type(A().__enter__())  # should be Self but assumed to be Any

and mypy disallows writing

class B(contextlib.AbstractContextManager[Self]):  # error: Self type is only allowed in annotations within class definition  [misc]
    pass

and doesn't issue a warning for this runtime error:

class C(contextlib.AbstractContextManager[C]):
    pass

Full example here
https://mypy-play.net/?mypy=latest&python=3.11&gist=7e76e56be6c702005a7ffbb7846b69c2

Expected Behavior

Either Self is allowed in the AbstractContextManager parameter list, or the type vars work by default.

Actual Behavior

Errors. See the playground.
(Although pyright doesn't error when using Self in the parameter list, but it still wrongly infers Any as the return of __enter__)

Workaround

Repeat the class name, and repeat the __enter__ impelmentation

class E(contextlib.AbstractContextManager["E"]):
    def __enter__(self) -> Self:
        return self

Your Environment

  • Mypy version used: 1.11.2
  • Mypy command-line flags: n/a
  • Python version used: 3.11
@Hnasar Hnasar added the bug mypy got something wrong label Sep 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong
Projects
None yet
Development

No branches or pull requests

1 participant