-
Notifications
You must be signed in to change notification settings - Fork 264
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Corrections and enchancements to type stubs #1349
Conversation
…tensions for 3.8 compat
…o 'from ... import *' without '__all__' in _netCDF4.pyx
…TypeVar for variable type is now unbound.
I've decided that running mypy against the existing tests is good enough for now (not going to write more typing-focused tests). |
This reverts commit cd002c8.
I haven't had time to try to set up the whole CI environment on my own machine but I don't think the failures are caused by this PR. |
yes, the mpi test failure is due an update in the conda mpi package - nothing to do with this PR. Just awaiting @headtr1ck's review now. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some good improvements, but I got some questions :)
@headtr1ck are you ok with this PR now? |
Would like to make a 1.7.2 release this week, and include this if possible. @headtr1ck could you please complete your review? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Besides the changed test files this looks good!
Sorry for the delay in review :)
I guess we have to solve some merge conflicts. |
@jswhit looks good now! |
@RandallPittmanOrSt if you update your fork to the latest develop I can merge |
@jswhit Done. |
OK thanks @RandallPittmanOrSt - merging now |
This PR includes some corrections and enhancements to the type stubs provided in #1302. Some test cases and examples were modified during the process of testing the stubs on them with
mypy
.Enhancements
Variable.dtype
andVariable.datatype
properties are overloaded via faux descriptor class workaround (ref. these mypy and pyright issues). This is the only way we can actually get correct types out of these in static analysis.--check-untyped-defs
. (Some nuisance errors had to be silenced with the--allow-redefinition
flag and some# type: ignore
comments.)test/run_all.py
Fixes
str
orint
). It may be argued that this renders inclusion of the type literals ('u2'
,'i4'
, etc.) moot but they are left in as they can be useful in IDE pop-up documentation.dtype_is_complex
into__init__.pyi
. It is publicly accessible innetCDF4
due to import of__all__
fromnetCDF4._netCDF4
.TheEDIT: We decided to accept anything forGetSetItemKey
for type alias__getitem__
and__setitem__
needs to also acceptfloat
(might be a mathematical integer) andstr
(might be convertible to integer) and also needs separatelist[]
generics defined for each possible type, rather thanlist[int | bool | etc...]
due to lists be invariant and mutable.key
.Dimension
can be created withfloat
size if it's a mathematical integer.zlib
,shuffle
,fletcher32
, andcontiguous
arguments for creating a variable don't have to bebool
, just "truthy", so they are changed toAny
.Variable
typing and overloadingTypeVar
forVariable
(now calledVarT
) has been changed to be unbound. The types in the oldbound=
argument weren't really correct nor useful.Variable.__new__
(andcreateVariable
) where if a variable is created using a NumPy type orstr
as the datatype, then the type of the variable can be known.str
or a NumPy type cannot be defined statically. In the future, we might be able to makeCompoundType
,EnumType
, and/orVLType
generic in which case we could possibly use those specified generics as types forVariable
.EnumType, the
enum_dictarg has been changed to
Mappinginstead of
dict`... This helps with [co/in]variance issues.Dataset.__dealloc__
is a Cython-only method and not publically accessible, so it was removed from the stubs.test_enum.EnumDictTestCase
wasn't called due to bad indentation of methods. Fixed some issues with that test case.ma.masked_array
instead ofma.core.MaskedArray
. NumPy has gone back and forth withMaskedArray
being part ofma.core
or justma
depending on NumPy version.ma.masked_array
appears to be a reliable alias in all cases.isinstance
assertions for type narrowingtest_masked.py
- around line 96test_multifile.py
- around line 77test_multifile.py
)"Cosmetic" changes
ruff
. Used 130-character limit as per https://typing.readthedocs.io/en/latest/source/stubs.html#maximum-line-length. To see changes in this PR excluding this reformat, see here: RandallPittmanOrSt/netcdf4-python@851d453...rwp_typing.TypeAlias
es to be singular where each represents a single thing, not a collection.Future possible changes
It might be better to allow any string or int where we're requiring
Literal
arguments in functions, while still providing theLiteral
s for auto-completion tooling and documentation. Many of the modifications to the test cases could be reverted if we did this.