cartopy
cartopy copied to clipboard
Add type hints for mypy
Description
I would like to propose adding type hints to cartopy so that mypy can be used with the project.
Code to reproduce
Using the following code (adapted from the global map tutorial):
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
fig = plt.figure(figsize=(10, 5))
ax = fig.add_subplot(1, 1, 1, projection=ccrs.Robinson())
ax.set_global()
ax.stock_img()
ax.coastlines()
ax.plot(-0.08, 51.53, 'o', transform=ccrs.PlateCarree())
ax.plot([-0.08, 132], [51.53, 43.17], transform=ccrs.PlateCarree())
ax.plot([-0.08, 132], [51.53, 43.17], transform=ccrs.Geodetic())
plt.show()
If you run:
> mypy --strict plot.py
you'll see several errors.
Traceback
test.py:2: error: Skipping analyzing "cartopy.crs": module is installed, but missing library stubs or py.typed marker [import-untyped]
test.py:2: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
test.py:2: error: Skipping analyzing "cartopy": module is installed, but missing library stubs or py.typed marker [import-untyped]
test.py:8: error: "Axes" has no attribute "set_global" [attr-defined]
test.py:9: error: "Axes" has no attribute "stock_img" [attr-defined]
test.py:10: error: "Axes" has no attribute "coastlines" [attr-defined]
Found 5 errors in 1 file (checked 1 source file)
Full environment definition
Operating system
macOS and Linux
Cartopy version
0.22.0
conda list
N/A
pip list
Package Version
----------------------------- -----------
absl-py 1.4.0
aenum 3.1.12
affine 2.1.0
aiohttp 3.8.4
aiosignal 1.2.0
alabaster 0.7.13
altgraph 0.17.2
antlr4-python3-runtime 4.9.3
anyio 4.0.0
appdirs 1.4.4
appnope 0.1.3
argon2-cffi 21.3.0
argon2-cffi-bindings 21.2.0
arrow 1.2.3
asttokens 2.4.0
astunparse 1.6.3
async-lru 1.0.3
async-timeout 4.0.2
attrs 23.1.0
Babel 2.12.1
backcall 0.2.0
beautifulsoup4 4.12.2
black 23.11.0
bleach 6.0.0
Bottleneck 1.3.7
build 1.0.3
cachetools 5.2.0
Cartopy 0.22.0
certifi 2023.5.7
cffi 1.15.1
cftime 1.0.3.4
charset-normalizer 3.1.0
click 8.1.3
click-plugins 1.1.1
cligj 0.7.2
cmocean 3.0.3
colorama 0.4.6
comm 0.1.3
contourpy 1.0.7
coverage 7.2.6
cycler 0.11.0
debugpy 1.6.7
decorator 5.1.1
defusedxml 0.7.1
docstring-parser 0.15
docutils 0.18.1
editables 0.3
efficientnet-pytorch 0.7.1
einops 0.7.0
et-xmlfile 1.0.1
executing 1.2.0
fastjsonschema 2.16.3
filelock 3.12.4
Fiona 1.9.4
flake8 6.1.0
fonttools 4.39.4
fqdn 1.5.1
frozenlist 1.3.1
fsspec 2023.1.0
future 0.18.2
GDAL 3.8.0
geocube 0.3.2
geopandas 0.11.1
gevent 23.7.0
google-auth 2.20.0
google-auth-oauthlib 0.5.2
greenlet 2.0.2
grpcio 1.52.0
h5py 3.8.0
hatch-jupyter-builder 0.8.3
hatchling 1.18.0
huggingface-hub 0.14.1
hydra-core 1.3.1
idna 3.4
imageio 2.30.0
imagesize 1.4.1
importlib-metadata 6.6.0
importlib-resources 5.12.0
iniconfig 2.0.0
ipykernel 6.23.1
ipython 8.14.0
ipywidgets 8.0.2
isoduration 20.11.0
isort 5.12.0
jaraco.classes 3.2.3
jedi 0.18.2
Jinja2 3.0.3
joblib 1.2.0
json5 0.9.14
jsonargparse 4.25.0
jsonpointer 2.0
jsonschema 4.17.3
jupyter_client 8.2.0
jupyter_core 5.3.0
jupyter-events 0.6.3
jupyter-lsp 2.2.0
jupyter_server 2.6.0
jupyter_server_terminals 0.4.4
jupyterlab 4.0.1
jupyterlab-pygments 0.2.2
jupyterlab_server 2.22.1
jupyterlab-widgets 3.0.3
keyring 23.13.1
kiwisolver 1.4.4
kornia 0.7.0
laspy 2.2.0
lazy_loader 0.1
lightly 1.4.18
lightly-utils 0.0.2
lightning 2.1.2
lightning-utilities 0.8.0
macholib 1.15.2
Markdown 3.4.1
markdown-it-py 3.0.0
MarkupSafe 2.1.3
matplotlib 3.8.2
matplotlib-inline 0.1.6
mccabe 0.7.0
mdurl 0.1.2
mistune 2.0.5
more-itertools 9.1.0
mpmath 1.2.1
multidict 6.0.4
munch 2.5.0
mypy 1.7.0
mypy-extensions 1.0.0
nbclient 0.6.7
nbconvert 7.4.0
nbformat 5.8.0
nbmake 1.4.3
nbsphinx 0.8.8
nest-asyncio 1.5.6
netCDF4 1.6.2
networkx 3.1
notebook_shim 0.2.3
numexpr 2.8.4
numpy 1.26.2
oauthlib 3.2.1
odc-geo 0.1.2
omegaconf 2.3.0
openpyxl 3.1.2
overrides 7.3.1
packaging 23.1
pandas 2.1.3
pandocfilters 1.5.0
parso 0.8.3
pathspec 0.11.1
pexpect 4.8.0
pickleshare 0.7.5
Pillow 10.0.0
pip 21.2.4
pkginfo 1.9.6
planetary-computer 0.4.9
platformdirs 3.10.0
pluggy 1.0.0
pooch 1.7.0
pretrainedmodels 0.7.4
prometheus-client 0.17.0
prompt-toolkit 3.0.38
protobuf 3.20.3
psutil 5.9.5
ptyprocess 0.7.0
pure-eval 0.2.2
pyasn1 0.4.8
pyasn1-modules 0.2.8
pybind11 2.11.0
pycocotools 2.0.6
pycodestyle 2.11.0
pycparser 2.21
pydantic 1.10.9
pydocstyle 6.2.1
pyflakes 3.1.0
pygeos 0.14
Pygments 2.16.1
pyparsing 3.0.9
pyproj 3.2.1
pyproject_hooks 1.0.0
pyrsistent 0.19.3
pyshp 2.1.0
pystac 1.4.0
pystac-client 0.5.1
pytest 7.3.2
pytest-cov 4.0.0
python-dateutil 2.8.2
python-dotenv 0.19.2
python-json-logger 2.0.7
pytorch-lightning 2.0.0
pytorch-sphinx-theme 0.0.24
pytz 2023.3
pyupgrade 3.3.1
pyvista 0.42.3
PyWavelets 1.4.1
PyYAML 6.0
pyzmq 25.0.2
radiant-mlhub 0.3.1
rarfile 4.1
rasterio 1.3.8
readme-renderer 37.3
requests 2.31.0
requests-oauthlib 1.3.1
requests-toolbelt 1.0.0
rfc3339-validator 0.1.4
rfc3986 2.0.0
rfc3986-validator 0.1.1
rich 13.4.2
rioxarray 0.4.1.post0
rsa 4.9
Rtree 1.1.0
safetensors 0.3.1
scikit-image 0.20.0
scikit-learn 1.3.2
scipy 1.11.4
scooby 0.5.7
segmentation-models-pytorch 0.3.3
Send2Trash 1.8.0
setuptools 63.4.3
Shapely 1.8.1
six 1.16.0
sniffio 1.3.0
snowballstemmer 2.2.0
snuggs 1.4.1
soupsieve 2.4.1
Sphinx 5.3.0
sphinx-copybutton 0.2.12
sphinx_design 0.4.1
sphinx-rtd-theme 1.2.2
sphinxcontrib-applehelp 1.0.4
sphinxcontrib-devhelp 1.0.2
sphinxcontrib-htmlhelp 2.0.1
sphinxcontrib-jquery 4.1
sphinxcontrib-jsmath 1.0.1
sphinxcontrib-programoutput 0.15
sphinxcontrib-qthelp 1.0.3
sphinxcontrib-serializinghtml 1.1.9
stack-data 0.6.2
sympy 1.11.1
tensorboard 2.14.1
tensorboard-data-server 0.7.0
terminado 0.17.1
threadpoolctl 3.1.0
tifffile 2023.8.30
timm 0.9.2
tinycss2 1.2.1
tokenize-rt 4.2.1
torch 2.1.1
torchmetrics 1.2.0
torchvision 0.16.1
tornado 6.3.3
tqdm 4.66.1
traitlets 5.9.0
trove-classifiers 2023.8.7
twine 4.0.2
typeshed-client 2.1.0
typing_extensions 4.8.0
tzdata 2023.3
uri-template 1.2.0
urllib3 1.26.12
vermin 1.5.2
wcwidth 0.2.7
webcolors 1.11.1
webencodings 0.5.1
websocket-client 1.6.3
Werkzeug 3.0.0
wheel 0.41.2
widgetsnbextension 4.0.3
xarray 2023.7.0
yarl 1.9.2
zipfile-deflate64 0.2.0
zipp 3.17.0
zope.event 4.6
zope.interface 5.4.0
fig.add_subplot
is a Matplotlib method, and Matplotlib's type stubs indicate that it returns a matplotlib Axes
instance, which indeed does not have set_global
, etc. I'm not sure it's actually possible for Cartopy to tell mypy that add_subplot
will return a GeoAxes
if one of Cartopy's projections is passed.
See this related issue in Matplotlib https://github.com/matplotlib/matplotlib/issues/27455
Thanks, I can use typing.cast, but we'll still need to add type hints to cartopy in order for GeoAxes to have this method.
I'm ok with following Matplotlib's lead on this with regards to including them (.pyi
) files, but I suspect someone in the community is going to need to take the lead on creating a PR to include them.