Skip to content

Sync enum members logic in typeanal and checkmember #19687

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

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

sterliakov
Copy link
Collaborator

@sterliakov sterliakov commented Aug 19, 2025

Fixes #19686.
Fixes #15540.
Fixes #14600.

This comment has been minimized.

@sterliakov sterliakov changed the title Do not treat unassigned attributes as enum members Sync enum members logic in typeanal and checkmember Aug 19, 2025
@sterliakov
Copy link
Collaborator Author

sterliakov commented Aug 19, 2025

All new errors in primer stem from invalid enum definitions in stubs - bare annotations are not considered enum members, even in stubs (both by spec and by common sense). To hide a value, stubs should assign ellipsis to enum members. Here's the relevant spec chapter: https://typing.python.org/en/latest/spec/enums.html#defining-members

Do we have direct contact with pandas team? Should I open a ticket to let them know and get that fixed?

https://github.com/pandas-dev/pandas/blob/3940df8255ed04db0089e66ce09a4c986b97cac4/pandas/_libs/tslibs/dtypes.pyi#L30-L43

https://github.com/srittau/python-htmlgen/blob/01e3b911ac282d067850bb324b913e3594bc0e23/htmlgen/video.pyi#L6-L9

Copy link
Contributor

Diff from mypy_primer, showing the effect of this PR on open source code:

pandas (https://github.com/pandas-dev/pandas)
+ pandas/core/arrays/period.py:1438: error: "int" has no attribute "value"  [attr-defined]
+ pandas/core/arrays/period.py:1442: error: "int" has no attribute "value"  [attr-defined]
+ pandas/core/tools/datetimes.py:531: error: "int" has no attribute "value"  [attr-defined]
+ pandas/tests/tslibs/test_strptime.py:18: error: "int" has no attribute "value"  [attr-defined]
+ pandas/tests/tslibs/test_array_to_datetime.py:22: error: "int" has no attribute "value"  [attr-defined]
+ pandas/plotting/_matplotlib/converter.py:534: error: "int" has no attribute "value"  [attr-defined]
+ pandas/plotting/_matplotlib/converter.py:539: error: Non-overlapping equality check (left operand type: "FreqGroup", right operand type: "int")  [comparison-overlap]
+ pandas/plotting/_matplotlib/converter.py:542: error: Non-overlapping equality check (left operand type: "FreqGroup", right operand type: "int")  [comparison-overlap]
+ pandas/plotting/_matplotlib/converter.py:545: error: Non-overlapping equality check (left operand type: "FreqGroup", right operand type: "int")  [comparison-overlap]
+ pandas/plotting/_matplotlib/converter.py:548: error: Non-overlapping equality check (left operand type: "FreqGroup", right operand type: "int")  [comparison-overlap]
+ pandas/plotting/_matplotlib/converter.py:551: error: Non-overlapping equality check (left operand type: "FreqGroup", right operand type: "int")  [comparison-overlap]
+ pandas/plotting/_matplotlib/converter.py:554: error: Non-overlapping equality check (left operand type: "FreqGroup", right operand type: "int")  [comparison-overlap]
+ pandas/plotting/_matplotlib/converter.py:691: error: "int" has no attribute "value"  [attr-defined]
+ pandas/plotting/_matplotlib/converter.py:911: error: Non-overlapping equality check (left operand type: "FreqGroup", right operand type: "int")  [comparison-overlap]
+ pandas/plotting/_matplotlib/converter.py:913: error: Non-overlapping equality check (left operand type: "FreqGroup", right operand type: "int")  [comparison-overlap]
+ pandas/plotting/_matplotlib/converter.py:915: error: Non-overlapping equality check (left operand type: "FreqGroup", right operand type: "int")  [comparison-overlap]
+ pandas/plotting/_matplotlib/converter.py:917: error: "int" has no attribute "value"  [attr-defined]
+ pandas/plotting/_matplotlib/converter.py:917: error: Non-overlapping equality check (left operand type: "FreqGroup", right operand type: "int")  [comparison-overlap]
+ pandas/plotting/_matplotlib/timeseries.py:256: error: "int" has no attribute "value"  [attr-defined]

python-htmlgen (https://github.com/srittau/python-htmlgen)
+ test_htmlgen/video.py:38: error: Incompatible types in assignment (expression has type "str", variable has type "Preload | None")  [assignment]
+ test_htmlgen/form.py:51: error: Incompatible types in assignment (expression has type "str", variable has type "Autocomplete | None")  [assignment]

@brianschubert
Copy link
Member

cc @Dr-Irv for pandas typing

@Dr-Irv
Copy link

Dr-Irv commented Aug 19, 2025

cc @Dr-Irv for pandas typing

Thanks for the ping. Here is what is happening here. We have some enums defined in cython, which is where the values get set. So in our PYI files, we do things like this:

class FreqGroup(Enum):
    FR_ANN: int
    FR_QTR: int
    FR_MTH: int
    FR_WK: int
    FR_BUS: int
    FR_DAY: int

Then we have code that has things like FreqGroup.FR_MTH.value which is what causes the errors reported above.

Now, for the ones we define in cython, we can change this to the actual values in the PYI files. Not ideal - then we have to maintain the numerical values in two places.

But we also have some that are dependent on values stored in the C code in numpy. Getting those might be a bit difficult.

What's the best way to handle this?

@brianschubert
Copy link
Member

brianschubert commented Aug 19, 2025

You can use ... as a placeholder to omit the runtime values, e.g.

class FreqGroup(Enum):
    FR_ANN = ...

https://typing.python.org/en/latest/spec/enums.html#:~:text=Within%20a%20type%20stub,can%20be%20used

To include type information, you can use cast(int, ...)

@Dr-Irv
Copy link

Dr-Irv commented Aug 19, 2025

To include type information, you can use cast(int, ...)

I think the following will work:

class FreqGroup(Enum):
    _value_ : int
    FR_ANN = ...

and then we won't need to use cast

@sterliakov
Copy link
Collaborator Author

Yeah, we added _value_ support recently (#19352), _value_ and ellipsis initializer should work now.

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