-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Issue warning for enum with no members in stub #18068
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
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2588,20 +2588,30 @@ def check_typevar_defaults(self, tvars: Sequence[TypeVarLikeType]) -> None: | |
|
|
||
| def check_enum(self, defn: ClassDef) -> None: | ||
| assert defn.info.is_enum | ||
| if defn.info.fullname not in ENUM_BASES: | ||
| for sym in defn.info.names.values(): | ||
| if ( | ||
| isinstance(sym.node, Var) | ||
| and sym.node.has_explicit_value | ||
| and sym.node.name == "__members__" | ||
| ): | ||
| # `__members__` will always be overwritten by `Enum` and is considered | ||
| # read-only so we disallow assigning a value to it | ||
| self.fail(message_registry.ENUM_MEMBERS_ATTR_WILL_BE_OVERRIDEN, sym.node) | ||
| if defn.info.fullname not in ENUM_BASES and "__members__" in defn.info.names: | ||
|
Member
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. This is just an optimization because previously we were iterating over the whole dict unnecessarily, right?
Collaborator
Author
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. Yup |
||
| sym = defn.info.names["__members__"] | ||
| if isinstance(sym.node, Var) and sym.node.has_explicit_value: | ||
| # `__members__` will always be overwritten by `Enum` and is considered | ||
| # read-only so we disallow assigning a value to it | ||
| self.fail(message_registry.ENUM_MEMBERS_ATTR_WILL_BE_OVERRIDEN, sym.node) | ||
| for base in defn.info.mro[1:-1]: # we don't need self and `object` | ||
| if base.is_enum and base.fullname not in ENUM_BASES: | ||
| self.check_final_enum(defn, base) | ||
|
|
||
| if self.is_stub and not self.is_typeshed_stub and self.tree.fullname != "enum": | ||
|
||
| if not defn.info.enum_members: | ||
| self.fail( | ||
| "Detected an enum in a type stub with zero members. " | ||
| "There is a chance this is due to a recent change in the semantics of " | ||
| "enum membership. If so, use `member = ...` to mark an enum member, " | ||
| "instead of `member: type`", | ||
| defn, | ||
| ) | ||
| self.note( | ||
| "See https://typing.readthedocs.io/en/latest/spec/enums.html#defining-members", | ||
| defn, | ||
| ) | ||
|
|
||
| self.check_enum_bases(defn) | ||
| self.check_enum_new(defn) | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.