Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enums with only descriptor members should not be final #12494

Open
decorator-factory opened this issue Mar 30, 2022 · 0 comments
Open

Enums with only descriptor members should not be final #12494

decorator-factory opened this issue Mar 30, 2022 · 0 comments
Labels
bug mypy got something wrong topic-descriptors Properties, class vs. instance attributes topic-enum topic-runtime-semantics mypy doesn't model runtime semantics correctly

Comments

@decorator-factory
Copy link

decorator-factory commented Mar 30, 2022

Bug Report

If all members of an Enum class are descriptors, the class should still be subclassable.

Quote from the Python documentation:

The rules for what is allowed are as follows: names that start and end with a single underscore are reserved by enum and cannot be used; all other attributes defined within an enumeration will become members of this enumeration, with the exception of special methods (__str__(), __add__(), etc.), descriptors (methods are also descriptors), and variable names listed in _ignore_.

This would also match the behaviour of Pyright (1.1.234)

To Reproduce

  1. Install mypy==0.941
  2. Run mypy on this code:
import logging
from enum import Enum


class LoggedAccess:
    def __set_name__(self, owner, name):
        self.public_name = name
        self.private_name = '_' + name

    def __get__(self, obj, objtype=None):
        value = getattr(obj, self.private_name)
        logging.info('Accessing %r giving %r', self.public_name, value)
        return value

    def __set__(self, obj, value):
        logging.info('Updating %r to %r', self.public_name, value)
        setattr(obj, self.private_name, value)


class Foo(Enum):
    thing = LoggedAccess()

    
class Bar(Foo):
    x = 42

Expected Behavior

No errors - this runs just fine

Actual Behavior

 error: Cannot extend enum with existing members: "Foo"

Your Environment

  • Mypy version used: 0.941
@decorator-factory decorator-factory added the bug mypy got something wrong label Mar 30, 2022
@AlexWaygood AlexWaygood added topic-enum topic-runtime-semantics mypy doesn't model runtime semantics correctly labels Mar 30, 2022
@AlexWaygood AlexWaygood added the topic-descriptors Properties, class vs. instance attributes label Apr 26, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong topic-descriptors Properties, class vs. instance attributes topic-enum topic-runtime-semantics mypy doesn't model runtime semantics correctly
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants