Skip to content

TYP: ExtensionDtype.name/type as ClassVar #54458

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 3 commits into from
Aug 23, 2023
Merged

TYP: ExtensionDtype.name/type as ClassVar #54458

merged 3 commits into from
Aug 23, 2023

Conversation

twoertwein
Copy link
Member

xref #51736

ExtensionDtype.name/type is sometimes a property and sometimes a class variable. Going either way would require non-typing changes. I believe the ClassVar approach is used more frequently and also mandated by some of the register methods.

If ClassVar is the main way of defining type/name, let's annotate as much as possible with that and ignore the few cases that use properties:

pandas/core/dtypes/dtypes.py:854: error: Cannot override writeable attribute with read-only property  [override]
pandas/core/dtypes/dtypes.py:1048: error: Cannot override writeable attribute with read-only property  [override]
pandas/core/dtypes/dtypes.py:1422: error: Cannot override writeable attribute with read-only property  [override]
pandas/core/dtypes/dtypes.py:1429: error: Cannot override writeable attribute with read-only property  [override]
pandas/core/dtypes/dtypes.py:1766: error: Cannot override writeable attribute with read-only property  [override]

I will fix this PR after a decision has been made.

@mroeschke
Copy link
Member

Since the base EDtype defined it as property previously, going to ClassVar would be an API change that would make it writable (which I think we should make raise) cc @jbrockmendel

@twoertwein
Copy link
Member Author

twoertwein commented Aug 8, 2023

Since the base EDtype defined it as property previously, going to ClassVar would be an API change that would make it writable (which I think we should make raise) cc @jbrockmendel

I would agree with that (and the previous PR was aimed to making it a property; it also makes it more explicit that subclasses have to implement it) but in practice, it seems that it has to be a ClassVar (at least for .name)

assert isinstance(cls.name, str), (cls, type(cls.name))

above is called when trying to register a user-defined ExtensionDtype

Might be good to discuss ClassVar/property separately for name and type (both are themselves not consistent).

edit: When changing these two lines to a property in the pandas-stubs tests, and then run the tests, it fails
https://github.com/pandas-dev/pandas-stubs/blob/main/tests/extension/decimal/array.py#L41

@jbrockmendel
Copy link
Member

seems like we have a check for .name but not for .type? .type being a property matters for ArrowDtype.

In general I follow @simonjayhawkins approach of not wanting to futz with code just to making typing checks happy

@twoertwein
Copy link
Member Author

I'm happy to focus only on name and try to create a minimally invasive PR :) Personally, I think it would be better to also change name in ExtensionDtype to a ClassVar (as it also confuses users - clearly, they cannot use a property (asserts) but they are expected to use one from an inheritance perspective) but then we cannot have a doc-string anymore.

@twoertwein
Copy link
Member Author

I updated the PR (removed all .type changes and all runtime code changes). I think the current state is better than on main but far from ideal: it should be consistent but that requires code changes and maybe even API changes if the property approach is preferred.

@twoertwein twoertwein marked this pull request as ready for review August 9, 2023 22:00
@mroeschke mroeschke added the Typing type annotations, mypy/pyright type checking label Aug 10, 2023
@mroeschke mroeschke added this to the 2.1 milestone Aug 10, 2023
Copy link
Member

@mroeschke mroeschke left a comment

Choose a reason for hiding this comment

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

LGTM merge when ready @jbrockmendel

@mroeschke mroeschke modified the milestones: 2.1, 2.2 Aug 23, 2023
@mroeschke mroeschke merged commit 65f22a0 into pandas-dev:main Aug 23, 2023
@mroeschke
Copy link
Member

Thanks @twoertwein

@twoertwein twoertwein deleted the extensiondtype branch September 19, 2023 15:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Typing type annotations, mypy/pyright type checking
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants