-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Marks enums with values as implicitly final #11247
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -76,7 +76,8 @@ | |
get_nongen_builtins, get_member_expr_fullname, REVEAL_TYPE, | ||
REVEAL_LOCALS, is_final_node, TypedDictExpr, type_aliases_source_versions, | ||
EnumCallExpr, RUNTIME_PROTOCOL_DECOS, FakeExpression, Statement, AssignmentExpr, | ||
ParamSpecExpr, EllipsisExpr | ||
ParamSpecExpr, EllipsisExpr, | ||
FuncBase, implicit_module_attrs, | ||
) | ||
from mypy.tvar_scope import TypeVarLikeScope | ||
from mypy.typevars import fill_typevars | ||
|
@@ -95,7 +96,6 @@ | |
) | ||
from mypy.typeops import function_type | ||
from mypy.type_visitor import TypeQuery | ||
from mypy.nodes import implicit_module_attrs | ||
from mypy.typeanal import ( | ||
TypeAnalyser, analyze_type_alias, no_subscript_builtin_alias, | ||
TypeVarLikeQuery, TypeVarLikeList, remove_dups, has_any_from_unimported_type, | ||
|
@@ -1096,8 +1096,8 @@ def analyze_ 8000 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( | ||
defn, bases, context=defn) | ||
|
||
for tvd in tvar_defs: | ||
if any(has_placeholder(t) for t in [tvd.upper_bound] + tvd.values): | ||
|
@@ -1521,6 +1521,19 @@ def configure_base_classes(self, | |
elif isinstance(base, Instance): | ||
if base.type.is_newtype: | ||
self.fail('Cannot subclass "NewType"', defn) | ||
if ( | ||
base.type.is_enum | ||
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)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What about a nested class definition? Would it be fine? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
for n in base.type.names.values()) | ||
): | ||
# This means that are trying to subclass a non-default | ||
# Enum class, with defined members. This is not possible. | ||
# In runtime, it will raise. We need to mark this type as final. | ||
# However, methods can be defined on a type: only values can't. | ||
base.type.is_final = True | ||
base_types.append(base) | ||
elif isinstance(base, AnyType): | ||
if self.options.disallow_subclassing_any: | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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:

Vs now:
