Skip to content

Commit 90f3252

Browse files
author
Jeff Whitaker
authored
Merge pull request Unidata#1302 from headtr1ck/typing
Add static typing support
2 parents 553cf18 + 1644caa commit 90f3252

16 files changed

Lines changed: 856 additions & 117 deletions

‎.github/stubtest-allowlist‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
netCDF4.AccessModeOptions
2+
netCDF4.CompressionLevelOptions
3+
netCDF4.CompressionOptions
4+
netCDF4.DatatypeOptions
5+
netCDF4.DimensionsOptions
6+
netCDF4.DiskFormatOptions
7+
netCDF4.EndianOptions
8+
netCDF4.FormatOptions
9+
netCDF4.QuantizeOptions
10+
netCDF4.CalendarOptions
11+
netCDF4.ellipsis
12+
netCDF4.DateTimeArray
13+
netCDF4.FiltersDict
14+
netCDF4.SzipInfo
15+
netCDF4.BloscInfo
16+
netCDF4.BoolInt
17+
netCDF4.GetSetItemKey
18+
netCDF4.T_Datatype
19+
netCDF4.T_DatatypeNC
20+
netCDF4.Dataset.__dealloc
21+
netCDF4.Dimension.__reduce_cython__
22+
netCDF4.Dimension.__setstate_cython__
23+
netCDF4.Variable.auto_complex
24+
netCDF4._netCDF4.Dataset.__dealloc
25+
netCDF4._netCDF4.Dimension.__reduce_cython__
26+
netCDF4._netCDF4.Dimension.__setstate_cython__
27+
netCDF4._netCDF4.NC_DISKLESS
28+
netCDF4._netCDF4.NC_PERSIST
29+
netCDF4._netCDF4.Variable.auto_complex
30+
netCDF4._netCDF4.__reduce_cython__
31+
netCDF4._netCDF4.__setstate_cython__
32+
netCDF4._netCDF4.__test__
33+
netCDF4.utils.bytes

‎.github/workflows/build_master.yml‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,14 @@ jobs:
4747
- name: Install python dependencies via pip
4848
run: |
4949
python -m pip install --upgrade pip
50-
pip install numpy cython cftime pytest twine wheel check-manifest mpi4py
50+
pip install numpy cython cftime pytest twine wheel check-manifest mpi4py mypy types-setuptools
5151
5252
- name: Install netcdf4-python
5353
run: |
5454
export PATH=${NETCDF_DIR}/bin:${PATH}
5555
export NETCDF_PLUGIN_DIR=${{ github.workspace }}/netcdf-c/plugins/plugindir
5656
python setup.py install
57+
5758
- name: Test
5859
run: |
5960
export PATH=${NETCDF_DIR}/bin:${PATH}
@@ -78,3 +79,9 @@ jobs:
7879
else
7980
echo "hdf5 compressed mpi test passed!"
8081
fi
82+
83+
- name: Stubtest
84+
run: |
85+
stubtest netCDF4 --allowlist .github/stubtest-allowlist --mypy-config-file=pyproject.toml
86+
mypy test
87+
mypy examples

