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

Issue warning for enum with no members in stub #18068

Merged
merged 5 commits into from
Oct 30, 2024

Conversation

hauntsaninja
Copy link
Collaborator

@hauntsaninja hauntsaninja commented Oct 29, 2024

Follow up to #17207

This comment has been minimized.

CHANGELOG.md Show resolved Hide resolved
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":
Copy link
Member

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?

Copy link
Collaborator Author

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

Copy link
Contributor

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:
Copy link
Member

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?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yup

@hauntsaninja hauntsaninja merged commit 58f7628 into python:master Oct 30, 2024
20 checks passed
@hauntsaninja hauntsaninja deleted the enum-followup branch October 30, 2024 00:21
hauntsaninja added a commit to hauntsaninja/pandas-stubs that referenced this pull request Oct 30, 2024
hauntsaninja added a commit to hauntsaninja/django-stubs that referenced this pull request Oct 30, 2024
sobolevn pushed a commit to typeddjango/django-stubs that referenced this pull request Oct 30, 2024
* 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>
Dr-Irv pushed a commit to pandas-dev/pandas-stubs that referenced this pull request Oct 30, 2024
* 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

* .

* .
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.

2 participants