From c1bd0be8e80bb3a66f49da6d4cea3c2e111343dc Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Mon, 13 Nov 2023 17:26:57 +0800 Subject: [PATCH 01/17] Add mypy as a dependency --- environment.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/environment.yml b/environment.yml index 73b45304436..d43321d10f0 100644 --- a/environment.yml +++ b/environment.yml @@ -42,3 +42,5 @@ dependencies: - sphinx-design - sphinx-gallery - sphinx_rtd_theme + # Dev dependencies (type hints) + - mypy From 594e07ecbdd82e58302072b089d8d70965a8b43d Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Mon, 13 Nov 2023 17:44:47 +0800 Subject: [PATCH 02/17] Add a make target for static type check --- Makefile | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Makefile b/Makefile index 3fcab6575ad..4811c7d4960 100644 --- a/Makefile +++ b/Makefile @@ -19,6 +19,7 @@ help: @echo " check run code style and quality checks (black, blackdoc, docformatter, ruff)" @echo " codespell run codespell to check common misspellings" @echo " lint run pylint for a deeper (and slower) quality check" + @echo " typecheck run Mypy for static type check" @echo " clean clean up build and generated files" @echo " distclean clean up build and generated files, including project metadata files" @echo "" @@ -77,6 +78,9 @@ codespell: lint: pylint $(LINT_FILES) +typecheck: + mypy ${PROJECT} + clean: find . -name "*.pyc" -exec rm -v {} + find . -name "*~" -exec rm -v {} + From 16da12ba8bfdb28648314317b7094ab381e8903e Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Mon, 13 Nov 2023 17:45:19 +0800 Subject: [PATCH 03/17] Run the static type check in the Style Checks workflow --- .github/workflows/style_checks.yaml | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/.github/workflows/style_checks.yaml b/.github/workflows/style_checks.yaml index a286b7c85b5..6b1749e9511 100644 --- a/.github/workflows/style_checks.yaml +++ b/.github/workflows/style_checks.yaml @@ -34,7 +34,7 @@ jobs: - name: Install packages run: | - python -m pip install black blackdoc docformatter pylint ruff + python -m pip install black blackdoc docformatter pylint ruff mypy python -m pip list sudo apt-get install dos2unix @@ -44,6 +44,9 @@ jobs: - name: Linting (pylint) run: make lint + - name: Static type check + run: make typecheck + - name: Ensure files use UNIX line breaks and have 644 permission run: | find . -type f -not -path '*/\.git/*' -exec grep -Iq . {} \; -exec dos2unix --quiet {} \; From fec7e8b5e6e00aeece9f7fe37bdeb9f0aa662b5f Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Mon, 13 Nov 2023 20:27:25 +0800 Subject: [PATCH 04/17] Ignore all tests --- pyproject.toml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/pyproject.toml b/pyproject.toml index 9414f2a557b..17981635d99 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -86,6 +86,9 @@ make-summary-multi-line = true wrap-summaries = 79 wrap-descriptions = 79 +[tool.mypy] +exclude = ["pygmt/tests/"] + [tool.ruff] line-length = 88 # E501 (line-too-long) show-source = true From ebe7d5035bed413d7507281e7e508db907d3da82 Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Mon, 13 Nov 2023 22:43:14 +0800 Subject: [PATCH 05/17] Add pandas-stub --- .github/workflows/style_checks.yaml | 3 ++- environment.yml | 1 + 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/.github/workflows/style_checks.yaml b/.github/workflows/style_checks.yaml index 6b1749e9511..7c78ca02003 100644 --- a/.github/workflows/style_checks.yaml +++ b/.github/workflows/style_checks.yaml @@ -34,7 +34,8 @@ jobs: - name: Install packages run: | - python -m pip install black blackdoc docformatter pylint ruff mypy + python -m pip install black blackdoc docformatter pylint ruff \ + mypy pandas-stubs python -m pip list sudo apt-get install dos2unix diff --git a/environment.yml b/environment.yml index d43321d10f0..acba7bdad71 100644 --- a/environment.yml +++ b/environment.yml @@ -44,3 +44,4 @@ dependencies: - sphinx_rtd_theme # Dev dependencies (type hints) - mypy + - pandas-stubs From 85e712869f9125cd31874b741772ec3e716be415 Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Tue, 14 Nov 2023 12:04:12 +0800 Subject: [PATCH 06/17] Install more packages --- .github/workflows/style_checks.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/style_checks.yaml b/.github/workflows/style_checks.yaml index 7c78ca02003..b0da45292b8 100644 --- a/.github/workflows/style_checks.yaml +++ b/.github/workflows/style_checks.yaml @@ -35,7 +35,7 @@ jobs: - name: Install packages run: | python -m pip install black blackdoc docformatter pylint ruff \ - mypy pandas-stubs + mypy pandas-stubs xarray geopandas fiona contextily rioxarray IPython pytest matplotlib sphinx-gallery python -m pip list sudo apt-get install dos2unix From 2310e7e3602ff5761d46b0e23539a9517a3df03e Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Tue, 14 Nov 2023 14:40:28 +0800 Subject: [PATCH 07/17] GMTRemoteDataset's units can be None --- pygmt/datasets/load_remote_dataset.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pygmt/datasets/load_remote_dataset.py b/pygmt/datasets/load_remote_dataset.py index af3e4f13912..1fe3caa9157 100644 --- a/pygmt/datasets/load_remote_dataset.py +++ b/pygmt/datasets/load_remote_dataset.py @@ -1,7 +1,7 @@ """ Internal function to load GMT remote datasets. """ -from typing import NamedTuple +from typing import NamedTuple, Union from pygmt.exceptions import GMTInvalidInput from pygmt.helpers import kwargs_to_strings @@ -58,7 +58,7 @@ class GMTRemoteDataset(NamedTuple): title: str name: str long_name: str - units: str + units: Union[str, None] resolutions: dict[str, Resolution] extra_attributes: dict From 9dc8c98bd4a162c49309d5b82ebc9ba1c339872e Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Tue, 14 Nov 2023 14:47:55 +0800 Subject: [PATCH 08/17] Remove modules that don't support type hints --- .github/workflows/style_checks.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/style_checks.yaml b/.github/workflows/style_checks.yaml index b0da45292b8..2c2bcf95115 100644 --- a/.github/workflows/style_checks.yaml +++ b/.github/workflows/style_checks.yaml @@ -35,7 +35,7 @@ jobs: - name: Install packages run: | python -m pip install black blackdoc docformatter pylint ruff \ - mypy pandas-stubs xarray geopandas fiona contextily rioxarray IPython pytest matplotlib sphinx-gallery + mypy IPython matplotlib pandas-stubs pytest rioxarray xarray python -m pip list sudo apt-get install dos2unix From dda26d49c8147496132e825d665879f17e558b61 Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Tue, 14 Nov 2023 14:53:23 +0800 Subject: [PATCH 09/17] Set ignore_missing_imports to true --- pyproject.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/pyproject.toml b/pyproject.toml index 17981635d99..fa0e13f2bf2 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -88,6 +88,7 @@ wrap-descriptions = 79 [tool.mypy] exclude = ["pygmt/tests/"] +ignore_missing_imports = true [tool.ruff] line-length = 88 # E501 (line-too-long) From 97d3dbc6ec5fe403910455b9c0caca7e63e24fa9 Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Wed, 15 Nov 2023 22:48:12 +0800 Subject: [PATCH 10/17] Ignore the 'Unsupported class scoped import [misc]' error --- pygmt/figure.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pygmt/figure.py b/pygmt/figure.py index ade6a31e35e..a7b47223909 100644 --- a/pygmt/figure.py +++ b/pygmt/figure.py @@ -518,7 +518,7 @@ def _repr_html_(self): html = '' 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 basemap, coast, colorbar, From 8b2a5b94092dcebb5991003bd467390814e90e00 Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Thu, 16 Nov 2023 10:49:20 +0800 Subject: [PATCH 11/17] Add a new workflow for static type checking --- .github/workflows/style_checks.yaml | 6 +--- .github/workflows/typecheck.yml | 44 +++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 5 deletions(-) create mode 100644 .github/workflows/typecheck.yml diff --git a/.github/workflows/style_checks.yaml b/.github/workflows/style_checks.yaml index 2c2bcf95115..a286b7c85b5 100644 --- a/.github/workflows/style_checks.yaml +++ b/.github/workflows/style_checks.yaml @@ -34,8 +34,7 @@ jobs: - name: Install packages run: | - python -m pip install black blackdoc docformatter pylint ruff \ - mypy IPython matplotlib pandas-stubs pytest rioxarray xarray + python -m pip install black blackdoc docformatter pylint ruff python -m pip list sudo apt-get install dos2unix @@ -45,9 +44,6 @@ jobs: - name: Linting (pylint) run: make lint - - name: Static type check - run: make typecheck - - name: Ensure files use UNIX line breaks and have 644 permission run: | find . -type f -not -path '*/\.git/*' -exec grep -Iq . {} \; -exec dos2unix --quiet {} \; diff --git a/.github/workflows/typecheck.yml b/.github/workflows/typecheck.yml new file mode 100644 index 00000000000..5c90635460e --- /dev/null +++ b/.github/workflows/typecheck.yml @@ -0,0 +1,44 @@ +# Static type check +# +# This workflow runs static type check. +# +# It is run on every commit to the main and pull request branches. It is also +# scheduled to run daily on the main branch. +# +name: Static Type Check + +on: + push: + branches: [ main ] + pull_request: + # Schedule daily tests + schedule: + - cron: '0 0 * * *' + +jobs: + static_check: + name: Static Type Check + runs-on: ubuntu-latest + + steps: + # Checkout current git repository + - name: Checkout + uses: actions/checkout@v4.1.1 + + # Setup Python + - name: Set up Python + uses: actions/setup-python@v4.7.1 + with: + python-version: '3.12' + + - name: Install packages + run: | + python -m pip install \ + numpy pandas xarray netcdf4 packaging \ + contextily geopandas ipython rioxarray \ + mypy pandas-stubs \ + matplotlib pytest + python -m pip list + + - name: Static type check + run: make typecheck From 9fd5a0428954ebd810c00b5fc48a3c7eec1b44fe Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Thu, 16 Nov 2023 16:15:03 +0800 Subject: [PATCH 12/17] Add comments to typecheck workflow --- .github/workflows/typecheck.yml | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/.github/workflows/typecheck.yml b/.github/workflows/typecheck.yml index 5c90635460e..9f103a13478 100644 --- a/.github/workflows/typecheck.yml +++ b/.github/workflows/typecheck.yml @@ -15,6 +15,10 @@ on: schedule: - cron: '0 0 * * *' +concurrency: + group: ${{ github.workflow }}-${{ github.ref }} + cancel-in-progress: ${{ github.ref != 'refs/heads/main' }} + jobs: static_check: name: Static Type Check @@ -33,6 +37,11 @@ jobs: - name: Install packages run: | + # 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 \ From e62dcb637bbde43e4b617a539da05ae6fb89be4d Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Sat, 18 Nov 2023 23:13:29 +0800 Subject: [PATCH 13/17] Ignore .mypy_cache and run 'make clean' to delete it --- .gitignore | 1 + Makefile | 1 + 2 files changed, 2 insertions(+) diff --git a/.gitignore b/.gitignore index c89f876a3ca..7d46956d921 100644 --- a/.gitignore +++ b/.gitignore @@ -17,6 +17,7 @@ MANIFEST .coverage coverage.xml htmlcov/ +.mypy_cache/ .pytest_cache/ .ruff_cache/ results/ diff --git a/Makefile b/Makefile index 4d435043aed..c56ac45eb6c 100644 --- a/Makefile +++ b/Makefile @@ -88,6 +88,7 @@ clean: find . -name "*~" -exec rm -v {} + find . -type d -name "__pycache__" -exec rm -rv {} + rm -rvf build dist .eggs MANIFEST .coverage .cache .pytest_cache htmlcov coverage.xml + rm -rvf .mypy_cache rm -rvf $(TESTDIR) rm -rvf baseline rm -rvf result_images From a7545addf6c24cba72d34edb4ebca1ffa930d28c Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Mon, 27 Nov 2023 13:14:54 +0800 Subject: [PATCH 14/17] Apply suggestions from code review Co-authored-by: Wei Ji <23487320+weiji14@users.noreply.github.com> --- .github/workflows/typecheck.yml | 2 +- Makefile | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/typecheck.yml b/.github/workflows/typecheck.yml index 9f103a13478..c79ab33b5b2 100644 --- a/.github/workflows/typecheck.yml +++ b/.github/workflows/typecheck.yml @@ -1,6 +1,6 @@ # Static type check # -# This workflow runs static type check. +# This workflow runs static type checks using mypy. # # It is run on every commit to the main and pull request branches. It is also # scheduled to run daily on the main branch. diff --git a/Makefile b/Makefile index 5947e5c3239..8e73587bea6 100644 --- a/Makefile +++ b/Makefile @@ -18,7 +18,7 @@ help: @echo " format run blackdoc, docformatter and ruff to automatically format the code" @echo " check run code style and quality checks (blackdoc, docformatter, ruff)" @echo " codespell run codespell to check common misspellings" - @echo " typecheck run Mypy for static type check" + @echo " typecheck run mypy for static type check" @echo " clean clean up build and generated files" @echo " distclean clean up build and generated files, including project metadata files" @echo "" From 49e67d5b02ef5eaa44b4f3aba10d6da83686938b Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Mon, 27 Nov 2023 13:15:36 +0800 Subject: [PATCH 15/17] Rename typecheck.yml -> type_checks.ym --- .github/workflows/{typecheck.yml => type_checks.yml} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename .github/workflows/{typecheck.yml => type_checks.yml} (100%) diff --git a/.github/workflows/typecheck.yml b/.github/workflows/type_checks.yml similarity index 100% rename from .github/workflows/typecheck.yml rename to .github/workflows/type_checks.yml From 567bfb821a9069968139d2feb0eedf1ab35dc56b Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Mon, 27 Nov 2023 19:42:14 +0800 Subject: [PATCH 16/17] Fix some typos --- .github/workflows/type_checks.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/type_checks.yml b/.github/workflows/type_checks.yml index c79ab33b5b2..f8f7c0e8304 100644 --- a/.github/workflows/type_checks.yml +++ b/.github/workflows/type_checks.yml @@ -1,11 +1,11 @@ -# Static type check +# Static type checks # # This workflow runs static type checks using mypy. # # It is run on every commit to the main and pull request branches. It is also # scheduled to run daily on the main branch. # -name: Static Type Check +name: Static Type Checks on: push: @@ -40,7 +40,7 @@ jobs: # Need to install four groups of packages: # 1. required packages # 2. optional packages - # 3. type checker and stub package + # 3. type checker and stub packages # 4. other packages that are used somewhere in PyGMT python -m pip install \ numpy pandas xarray netcdf4 packaging \ From f230b0d9e03e0df3b0233767fa178adc14ad4ae7 Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Fri, 1 Dec 2023 16:16:30 +0800 Subject: [PATCH 17/17] Ignore a specific error code --- pygmt/figure.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pygmt/figure.py b/pygmt/figure.py index 80e4863ff9f..5491e8d0b0d 100644 --- a/pygmt/figure.py +++ b/pygmt/figure.py @@ -514,7 +514,7 @@ def _repr_html_(self): html = '' return html.format(image=base64_png.decode("utf-8"), width=500) - from pygmt.src import ( # type: ignore + from pygmt.src import ( # type: ignore [misc] basemap, coast, colorbar,