‎.gitignore‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@ build/
22
*.pyc
33
dist/
44
*.egg-info/
5-
netCDF4/_netCDF4.c
6-
netCDF4/*.so
5+
__pycache__
6+
.mypy_cache
7+
src/netCDF4/*.c
8+
src/netCDF4/*.so
9+
src/netCDF4/*.pyd
710
include/constants.pyx
811
include/parallel_support_imports.pxi
912
netcdftime/_netcdftime.c

‎Changelog‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
version 1.7.2 (tag v1.7.2rel)
2+
===============================
3+
* add static type hints (PR #1302)
4+
15
version 1.7.1 (tag v1.7.1rel)
26
===============================
37
* include nc_complex source code from v0.2.0 tag (instead of using submodule).

‎MANIFEST.in‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ include src/netCDF4/_netCDF4.pyx
1515
exclude src/netCDF4/_netCDF4.c
1616
include src/netCDF4/utils.py
1717
include src/netCDF4/plugins/empty.txt
18+
include src/netCDF4/py.typed
19+
include src/netCDF4/*.pyi
1820
include include/netCDF4.pxi
1921
include include/mpi-compat.h
2022
include include/membuf.pyx

‎examples/bench_compress.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
ntrials = 10
1414
sys.stdout.write('reading and writing a %s by %s by %s by %s random array ..\n'%(n1dim,n2dim,n3dim,n4dim))
1515
sys.stdout.write('(average of %s trials)\n' % ntrials)
16-
array = netCDF4.utils._quantize(uniform(size=(n1dim,n2dim,n3dim,n4dim)),4)
16+
array = netCDF4.utils._quantize(uniform(size=(n1dim,n2dim,n3dim,n4dim)),4) # type: ignore
1717

1818

1919
def write_netcdf(filename,zlib=False,shuffle=False,complevel=6):

‎examples/mpi_example.py‎

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,50 @@
11
# to run: mpirun -np 4 python mpi_example.py
22
import sys
3+
from typing import Literal
34
from mpi4py import MPI
45
import numpy as np
56
from netCDF4 import Dataset
7+
8+
format: Literal[
9+
'NETCDF4',
10+
'NETCDF4_CLASSIC',
11+
'NETCDF3_CLASSIC',
12+
'NETCDF3_64BIT_OFFSET',
13+
'NETCDF3_64BIT_DATA'
14+
]
615
if len(sys.argv) == 2:
7-
format = sys.argv[1]
16+
format = sys.argv[1] # type: ignore
817
else:
918
format = 'NETCDF4_CLASSIC'
19+
1020
rank = MPI.COMM_WORLD.rank # The process ID (integer 0-3 for 4-process run)
1121
if rank == 0:
12-
print('Creating file with format {}'.format(format))
22+
print('Creating file with format {}'.format(format))
1323
nc = Dataset('parallel_test.nc', 'w', parallel=True, comm=MPI.COMM_WORLD,
14-
info=MPI.Info(),format=format)
24+
info=MPI.Info(), format=format)
1525
# below should work also - MPI_COMM_WORLD and MPI_INFO_NULL will be used.
1626
#nc = Dataset('parallel_test.nc', 'w', parallel=True)
1727
d = nc.createDimension('dim',4)
1828
v = nc.createVariable('var', np.int32, 'dim')
1929
v[rank] = rank
30+
2031
# switch to collective mode, rewrite the data.
2132
v.set_collective(True)
2233
v[rank] = rank
2334
nc.close()
35+
2436
# reopen the file read-only, check the data
2537
nc = Dataset('parallel_test.nc', parallel=True, comm=MPI.COMM_WORLD,
26-
info=MPI.Info())
38+
info=MPI.Info())
2739
assert rank==nc['var'][rank]
2840
nc.close()
41+
2942
# reopen the file in append mode, modify the data on the last rank.
3043
nc = Dataset('parallel_test.nc', 'a',parallel=True, comm=MPI.COMM_WORLD,
31-
info=MPI.Info())
44+
info=MPI.Info())
3245
if rank == 3: v[rank] = 2*rank
3346
nc.close()
47+
3448
# reopen the file read-only again, check the data.
3549
# leave out the comm and info kwargs to check that the defaults
3650
# (MPI_COMM_WORLD and MPI_INFO_NULL) work.

‎examples/test_stringarr.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from netCDF4 import Dataset, stringtochar, chartostring
22
import random, numpy
3+
from typing import Final
34

45
# test utilities for converting arrays of fixed-length strings
56
# to arrays of characters (with an extra dimension), and vice-versa.
@@ -16,7 +17,7 @@
1617

1718

1819
FILE_NAME = 'tst_stringarr.nc'
19-
FILE_FORMAT = 'NETCDF4_CLASSIC'
20+
FILE_FORMAT: Final = 'NETCDF4_CLASSIC'
2021
chars = '1234567890aabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
2122

2223
nc = Dataset(FILE_NAME,'w',format=FILE_FORMAT)
@@ -26,7 +27,6 @@
2627
nc.createDimension('nchar',nchar)
2728
v = nc.createVariable('strings','S1',('n1','n2','nchar'))
2829
for nrec in range(nrecs):
29-
data = []
3030
data = numpy.empty((n2,),'S'+repr(nchar))
3131
# fill data with random nchar character strings
3232
for n in range(n2):

‎examples/threaded_read.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
nc.close()
2929

3030
# Queue them up
31-
items = queue.Queue()
31+
items: queue.Queue = queue.Queue()
3232
for data,fname in zip(datal,fnames):
3333
items.put(fname)
3434

0 commit comments

Comments
 (0)