Skip to content

Commit 14012a5

Browse files
author
Jeff Whitaker
authored
Merge pull request Unidata#1349 from RandallPittmanOrSt/rwp_typing
Corrections and enchancements to type stubs
2 parents d181813 + 1004467 commit 14012a5

35 files changed

+593
-405
lines changed

.github/stubtest-allowlist

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,42 @@
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
1+
netCDF4.RealTypeLiteral
2+
netCDF4.ComplexTypeLiteral
3+
netCDF4.NumericTypeLiteral
4+
netCDF4.CharTypeLiteral
5+
netCDF4.TypeLiteral
6+
netCDF4.NumPyRealType
7+
netCDF4.NumPyComplexType
8+
netCDF4.NumPyNumericType
9+
netCDF4.NetCDFUDTClass
10+
netCDF4.AccessMode
11+
netCDF4.CompressionLevel
12+
netCDF4.CompressionType
13+
netCDF4.DatatypeType
14+
netCDF4.DimensionsType
15+
netCDF4.DiskFormat
16+
netCDF4.EndianType
17+
netCDF4.Format
18+
netCDF4.QuantizeMode
19+
netCDF4.CalendarType
1220
netCDF4.DateTimeArray
1321
netCDF4.FiltersDict
1422
netCDF4.SzipInfo
1523
netCDF4.BloscInfo
1624
netCDF4.BoolInt
17-
netCDF4.GetSetItemKey
18-
netCDF4.T_Datatype
19-
netCDF4.T_DatatypeNC
20-
netCDF4.Dataset.__dealloc
25+
netCDF4.VarT
26+
netCDF4.RealVarT
27+
netCDF4.ComplexVarT
28+
netCDF4.NumericVarT
2129
netCDF4.Dimension.__reduce_cython__
2230
netCDF4.Dimension.__setstate_cython__
2331
netCDF4.Variable.auto_complex
24-
netCDF4._netCDF4.Dataset.__dealloc
32+
netCDF4.Variable.__iter__
2533
netCDF4._netCDF4.Dimension.__reduce_cython__
2634
netCDF4._netCDF4.Dimension.__setstate_cython__
2735
netCDF4._netCDF4.NC_DISKLESS
2836
netCDF4._netCDF4.NC_PERSIST
2937
netCDF4._netCDF4.Variable.auto_complex
38+
netCDF4._netCDF4.Variable.__iter__
3039
netCDF4._netCDF4.__reduce_cython__
3140
netCDF4._netCDF4.__setstate_cython__
3241
netCDF4._netCDF4.__test__
33-
netCDF4.utils.bytes
42+
netCDF4.utils

.github/workflows/build_latest.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ jobs:
5858
- name: Install python dependencies via pip
5959
run: |
6060
python -m pip install --upgrade pip
61-
pip install numpy cython cftime pytest twine wheel check-manifest mpi4py
61+
pip install numpy cython cftime pytest twine wheel check-manifest mpi4py typing-extensions
6262
6363
- name: Install netcdf4-python
6464
run: |

.github/workflows/build_master.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ 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 mypy types-setuptools
50+
pip install numpy cython cftime pytest twine wheel check-manifest mpi4py mypy types-setuptools typing-extensions
5151
5252
- name: Install netcdf4-python
5353
run: |

.github/workflows/build_old.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ jobs:
5959
- name: Install python dependencies via pip
6060
run: |
6161
python -m pip install --upgrade pip
62-
pip install numpy cython cftime pytest twine wheel check-manifest mpi4py
62+
pip install numpy cython cftime pytest twine wheel check-manifest mpi4py typing-extensions
6363
6464
- name: Install netcdf4-python
6565
run: |

