-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
Issue warning for enum with no members in stub #18068
Conversation
This comment has been minimized.
This comment has been minimized.
mypy/checker.py
Outdated
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": |
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.
Why do we need to exclude typeshed? Didn't we already fix this in typeshed?
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.
This was before I added the check against enum.pyi specifically. Looks like I'd also need to silence _typeshed
because it has some StrEnum stuff
Diff from mypy_primer, showing the effect of this PR on open source code: python-htmlgen (https://github.com/srittau/python-htmlgen)
+ htmlgen/form.pyi:8: error: Detected enum "htmlgen.form.Autocomplete" 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 = value` to mark an enum member, instead of `member: type` [misc]
+ htmlgen/form.pyi:8: note: See https://typing.readthedocs.io/en/latest/spec/enums.html#defining-members
+ htmlgen/video.pyi:6: error: Detected enum "htmlgen.video.Preload" 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 = value` to mark an enum member, instead of `member: type` [misc]
+ htmlgen/video.pyi:6: note: See https://typing.readthedocs.io/en/latest/spec/enums.html#defining-members
Tanjun (https://github.com/FasterSpeeding/Tanjun)
+ tanjun/_internal/vendor/inspect.pyi:196: error: Detected enum "tanjun._internal.vendor.inspect._ParameterKind" 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 = value` to mark an enum member, instead of `member: type` [misc]
+ tanjun/_internal/vendor/inspect.pyi:196: note: See https://typing.readthedocs.io/en/latest/spec/enums.html#defining-members
pandas (https://github.com/pandas-dev/pandas)
+ pandas/_libs/tslibs/dtypes.pyi:31: error: Detected enum "pandas._libs.tslibs.dtypes.FreqGroup" 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 = value` to mark an enum member, instead of `member: type` [misc]
+ pandas/_libs/tslibs/dtypes.pyi:31: note: See https://typing.readthedocs.io/en/latest/spec/enums.html#defining-members
+ pandas/_libs/tslibs/dtypes.pyi:48: error: Detected enum "pandas._libs.tslibs.dtypes.Resolution" 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 = value` to mark an enum member, instead of `member: type` [misc]
+ pandas/_libs/tslibs/dtypes.pyi:48: note: See https://typing.readthedocs.io/en/latest/spec/enums.html#defining-members
+ pandas/_libs/tslibs/dtypes.pyi:70: error: Detected enum "pandas._libs.tslibs.dtypes.NpyDatetimeUnit" 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 = value` to mark an enum member, instead of `member: type` [misc]
+ pandas/_libs/tslibs/dtypes.pyi:70: note: See https://typing.readthedocs.io/en/latest/spec/enums.html#defining-members
django-stubs (https://github.com/typeddjango/django-stubs)
+ django-stubs/db/models/constants.pyi:5: error: Detected enum "django.db.models.constants.OnConflict" 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 = value` to mark an enum member, instead of `member: type` [misc]
+ django-stubs/db/models/constants.pyi:5: note: See https://typing.readthedocs.io/en/latest/spec/enums.html#defining-members
+ django-stubs/contrib/gis/gdal/srs.pyi:7: error: Detected enum "django.contrib.gis.gdal.srs.AxisOrder" 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 = value` to mark an enum member, instead of `member: type` [misc]
+ django-stubs/contrib/gis/gdal/srs.pyi:7: note: See https://typing.readthedocs.io/en/latest/spec/enums.html#defining-members
+ django-stubs/template/base.pyi:28: error: Detected enum "django.template.base.TokenType" 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 = value` to mark an enum member, instead of `member: type` [misc]
+ django-stubs/template/base.pyi:28: note: See https://typing.readthedocs.io/en/latest/spec/enums.html#defining-members
+ django-stubs/db/models/enums.pyi:44: error: Detected enum "django.db.models.enums.Choices" 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 = value` to mark an enum member, instead of `member: type` [misc]
+ django-stubs/db/models/enums.pyi:44: note: See https://typing.readthedocs.io/en/latest/spec/enums.html#defining-members
+ django-stubs/db/models/enums.pyi:63: error: Detected enum "django.db.models.enums.IntegerChoices" 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 = value` to mark an enum member, instead of `member: type` [misc]
+ django-stubs/db/models/enums.pyi:63: note: See https://typing.readthedocs.io/en/latest/spec/enums.html#defining-members
+ django-stubs/db/models/enums.pyi:79: error: Detected enum "django.db.models.enums.TextChoices" 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 = value` to mark an enum member, instead of `member: type` [misc]
+ django-stubs/db/models/enums.pyi:79: note: See https://typing.readthedocs.io/en/latest/spec/enums.html#defining-members
+ django-stubs/db/models/constraints.pyi:12: error: Detected enum "django.db.models.constraints.Deferrable" 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 = value` to mark an enum member, instead of `member: type` [misc]
+ django-stubs/db/models/constraints.pyi:12: note: See https://typing.readthedocs.io/en/latest/spec/enums.html#defining-members
+ django-stubs/contrib/admin/options.pyi:47: error: Detected enum "django.contrib.admin.options.ShowFacets" 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 = value` to mark an enum member, instead of `member: type` [misc]
+ django-stubs/contrib/admin/options.pyi:47: note: See https://typing.readthedocs.io/en/latest/spec/enums.html#defining-members
pandas-stubs (https://github.com/pandas-dev/pandas-stubs)
+ pandas-stubs/core/interchange/dataframe_protocol.pyi:16: error: Detected enum "pandas.core.interchange.dataframe_protocol.DlpackDeviceType" 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 = value` to mark an enum member, instead of `member: type` [misc]
+ pandas-stubs/core/interchange/dataframe_protocol.pyi:16: note: See https://typing.readthedocs.io/en/latest/spec/enums.html#defining-members
+ pandas-stubs/core/interchange/dataframe_protocol.pyi:26: error: Detected enum "pandas.core.interchange.dataframe_protocol.DtypeKind" 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 = value` to mark an enum member, instead of `member: type` [misc]
+ pandas-stubs/core/interchange/dataframe_protocol.pyi:26: note: See https://typing.readthedocs.io/en/latest/spec/enums.html#defining-members
+ pandas-stubs/core/interchange/dataframe_protocol.pyi:35: error: Detected enum "pandas.core.interchange.dataframe_protocol.ColumnNullType" 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 = value` to mark an enum member, instead of `member: type` [misc]
+ pandas-stubs/core/interchange/dataframe_protocol.pyi:35: note: See https://typing.readthedocs.io/en/latest/spec/enums.html#defining-members
+ pandas-stubs/_libs/tslibs/dtypes.pyi:31: error: Detected enum "pandas._libs.tslibs.dtypes.Resolution" 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 = value` to mark an enum member, instead of `member: type` [misc]
+ pandas-stubs/_libs/tslibs/dtypes.pyi:31: note: See https://typing.readthedocs.io/en/latest/spec/enums.html#defining-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: |
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.
This is just an optimization because previously we were iterating over the whole dict unnecessarily, right?
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.
Yup
See https://typing.readthedocs.io/en/latest/spec/enums.html#defining-members python/typing-council#11 The next version of mypy will obey the spec change: python/mypy#18068 pyright already requires this
See https://typing.readthedocs.io/en/latest/spec/enums.html#defining-members python/typing-council#11 The next version of mypy will obey the spec change: python/mypy#18068 pyright already requires this
* Update enums to reflect typing spec changes See https://typing.readthedocs.io/en/latest/spec/enums.html#defining-members python/typing-council#11 The next version of mypy will obey the spec change: python/mypy#18068 pyright already requires this Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
* Update enums to reflect typing spec changes See https://typing.readthedocs.io/en/latest/spec/enums.html#defining-members python/typing-council#11 The next version of mypy will obey the spec change: python/mypy#18068 pyright already requires this * warning * . * .
Follow up to #17207