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

Enable mypy in pre-commit #215

Merged
merged 4 commits into from
Feb 2, 2024
Merged

Enable mypy in pre-commit #215

merged 4 commits into from
Feb 2, 2024

Conversation

MichaelTiemannOSC
Copy link
Collaborator

Initial attempt to add mypy to pre-commit actions.

  • Closes PintType is not mypy-friendly #213
  • Executed pre-commit run --all-files with no errors
  • The change is fully covered by automated unit tests
  • Documented in docs/ as appropriate
  • Added an entry to the CHANGES file

This changes no user-visible functionality. It enables mypy type checking. If this fails (because pre-commit exceeds certain memory limits, I have a fix for that).

Initial attempt to add mypy to pre-commit actions.

Signed-off-by: Michael Tiemann <[email protected]>
Fix indentation error from copy-paste.

Signed-off-by: Michael Tiemann <[email protected]>
@MichaelTiemannOSC
Copy link
Collaborator Author

Checks are failing because mypy is now seeing things in pint_pandas it doesn't like:

docs/conf.py:25: error: Incompatible types in assignment (expression has type "str", variable has type "Callable[[str], str]")  [assignment]
docs/conf.py:29: error: Incompatible types in assignment (expression has type "str", variable has type "Callable[[str], str]")  [assignment]
pint_pandas/pint_array.py:20: error: Module "pandas.core" has no attribute "nanops"  [attr-defined]
pint_pandas/pint_array.py:50: error: Need type annotation for "_cache" (hint: "_cache: Dict[<type>, <type>] = ...")  [var-annotated]
pint_pandas/pint_array.py:81: error: Unsupported format character "P"  [str-format]
pint_pandas/pint_array.py:85: error: Unsupported format character "P"  [str-format]
pint_pandas/pint_array.py:196: error: Module has no attribute "PandasDtype"  [attr-defined]
pint_pandas/pint_array.py:198: error: Module has no attribute "NumpyEADtype"  [attr-defined]
pint_pandas/pint_array.py:386: error: "ndarray[Any, dtype[Any]]" has no attribute "isna"  [attr-defined]
pint_pandas/pint_array.py:627: error: Incompatible return value type (got "bool_", expected "bool")  [return-value]
pint_pandas/pint_array.py:629: error: Incompatible return value type (got "bool | bool_", expected "bool")  [return-value]
pint_pandas/pint_array.py:918: error: Cannot determine type of "result"  [has-type]
pint_pandas/pint_array.py:921: error: Cannot determine type of "result"  [has-type]
pint_pandas/pint_array.py:922: error: Cannot determine type of "result"  [has-type]
pint_pandas/pint_array.py:924: error: Cannot determine type of "result"  [has-type]
pint_pandas/pint_array.py:925: error: Cannot determine type of "result"  [has-type]
pint_pandas/pint_array.py:942: error: Cannot determine type of "result"  [has-type]
pint_pandas/pint_array.py:[118](https://github.com/hgrecco/pint-pandas/actions/runs/7513091234/job/20454462065#step:4:120)4: error: Module has no attribute "upcast_types"; maybe "is_upcast_type", "upcast_type_names", or "upcast_type_map"?  [attr-defined]
pint_pandas/pint_array.py:1188: error: "Mapping[str, type | None]" has no attribute "setdefault"  [attr-defined]
pint_pandas/pint_array.py:1189: error: "Mapping[str, type | None]" has no attribute "setdefault"  [attr-defined]
pint_pandas/__init__.py:9: error: Name "version" already defined (possibly by an import)  [no-redef]
pint_pandas/testsuite/test_pandas_extensiontests.py:384: error: "Timestamp" has no attribute "m"  [attr-defined]
pint_pandas/testsuite/test_pandas_extensiontests.py:386: error: "Timestamp" has no attribute "m"  [attr-defined]
pint_pandas/testsuite/test_pandas_extensiontests.py:395: error: "Timestamp" has no attribute "m"  [attr-defined]
pint_pandas/testsuite/test_pandas_extensiontests.py:397: error: "Timestamp" has no attribute "m"  [attr-defined]
Found 25 errors in 4 files (checked 7 source files)```

One question this raises for me is how to match up the versions of `pandas-stubs` with the Pandas library we are using.  Pint supports many versions of Pandas.  Could this be part of the problem?

@andrewgsavage
Copy link
Collaborator

it looks like pandas-stubs version is intended to match the version of pandas

https://pypi.org/project/pandas-stubs/#history
https://pypi.org/project/pandas/#history

and pandas-stubs is has a dependency of the same pandas version
pandas = "2.1.4"

so I think the versions should match up

@MichaelTiemannOSC
Copy link
Collaborator Author

Well that's good. I'll see what I can do to clean up these errors.

@MichaelTiemannOSC
Copy link
Collaborator Author

xref pandas-dev/pandas-stubs#850

Adjusted various declarations to use public Pandas APIs where possible, ignored things that cannot be fixed without fixing Pandas-Stubs, etc.

Might need some more attention for different versions of Pandas/Python, but this work for Pandas 2.1.4 and Python 3.11.

Signed-off-by: Michael Tiemann <[email protected]>
Make type hints compatible with python-3.9 (which requires `Union` and does not support the `|` operator to express type disjunction).

Signed-off-by: Michael Tiemann <[email protected]>
@MichaelTiemannOSC
Copy link
Collaborator Author

@andrewgsavage I think that as a first draft, these changes are ready. If/when pandas-stubs moves forward, we can remove a few # type: ignore messages and perhaps submit a few additional suggestions to pandas-stubs to further improve things.

@andrewgsavage
Copy link
Collaborator

very nice
Am I right in thinking this is run during the lint workflow check? which runs pre-commit
https://github.com/hgrecco/pint-pandas/blob/5f4c39de8bbf95273faf80419956349171c0aa8c/.github/workflows/lint.ym

@andrewgsavage andrewgsavage merged commit f9d3a66 into master Feb 2, 2024
58 checks passed
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.

PintType is not mypy-friendly
2 participants