Skip to content

Commit ac8c6fa

Browse files
Don't bother with TypeGuard
1 parent 29fd359 commit ac8c6fa

1 file changed

Lines changed: 8 additions & 19 deletions

File tree

‎examples/mpi_example.py‎

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,23 @@
11
# to run: mpirun -np 4 python mpi_example.py
22
import sys
3-
from typing import TYPE_CHECKING
43
from mpi4py import MPI
54
import numpy as np
65
from netCDF4 import Dataset
7-
from typing_extensions import TypeGuard
8-
if TYPE_CHECKING:
9-
from netCDF4 import Format as NCFormat
10-
else:
11-
NCFormat = object
12-
136

14-
def is_nc_format(fmt: str) -> TypeGuard[NCFormat]:
15-
return fmt in {
16-
'NETCDF4',
17-
'NETCDF4_CLASSIC',
18-
'NETCDF3_CLASSIC',
19-
'NETCDF3_64BIT_OFFSET',
20-
'NETCDF3_64BIT_DATA'
21-
}
227

238
nc_format = 'NETCDF4_CLASSIC' if len(sys.argv) < 2 else sys.argv[1]
24-
if not is_nc_format(nc_format):
25-
raise ValueError("Invalid file format")
269

2710
rank = MPI.COMM_WORLD.rank # The process ID (integer 0-3 for 4-process run)
2811
if rank == 0:
2912
print('Creating file with format {}'.format(nc_format))
30-
nc = Dataset('parallel_test.nc', 'w', parallel=True, comm=MPI.COMM_WORLD,
31-
info=MPI.Info(), format=nc_format)
13+
nc = Dataset(
14+
"parallel_test.nc",
15+
"w",
16+
parallel=True,
17+
comm=MPI.COMM_WORLD,
18+
info=MPI.Info(),
19+
format=nc_format, # type: ignore # we'll assume it's OK
20+
)
3221
# below should work also - MPI_COMM_WORLD and MPI_INFO_NULL will be used.
3322
#nc = Dataset('parallel_test.nc', 'w', parallel=True)
3423
d = nc.createDimension('dim',4)

0 commit comments

Comments
 (0)