Skip to content

TYP: Fix typehints for ExtensionDtype #41097

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 2 commits into from
Apr 26, 2021

Conversation

ozars
Copy link
Contributor

@ozars ozars commented Apr 22, 2021

This is a fix for type hints of ExtensionDtype. register_extension_dtype was confusing pyright autocomplete. It was fixed by parameterizing its return value on its input value.

Example:

import pandas as pd

pd.CategoricalDtype(categories=['a', 'b'])

Before this change:

$ pyright --version
pyright 1.1.133
$ pyright --lib pd.py
No configuration file found.
No pyproject.toml file found.
stubPath /home/omer/src/ext/typings is not a valid directory.
Assuming Python platform Linux
Searching for source files
Found 1 source file
/home/omer/src/ext/pd.py
  /home/omer/src/ext/pd.py:3:1 - error: Expected no arguments to "ExtensionDtype" constructor (reportGeneralTypeIssues)
1 error, 0 warnings, 0 infos
Completed in 0.72sec

After:

$ pyright --lib ../pd.py
No configuration file found.
pyproject.toml file found at /home/omer/src/ext/pandas.
Loading pyproject.toml file at /home/omer/src/ext/pandas/pyproject.toml
Pyproject file "/home/omer/src/ext/pandas/pyproject.toml" is missing "[tool.pyright] section.
stubPath /home/omer/src/ext/pandas/typings is not a valid directory.
Assuming Python platform Linux
Searching for source files
Found 1 source file
0 errors, 0 warnings, 0 infos
Completed in 0.777sec

@ozars ozars force-pushed the ext-dtype-hints branch from 16bcd8b to d84e1c1 Compare April 22, 2021 12:34
@ozars ozars force-pushed the ext-dtype-hints branch from d84e1c1 to 1be9f58 Compare April 22, 2021 13:30
@ozars
Copy link
Contributor Author

ozars commented Apr 22, 2021

pyright was yelling at me that return dtype_type.construct_from_string(dtype) actually returns ExtensionDtype from find. I tried fixing it while I was at it but reverted back later since it broke things elsewhere with mypy. This part should be irrelevant to the rest.

def find(self, dtype: type[ExtensionDtype] | str) -> type[ExtensionDtype] | None:

This should be ready for review if it passes CI. This is a very minor change but it helps removing # type: ignore in some cases with extension dtypes in the user code, keeping type checker and autocompleter happy.

@jreback jreback added the Typing type annotations, mypy/pyright type checking label Apr 22, 2021
@jreback
Copy link
Contributor

jreback commented Apr 22, 2021

cc @simonjayhawkins @Dr-Irv

@simonjayhawkins
Copy link
Member

lgtm @Dr-Irv register_extension_dtype was not giving pyright warnings in #39501?

@Dr-Irv
Copy link
Contributor

Dr-Irv commented Apr 24, 2021

lgtm @Dr-Irv register_extension_dtype was not giving pyright warnings in #39501?

@simonjayhawkins
I may have been using an earlier version of pyright. But I think it would be better to merge in #40421 and then see if this PR is needed, because #40421 handles many ExtensionDType things.

@simonjayhawkins simonjayhawkins added this to the 1.3 milestone Apr 26, 2021
@simonjayhawkins
Copy link
Member

#40421 has conflicts and needs a merge master anyway. let's get this one in.

@simonjayhawkins simonjayhawkins merged commit 4e26465 into pandas-dev:master Apr 26, 2021
@simonjayhawkins
Copy link
Member

Thanks @ozars small targeted PRs for adding/refining type annotations always welcome.

@ozars
Copy link
Contributor Author

ozars commented Apr 26, 2021

Thank you.

@ozars ozars deleted the ext-dtype-hints branch April 26, 2021 11:40
yeshsurya pushed a commit to yeshsurya/pandas that referenced this pull request May 6, 2021
JulianWgs pushed a commit to JulianWgs/pandas that referenced this pull request Jul 3, 2021
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.

4 participants