Skip to content

AbstractContextManger.__enter__ should be abstract #15583

@KotlinIsland

Description

@KotlinIsland

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    stubs: improvementImprove/refactor existing annotations, other stubs issues

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions