hgrecco / pint-pandas

Pandas support for pint
Other
166 stars 40 forks source link

Enable mypy in pre-commit #215

Closed MichaelTiemannOSC closed 5 months ago

MichaelTiemannOSC commented 5 months ago

Initial attempt to add mypy to pre-commit actions.

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

MichaelTiemannOSC commented 5 months ago

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 commented 5 months ago

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 commented 5 months ago

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

MichaelTiemannOSC commented 5 months ago

xref https://github.com/pandas-dev/pandas-stubs/issues/850

MichaelTiemannOSC commented 5 months ago

@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 commented 5 months ago

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