Skip to content

contextlib.ContextManager doesn't contain __slots__ = () #105726

Closed
@erezinman

Description

@erezinman

Python's ABCs should have __slots__ attributes to not interfere with slotting in derived classes (especially when the class has no members). However, it is not the case for contextlib.ContextManager (as for other contextlib ABCs):

from typing import ContextManager

class A(ContextManager[int]):
    __slots__ = ()
    __enter__ = __exit__ = lambda *args: None
    
A().a = 1  # No exception

Changing ContextManager to MutableMapping, for example (and setting relevant dunders), does raise an exception.

Observed on Ubuntu 20.04 and Python 3.9.16.

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    type-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions