pacificclimate / climpyrical

A Python tool for constraining and manipulating CanRCM4 climate models.
GNU General Public License v3.0
1 stars 0 forks source link

Enforced checking with mypy #29

Open nannau opened 4 years ago

nannau commented 4 years ago

Type hints are now included in every function in climpyrical, but enforcing them with mypy is less straightforward. mypy requires certain third-party criteria to be met. Since climpyrical often uses non-standard object types (numpy, xarray, geopandas, shapely, etc...), some effort will need to be made to get mypy to recognize and enforce these type hints. See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-type-hints-for-third-party-library for more information.

To reproduce problems, run pytest with the mypy extension: pytest --mypy climpyrical/tests

Gives confusing results:

$ pytest --mypy climpyrical/tests/
============================================= test session starts =============================================
platform linux -- Python 3.6.7, pytest-6.1.0, py-1.9.0, pluggy-0.13.0
rootdir: /home/nannau/09092020/climpyrical, configfile: pytest.ini
plugins: dash-1.16.2, cov-2.8.1, mypy-0.7.0
collected 110 items                                                                                           

climpyrical/tests/__init__.py .                                                                         [  0%]
climpyrical/tests/test_data.py .....................                                                    [ 19%]
climpyrical/tests/test_gridding.py ...........................................................          [ 72%]
climpyrical/tests/test_mask.py .................                                                        [ 88%]
climpyrical/tests/test_spytial.py ............                                                          [ 99%]
. F                                                                                                     [100%]

================================================== FAILURES ===================================================
________________________________________________ test session _________________________________________________
mypy exited with status 2.
============================================== warnings summary ===============================================
../../tri/lib/python3.6/site-packages/xarray/core/nanops.py:142
  /home/nannau/tri/lib/python3.6/site-packages/xarray/core/nanops.py:142: RuntimeWarning:

  Mean of empty slice

climpyrical/tests/test_gridding.py::test_transform_coords[x0-y0-source_crs0-target_crs0-expected_tuple0]
climpyrical/tests/test_gridding.py::test_transform_coords[x1-y1-source_crs1-target_crs1-expected_tuple1]
  /home/nannau/tri/lib/python3.6/site-packages/pyproj/crs.py:122: DeprecationWarning:

  '+init=<authority>:<code>' syntax is deprecated. '<authority>:<code>' is the preferred initialization method.

climpyrical/tests/test_mask.py::test_check_polygon_before_projection[p0-None]
climpyrical/tests/test_mask.py::test_rotate_shapefile[p0-crs0-expected0]
  /home/nannau/09092020/climpyrical/climpyrical/mask.py:49: UserWarning:

  Polygon provided is in projection epsg:4326, expected {'init': 'epsg:4326'} or epsg:4326.                 Other transformations are experimental and have not been tested.

climpyrical/tests/test_mask.py::test_gen_raster_mask_from_vector[x1-y1-p1-True-ValueError]
  /home/nannau/09092020/climpyrical/climpyrical/gridding.py:370: UserWarning:

  -21.329623647819652 is outside of array's domain between -34.27600493988434 and -33.87770404322978.             A station is outside of the CanRCM4 model grid space.

climpyrical/tests/test_mask.py::test_gen_raster_mask_from_vector[x1-y1-p1-True-ValueError]
  /home/nannau/09092020/climpyrical/climpyrical/gridding.py:370: UserWarning:

  28.690943868759113 is outside of array's domain between -34.27600493988434 and -33.87770404322978.             A station is outside of the CanRCM4 model grid space.

climpyrical/tests/test_mask.py::test_gen_raster_mask_from_vector[x1-y1-p1-True-ValueError]
  /home/nannau/09092020/climpyrical/climpyrical/gridding.py:370: UserWarning:

  -4.841651200465685 is outside of array's domain between -28.99599652844806 and -28.59725291807725.             A station is outside of the CanRCM4 model grid space.

climpyrical/tests/test_mask.py::test_gen_raster_mask_from_vector[x1-y1-p1-True-ValueError]
  /home/nannau/09092020/climpyrical/climpyrical/gridding.py:370: UserWarning:

  36.4599884172838 is outside of array's domain between -28.99599652844806 and -28.59725291807725.             A station is outside of the CanRCM4 model grid space.

-- Docs: https://docs.pytest.org/en/stable/warnings.html
=========================================== short test summary info ===========================================
FAILED ::mypy
================================= 1 failed, 110 passed, 9 warnings in 43.26s ==================================
R[write to console]: Warning messages:

R[write to console]: 1: 
R[write to console]: In (function (package, help, pos = 2, lib.loc = NULL, character.only = FALSE,  :
R[write to console]: 

R[write to console]:  libraries ‘/usr/local/lib/R/site-library’, ‘/usr/lib/R/site-library’ contain no packages

R[write to console]: 2: 
R[write to console]: In (function (package, help, pos = 2, lib.loc = NULL, character.only = FALSE,  :
R[write to console]: 

R[write to console]:  libraries ‘/usr/local/lib/R/site-library’, ‘/usr/lib/R/site-library’ contain no packages

Note with mypy.ini config file set as:

[mypy]
ignore_missing_imports = False
strict_optional = False
jameshiebert commented 4 years ago

Yeah, the output is not clear as to what is a test failure and what are warnings. It says that mypy is the failure:

=========================================== short test summary info ===========================================
FAILED ::mypy

But then all of the actionable mypyoutput is in the warning section (and warnings shouldn't affect the test outcome).

Do you get clearer output if you run mypy directly. Or do we tune to tune the configuration to omit warnings that we're not concerned about (e.g. upstream packages)?

nannau commented 4 years ago

Good idea, it does tell us to use mypy from master on GH... (?)

climpyrical/mask.py:7: error: Skipping analyzing 'nptyping': found module but no type hints or library stubs
climpyrical/mask.py:7: note: See https://mypy.readthedocs.io/en/latest/running_mypy.html#missing-imports
climpyrical/mask.py:9: error: Skipping analyzing 'shapely.geometry': found module but no type hints or library stubs
climpyrical/mask.py:10: error: Skipping analyzing 'tqdm': found module but no type hints or library stubs
climpyrical/mask.py:11: error: Skipping analyzing 'geopandas': found module but no type hints or library stubs
climpyrical/gridding.py:6: error: Skipping analyzing 'scipy.interpolate': found module but no type hints or library stubs
climpyrical/gridding.py:7: error: Skipping analyzing 'pyproj': found module but no type hints or library stubs
climpyrical/gridding.py:8: error: Skipping analyzing 'nptyping': found module but no type hints or library stubs
climpyrical/data.py:3: error: Skipping analyzing 'nptyping': found module but no type hints or library stubs
climpyrical/data.py:6: error: Skipping analyzing 'scipy.interpolate': found module but no type hints or library stubs
/home/nannau/tri/lib/python3.6/site-packages/xarray/core/dataarray.py:1906: error: INTERNAL ERROR -- Please try using mypy master on Github:
https://mypy.rtfd.io/en/latest/common_issues.html#using-a-development-mypy-build
If this issue continues with mypy master, please report a bug at https://github.com/python/mypy/issues
version: 0.782
/home/nannau/tri/lib/python3.6/site-packages/xarray/core/dataarray.py:1906: : note: please use --show-traceback to print a traceback when reporting a bug

To no avail... The same error appears. It seems to be caused by xarray's dataarray.py for mypy==0.790+dev.49a8c102a11d3ec8ffc48dc0a6d5409ed60767fd (master).

$ mypy climpyrical/mask.py 
climpyrical/mask.py:7: error: Skipping analyzing 'nptyping': found module but no type hints or library stubs
climpyrical/mask.py:7: note: See https://mypy.readthedocs.io/en/latest/running_mypy.html#missing-imports
climpyrical/mask.py:9: error: Skipping analyzing 'shapely.geometry': found module but no type hints or library stubs
climpyrical/mask.py:10: error: Skipping analyzing 'tqdm': found module but no type hints or library stubs
climpyrical/mask.py:11: error: Skipping analyzing 'geopandas': found module but no type hints or library stubs
climpyrical/gridding.py:6: error: Skipping analyzing 'scipy.interpolate': found module but no type hints or library stubs
climpyrical/gridding.py:7: error: Skipping analyzing 'pyproj': found module but no type hints or library stubs
climpyrical/gridding.py:8: error: Skipping analyzing 'nptyping': found module but no type hints or library stubs
climpyrical/data.py:3: error: Skipping analyzing 'nptyping': found module but no type hints or library stubs
climpyrical/data.py:6: error: Skipping analyzing 'scipy.interpolate': found module but no type hints or library stubs
/home/nannau/tri/lib/python3.6/site-packages/xarray/core/dataarray.py:2879: error: INTERNAL ERROR -- Please try using mypy master on Github:
https://mypy.rtfd.io/en/latest/common_issues.html#using-a-development-mypy-build
If this issue continues with mypy master, please report a bug at https://github.com/python/mypy/issues
version: 0.790+dev.49a8c102a11d3ec8ffc48dc0a6d5409ed60767fd
/home/nannau/tri/lib/python3.6/site-packages/xarray/core/dataarray.py:2879: : note: please use --show-traceback to print a traceback when reporting a bug

To be clear, I'm referring to this error, not the stub related errors:

/xarray/core/dataarray.py:1906: error: INTERNAL ERROR -- Please try using mypy master on Github:
https://mypy.rtfd.io/en/latest/common_issues.html#using-a-development-mypy-build
If this issue cont