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

Add Mypy for static type checking #2808

Merged
merged 28 commits into from
Dec 4, 2023
Merged

Add Mypy for static type checking #2808

merged 28 commits into from
Dec 4, 2023

Conversation

seisman
Copy link
Member

@seisman seisman commented Nov 13, 2023

Description of proposed changes

References:

  1. https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports

Part of #2794

Reminders

  • Run make format and make check to make sure the code follows the style guide.
  • Add tests for new features or tests that would have caught the bug that you're fixing.
  • Add new public functions/methods/classes to doc/api/index.rst.
  • Write detailed docstrings for all functions/methods.
  • If wrapping a new module, open a 'Wrap new GMT module' issue and submit reasonably-sized PRs.
  • If adding new functionality, add an example to docstrings or tutorials.
  • Use underscores (not hyphens) in names of Python files and directories.

Slash Commands

You can write slash commands (/command) in the first line of a comment to perform
specific operations. Supported slash commands are:

  • /format: automatically format and lint the code
  • /test-gmt-dev: run full tests on the latest GMT development version

@seisman seisman mentioned this pull request Nov 15, 2023
3 tasks
@seisman seisman added the typing Type hints and static type checking label Nov 15, 2023
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
Copy link
Member Author

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
Copy link
Member Author

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]
Copy link
Member Author

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]

@seisman seisman marked this pull request as ready for review November 16, 2023 08:19
@seisman seisman added this to the 0.11.0 milestone Nov 16, 2023
@seisman seisman added the maintenance Boring but important stuff for the core devs label Nov 16, 2023
@seisman seisman changed the title Use Mypy for static type checking Add Mypy for static type checking Nov 16, 2023
@seisman seisman added the needs review This PR has higher priority and needs review. label Nov 18, 2023
@seisman seisman requested a review from a team November 22, 2023 10:44
@seisman
Copy link
Member Author

seisman commented Nov 26, 2023

Ping @GenericMappingTools/pygmt-maintainers for review

Copy link
Member

@weiji14 weiji14 left a 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?

@seisman
Copy link
Member Author

seisman commented Nov 27, 2023

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 Show resolved Hide resolved
.github/workflows/typecheck.yml Outdated Show resolved Hide resolved
Makefile Outdated Show resolved Hide resolved
Comment on lines 40 to 50
# 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
Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Member Author

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]

@seisman seisman requested a review from weiji14 December 1, 2023 06:57
@weiji14
Copy link
Member

weiji14 commented Dec 1, 2023

Looking at astral-sh/ruff#3893, there's a Rust-based type checker at https://github.com/mtshiba/pylyzer. Should we consider that too?

@seisman
Copy link
Member Author

seisman commented Dec 2, 2023

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.

Copy link
Member

@weiji14 weiji14 left a 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.

Comment on lines +44 to +46
# Dev dependencies (type hints)
- mypy
- pandas-stubs
Copy link
Member

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?

Copy link
Member Author

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.

@weiji14 weiji14 added final review call This PR requires final review and approval from a second reviewer and removed needs review This PR has higher priority and needs review. labels Dec 4, 2023
@seisman seisman merged commit 716f622 into main Dec 4, 2023
17 checks passed
@seisman seisman deleted the typehints/mypy branch December 4, 2023 09:11
@seisman seisman removed the final review call This PR requires final review and approval from a second reviewer label Dec 4, 2023
This pull request was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
maintenance Boring but important stuff for the core devs typing Type hints and static type checking
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants