-
Notifications
You must be signed in to change notification settings - Fork 224
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
Add Mypy for static type checking #2808
Conversation
6165a1a
to
85e7128
Compare
e0698d4
to
2355292
Compare
2355292
to
dda26d4
Compare
pygmt/figure.py
Outdated
@@ -518,7 +518,7 @@ def _repr_html_(self): | |||
html = '<img src="data:image/png;base64,{image}" width="{width}px">' | |||
return html.format(image=base64_png.decode("utf-8"), width=500) | |||
|
|||
from pygmt.src import ( # pylint: disable=import-outside-toplevel | |||
from pygmt.src import ( # type: ignore # pylint: disable=import-outside-toplevel |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To suppress the following error:
pygmt/figure.py:521: error: Unsupported class scoped import [misc]
@@ -86,6 +86,10 @@ make-summary-multi-line = true | |||
wrap-summaries = 79 | |||
wrap-descriptions = 79 | |||
|
|||
[tool.mypy] | |||
exclude = ["pygmt/tests/"] | |||
ignore_missing_imports = true |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some packages like geopandas don't provide type hints and mypy reports following errors:
Cannot find implementation or library stub for module named "geopandas" [import-not-found]
Ignore these errors following https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports.
@@ -58,7 +58,7 @@ class GMTRemoteDataset(NamedTuple): | |||
title: str | |||
name: str | |||
long_name: str | |||
units: str | |||
units: Union[str, None] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fix the following error:
pygmt/datasets/load_remote_dataset.py:150: error: Argument "units" to "GMTRemoteDataset" has incompatible type "None"; expected "str" [arg-type]
Ping @GenericMappingTools/pygmt-maintainers for review |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are a couple of errors on the type check CI at https://github.com/GenericMappingTools/pygmt/actions/runs/6987432529/job/19013968423?pr=2808#step:5:12:
mypy pygmt
pygmt/src/tilemap.py:11: error: Incompatible types in assignment (expression has type "None", variable has type Module) [assignment]
pygmt/figure.py:12: error: Incompatible types in assignment (expression has type "None", variable has type Module) [assignment]
Found 2 errors in 2 files (checked 87 source files)
make: *** [Makefile:78: typecheck] Error 1
Can those be fixed?
They will be fixed in #2809 |
.github/workflows/typecheck.yml
Outdated
# Need to install four groups of packages: | ||
# 1. required packages | ||
# 2. optional packages | ||
# 3. type checker and stub package | ||
# 4. other packages that are used somewhere in PyGMT | ||
python -m pip install \ | ||
numpy pandas xarray netcdf4 packaging \ | ||
contextily geopandas ipython rioxarray \ | ||
mypy pandas-stubs \ | ||
matplotlib pytest | ||
python -m pip list |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Haven't used this before, but could we use mypy --install-types ...
following https://github.com/pydata/xarray/blob/f7edd1e8c2d6d1a8668cc8baba9e1c66d87b8bb4/.github/workflows/ci-additional.yaml#L124C14-L124C14 instead of listing all these dependencies?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It doesn't work. The doc says:
This flag causes mypy to install known missing stub packages for third-party libraries using pip.
So it only works for pandas which maintains a separate stub package pandas-stubs
and can't install packages like numpy.
I tried to create a fresh environment and run
$ conda create --name test python=3.12 mypy
$ conda activate test
$ mypy pygmt
pygmt/io.py:4: error: Cannot find implementation or library stub for module named "xarray" [import-not-found]
pygmt/datasets/tile_map.py:7: error: Cannot find implementation or library stub for module named "contextily" [import-not-found]
pygmt/datasets/tile_map.py:11: error: Cannot find implementation or library stub for module named "numpy" [import-not-found]
...
# A lot of similar errors
Installing missing stub packages: python -m pip install pandas-stubs
Install? [yN]
Co-authored-by: Wei Ji <23487320+weiji14@users.noreply.github.com>
Looking at astral-sh/ruff#3893, there's a Rust-based type checker at https://github.com/mtshiba/pylyzer. Should we consider that too? |
Mypy is the de facto static type checker for Python, so it makes sense to start type hints with Mypy. We may consider switching to other tools like pyright or pylyzer later. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just one tiny suggestion, otherwise merge #2809 first before this one.
# Dev dependencies (type hints) | ||
- mypy | ||
- pandas-stubs |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe move this under the # Dev dependencies (style checks)
section?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I prefer to keep them in a separate section.
Description of proposed changes
References:
Part of #2794
Reminders
make format
andmake check
to make sure the code follows the style guide.doc/api/index.rst
.Slash Commands
You can write slash commands (
/command
) in the first line of a comment to performspecific operations. Supported slash commands are:
/format
: automatically format and lint the code/test-gmt-dev
: run full tests on the latest GMT development version