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

Marks enums with values as implicitly final #11247

Merged
merged 5 commits into from
Oct 31, 2021
Merged

Marks enums with values as implicitly final #11247

merged 5 commits into from
Oct 31, 2021

Conversation

sobolevn
Copy link
Member

@sobolevn sobolevn commented Oct 2, 2021

Now enums with values cannot be inherited:

from enum import Enum

class NonEmptyEnum(Enum):
    x = 1

class ErrorEnumWithoutValue(NonEmptyEnum):  # E: Cannot inherit from final class "NonEmptyEnum"
    pass

In runtime it caused this error:

Traceback (most recent call last):
  File "/Users/sobolev/Desktop/mypy/ex.py", line 7, in <module>
    class ErrorEnumWithoutValue(NonEmptyEnum):  # E: Cannot inherit from final class "NonEmptyEnum"
  File "/Users/sobolev/.pyenv/versions/3.9.1/lib/python3.9/enum.py", line 127, in __prepare__
    metacls._check_for_existing_members(cls, bases)
  File "/Users/sobolev/.pyenv/versions/3.9.1/lib/python3.9/enum.py", line 483, in _check_for_existing_members
    raise TypeError("%s: cannot extend enumeration %r" % (class_name, base.__name__))
TypeError: ErrorEnumWithoutValue: cannot extend enumeration 'NonEmptyEnum'

Now runtime and mypy are in sync.

Closes #10857

Part of my "better enum" PR series: #10852

@@ -1096,8 +1096,8 @@ def analyze_class(self, defn: ClassDef) -> None:
self.update_metaclass(defn)

bases = defn.base_type_exprs
bases, tvar_defs, is_protocol = self.clean_up_bases_and_infer_type_variables(defn, bases,
context=defn)
bases, tvar_defs, is_protocol = self.clean_up_bases_and_infer_type_variables(
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had to refactor this function, it is unrelated, but this is how it was rendered for me:
Снимок экрана 2021-10-02 в 13 04 16

Vs now:
Снимок экрана 2021-10-02 в 13 55 21

@github-actions

This comment has been minimized.

2 similar comments
@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@sobolevn
Copy link
Member Author

sobolevn commented Oct 2, 2021

Copy link
Collaborator

@JukkaL JukkaL left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR! I like the detailed tests. Looks good, I only have one question.

and base.type.fullname not in (
'enum.Enum', 'enum.IntEnum', 'enum.Flag', 'enum.IntFlag')
and base.type.names
and any(not isinstance(n.node, (FuncBase, Decorator))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about a nested class definition? Would it be fine?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, I've added an explicit test case for it. Any nested class makes enum final.

It mirrors the CPython's behavior:
Снимок экрана 2021-10-08 в 22 11 55

@JukkaL
Copy link
Collaborator

JukkaL commented Oct 8, 2021

Also there's a merge conflict now.

Copy link
Member Author

@sobolevn sobolevn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Rebased.

and base.type.fullname not in (
'enum.Enum', 'enum.IntEnum', 'enum.Flag', 'enum.IntFlag')
and base.type.names
and any(not isinstance(n.node, (FuncBase, Decorator))
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, I've added an explicit test case for it. Any nested class makes enum final.

It mirrors the CPython's behavior:
Снимок экрана 2021-10-08 в 22 11 55

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Enum types cannot be inherited
3 participants