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 }}
Repository navigation
Mypy should allow for Self in parametrized parent class definition — issues with contextlib.AbstractContextManager #17758
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
classA(contextlib.AbstractContextManager):
passreveal_type(A().__enter__()) # should be Self but assumed to be Any
and mypy disallows writing
classB(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:
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
I encountered this issue today as well.
I was expecting this to work, especially when considering the following lines in the documentation of typing.Self:
Other common use cases include:
classmethods that are used as alternative constructors and return instances of the cls parameter.
Annotating an __enter__() method which returns self.
I haven't read too much in the discussion, however it looks like they'd like to add a default value to _EnterT_co of the abstract class definition.
What I was wondering was if it was possible to actually use Self to write something like @Hnasar wrote. In that way we can maintain the genericity of the Protocol definition while also not become crazy in writing the types ourselves in case we want to make a base context manager class that expects all the classes inheriting from it to return Self instead of having to write the type.
I don't think this is a sufficiently common problem to warrant a mypy-specific extension, so probably we should wait until semantics of Self parameterising inherited classes are specified.
Activity
Jazzinghen commentedon Jun 22, 2025
I encountered this issue today as well.
I was expecting this to work, especially when considering the following lines in the documentation of
typing.Self:brianschubert commentedon Jun 22, 2025
Linking python/typeshed#13950 for related discussion
Jazzinghen commentedon Jun 23, 2025
I haven't read too much in the discussion, however it looks like they'd like to add a default value to
_EnterT_coof the abstract class definition.What I was wondering was if it was possible to actually use
Selfto write something like @Hnasar wrote. In that way we can maintain the genericity of the Protocol definition while also not become crazy in writing the types ourselves in case we want to make a base context manager class that expects all the classes inheriting from it to returnSelfinstead of having to write the type.sterliakov commentedon Sep 2, 2025
This is explicitly listed in the spec as disallowed: https://typing.python.org/en/latest/spec/generics.html#valid-locations-for-self
I don't think this is a sufficiently common problem to warrant a mypy-specific extension, so probably we should wait until semantics of Self parameterising inherited classes are specified.