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
AbstractContextManager.__enter__ not properly annotated #13950
Per the Python docs, AbstractContextManager provides a default __enter__ implementation which just returns self. But this does not appear to be properly type annotated (using Self):
This is due to the duality of AbstractContextManager being both an ABC and a protocol. As a protocol it makes sense that it is generic over the return type of __enter__. Ideally we'd give the type variable a default of Self, but I'm not sure that that's possible.
Activity
srittau commentedon May 6, 2025
This is due to the duality of
AbstractContextManagerbeing both an ABC and a protocol. As a protocol it makes sense that it is generic over the return type of__enter__. Ideally we'd give the type variable a default ofSelf, but I'm not sure that that's possible.srittau commentedon May 6, 2025
I've opened #13951 as a example. I've also opened a discussion on discuss.python.org.
mharding-hpe commentedon May 6, 2025
Yeah, I tried using
AbstractContextManager[Self], but that gave an error sayingSelfcould only be used inside the class body itself.If it isn't possible to have it default to
Self, at least the "workaround" I used inA2isn't too painful.srittau commentedon May 11, 2025
Considering that
Selfcan't be used as a default, and the discussion doesn't look encouraging, I'm going to close this as "can't fix" for now.