.github/workflows/cibuildwheel.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ jobs:
103103
CIBW_TEST_SKIP: "cp38-*_aarch64 cp39-*_aarch64 cp310-*_aarch64 cp311-*_aarch64"
104104
CIBW_ENVIRONMENT: ${{ matrix.CIBW_ENVIRONMENT }}
105105
CIBW_BEFORE_BUILD_MACOS: brew install hdf5 netcdf
106-
CIBW_TEST_REQUIRES: pytest cython packaging
106+
CIBW_TEST_REQUIRES: pytest cython packaging typing-extensions
107107
CIBW_TEST_COMMAND: >
108108
python -c "import netCDF4; print(f'netCDF4 v{netCDF4.__version__}')"
109109
&& pytest -s -rxs -v {project}/test
@@ -155,7 +155,7 @@ jobs:
155155
CIBW_REPAIR_WHEEL_COMMAND_WINDOWS: >
156156
delvewheel show {wheel}
157157
&& delvewheel repair -w {dest_dir} {wheel}
158-
CIBW_TEST_REQUIRES: pytest cython packaging
158+
CIBW_TEST_REQUIRES: pytest cython packaging typing-extensions
159159
CIBW_TEST_COMMAND: >
160160
python -c "import netCDF4; print(f'netCDF4 v{netCDF4.__version__}')"
161161
&& pytest -s -rxs -v {project}\\test

.github/workflows/miniconda.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ jobs:
1616
os: [windows-latest, ubuntu-latest, macos-latest]
1717
platform: [x64, x32]
1818
exclude:
19-
- os: macos-latest
19+
- os: macos-latest
2020
platform: x32
2121
fail-fast: false
2222
defaults:
@@ -35,7 +35,7 @@ jobs:
3535
init-shell: bash
3636
create-args: >-
3737
python=${{ matrix.python-version }}
38-
numpy cython pip pytest hdf5 libnetcdf cftime zlib certifi
38+
numpy cython pip pytest hdf5 libnetcdf cftime zlib certifi typing-extensions
3939
--channel conda-forge
4040
4141
- name: Install netcdf4-python
@@ -69,7 +69,7 @@ jobs:
6969
init-shell: bash
7070
create-args: >-
7171
python=${{ matrix.python-version }}
72-
numpy cython pip pytest openmpi mpi4py hdf5=*=mpi* libnetcdf=*=mpi* cftime zlib certifi
72+
numpy cython pip pytest openmpi mpi4py hdf5=*=mpi* libnetcdf=*=mpi* cftime zlib certifi typing-extensions
7373
--channel conda-forge
7474
7575
- name: Install netcdf4-python with mpi
@@ -82,7 +82,7 @@ jobs:
8282
run: |
8383
cd test && python run_all.py
8484
cd ../examples
85-
export PATH="${CONDA_PREFIX}/bin:${CONDA_PREFIX}/Library/bin:$PATH"
85+
export PATH="${CONDA_PREFIX}/bin:${CONDA_PREFIX}/Library/bin:$PATH"
8686
which mpirun
8787
mpirun --version
8888
mpirun -np 4 --oversubscribe python mpi_example.py # for openmpi

examples/bench.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
# benchmark reads and writes, with and without compression.
22
# tests all four supported file formats.
3+
from typing import TYPE_CHECKING, Any
34
from numpy.random.mtrand import uniform
45
import netCDF4
56
from timeit import Timer
67
import os, sys
8+
if TYPE_CHECKING:
9+
from netCDF4 import Format as NCFormat
10+
else:
11+
NCFormat = Any
712

813
# create an n1dim by n2dim by n3dim random array.
914
n1dim = 30
@@ -14,7 +19,7 @@
1419
sys.stdout.write('reading and writing a %s by %s by %s by %s random array ..\n'%(n1dim,n2dim,n3dim,n4dim))
1520
array = uniform(size=(n1dim,n2dim,n3dim,n4dim))
1621

17-
def write_netcdf(filename,zlib=False,least_significant_digit=None,format='NETCDF4'):
22+
def write_netcdf(filename,zlib=False,least_significant_digit=None,format: NCFormat='NETCDF4'):
1823
file = netCDF4.Dataset(filename,'w',format=format)
1924
file.createDimension('n1', n1dim)
2025
file.createDimension('n2', n2dim)

examples/bench_compress.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
# benchmark reads and writes, with and without compression.
22
# tests all four supported file formats.
3+
from typing import TYPE_CHECKING, Any
34
from numpy.random.mtrand import uniform
45
import netCDF4
6+
import netCDF4.utils
57
from timeit import Timer
68
import os, sys
9+
if TYPE_CHECKING:
10+
from netCDF4 import CompressionLevel
11+
else:
12+
CompressionLevel = Any
713

814
# create an n1dim by n2dim by n3dim random array.
915
n1dim = 30
@@ -13,10 +19,9 @@
1319
ntrials = 10
1420
sys.stdout.write('reading and writing a %s by %s by %s by %s random array ..\n'%(n1dim,n2dim,n3dim,n4dim))
1521
sys.stdout.write('(average of %s trials)\n' % ntrials)
16-
array = netCDF4.utils._quantize(uniform(size=(n1dim,n2dim,n3dim,n4dim)),4) # type: ignore
22+
array = netCDF4.utils._quantize(uniform(size=(n1dim,n2dim,n3dim,n4dim)),4)
1723

18-
19-
def write_netcdf(filename,zlib=False,shuffle=False,complevel=6):
24+
def write_netcdf(filename,zlib=False,shuffle=False,complevel: CompressionLevel = 6):
2025
file = netCDF4.Dataset(filename,'w',format='NETCDF4')
2126
file.createDimension('n1', n1dim)
2227
file.createDimension('n2', n2dim)

examples/bench_compress4.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# benchmark reads and writes, with and without compression.
22
# tests all four supported file formats.
3+
from typing import Literal
34
from numpy.random.mtrand import uniform
45
import netCDF4
56
from timeit import Timer
@@ -19,7 +20,11 @@
1920
array = nc.variables['hgt'][0:n1dim,5,:,:]
2021

2122

22-
def write_netcdf(filename,nsd,quantize_mode='BitGroom'):
23+
def write_netcdf(
24+
filename,
25+
nsd,
26+
quantize_mode: Literal["BitGroom", "BitRound", "GranularBitRound"] = "BitGroom"
27+
):
2328
file = netCDF4.Dataset(filename,'w',format='NETCDF4')
2429
file.createDimension('n1', None)
2530
file.createDimension('n3', n3dim)

examples/bench_diskless.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
# benchmark reads and writes, with and without compression.
22
# tests all four supported file formats.
3+
from typing import TYPE_CHECKING, Any, Literal
34
from numpy.random.mtrand import uniform
45
import netCDF4
56
from timeit import Timer
67
import os, sys
8+
if TYPE_CHECKING:
9+
from netCDF4 import Format as NCFormat
10+
else:
11+
NCFormat = Any
712

813
# create an n1dim by n2dim by n3dim random array.
914
n1dim = 30
@@ -14,7 +19,7 @@
1419
sys.stdout.write('reading and writing a %s by %s by %s by %s random array ..\n'%(n1dim,n2dim,n3dim,n4dim))
1520
array = uniform(size=(n1dim,n2dim,n3dim,n4dim))
1621

17-
def write_netcdf(filename,zlib=False,least_significant_digit=None,format='NETCDF4',closeit=False):
22+
def write_netcdf(filename, zlib=False, least_significant_digit=None, format: NCFormat='NETCDF4',closeit=False):
1823
file = netCDF4.Dataset(filename,'w',format=format,diskless=True,persist=True)
1924
file.createDimension('n1', n1dim)
2025
file.createDimension('n2', n2dim)
@@ -42,13 +47,13 @@ def read_netcdf(ncfile):
4247
sys.stdout.write('writing took %s seconds\n' %\
4348
repr(sum(t.repeat(ntrials,1))/ntrials))
4449
# test reading.
45-
ncfile = write_netcdf('test1.nc',format=format)
50+
ncfile = write_netcdf('test1.nc',format=format) # type: ignore
4651
t = Timer("read_netcdf(ncfile)","from __main__ import read_netcdf,ncfile")
4752
sys.stdout.write('reading took %s seconds\n' %
4853
repr(sum(t.repeat(ntrials,1))/ntrials))
4954

5055
# test diskless=True in nc_open
51-
format='NETCDF3_CLASSIC'
56+
format: Literal["NETCDF3_CLASSIC"] = 'NETCDF3_CLASSIC' # mypy should know this but it needs help...
5257
trials=50
5358
sys.stdout.write('test caching of file in memory on open for %s\n' % format)
5459
sys.stdout.write('testing file format %s ...\n' % format)

0 commit comments

Comments
 (0)