Code sample in basedpyright playground
from contextlib import AbstractContextManager
from typing import override
class CM(AbstractContextManager[int]):
@override
def __exit__(self, *_): ...
with CM() as i:
print(i + 1)
AbstractContextManger.__enter__ has a default implementation that returns self, so by making it abstract you would require implementations to provide the correct implementation. this would force a redundant implementation if the type is Self, but that is currently impossible to type anyway: https://discuss.python.org/t/self-as-typevar-default/90939
even if we did have Self support, it would only require one line of redundant code: def __enter__(self) -> Self: return self
Code sample in basedpyright playground
AbstractContextManger.__enter__has a default implementation that returnsself, so by making it abstract you would require implementations to provide the correct implementation. this would force a redundant implementation if the type isSelf, but that is currently impossible to type anyway: https://discuss.python.org/t/self-as-typevar-default/90939even if we did have
Selfsupport, it would only require one line of redundant code:def __enter__(self) -> Self: return self