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
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
Bug Report
AbstractContextManager defines a
__enter__
that returns selfBut it's impossible to just inherit from
contextlib.AbstractContextManager
and have the type of__enter__
be correctly assumed to beSelf
, without a bit of boilerplate.To Reproduce
and mypy disallows writing
and doesn't issue a warning for this runtime error:
Full example here
https://mypy-play.net/?mypy=latest&python=3.11&gist=7e76e56be6c702005a7ffbb7846b69c2
Expected Behavior
Either
Self
is allowed in theAbstractContextManager
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__
impelmentationYour Environment
The text was updated successfully, but these errors were encountered: