Skip to content

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

Closed

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

Activity

  1. sunmy2019 commented on Jun 13, 2023

    @sunmy2019
    Member

    Indeed, can you open a PR for that?

  2. erezinman commented on Jun 13, 2023

    @erezinman
    Author

    @sunmy2019
    Yeah, I'll try to get to that later. To which branch?

  3. sunmy2019 commented on Jun 13, 2023

    @sunmy2019
    Member

    Changes like this are considered new features. So main

  4. added 2 commits that reference this issue on Jul 15, 2023
    22954c4
    a4bd151
  5. added a commit that references this issue on Jul 16, 2023
  6. sobolevn commented on Jul 16, 2023

    @sobolevn
    Member
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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