python / python/typeshed

`AbstractContextManger.__enter__` should be abstract

Open
#15,583 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

stubs: improvement
Dominant language
Python
Stars
5.1k
Forks
2.1k
Avg merge
1d 19h
Merged PRs (30d)
82

Description

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 without Self support, it is only one line of redundant code: def __enter__(self) -> Self: return self

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the basedpyright playground example and the AbstractContextManager stub entry point it exercises. Check how the current default enter affects abstract-class checking, then verify that making it abstract requires an implementation while preserving the intended context-manager typing behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
tooling
Issue type
Feature
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
55/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.