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. |
headtr1ck
left a comment
There was a problem hiding this comment.
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? |
headtr1ck
left a comment
There was a problem hiding this comment.
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.dtypeandVariable.datatypeproperties 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-redefinitionflag and some# type: ignorecomments.)test/run_all.pyFixes
strorint). 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_complexinto__init__.pyi. It is publicly accessible innetCDF4due to import of__all__fromnetCDF4._netCDF4.TheEDIT: We decided to accept anything forGetSetItemKeyfor 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.Dimensioncan be created withfloatsize if it's a mathematical integer.zlib,shuffle,fletcher32, andcontiguousarguments for creating a variable don't have to bebool, just "truthy", so they are changed toAny.Variabletyping and overloadingTypeVarforVariable(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 orstras the datatype, then the type of the variable can be known.stror a NumPy type cannot be defined statically. In the future, we might be able to makeCompoundType,EnumType, and/orVLTypegeneric in which case we could possibly use those specified generics as types forVariable.EnumType, theenum_dictarg has been changed toMappinginstead ofdict`... 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.EnumDictTestCasewasn't called due to bad indentation of methods. Fixed some issues with that test case.ma.masked_arrayinstead ofma.core.MaskedArray. NumPy has gone back and forth withMaskedArraybeing part ofma.coreor justmadepending on NumPy version.ma.masked_arrayappears to be a reliable alias in all cases.isinstanceassertions 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.TypeAliases 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
Literalarguments in functions, while still providing theLiterals for auto-completion tooling and documentation. Many of the modifications to the test cases could be reverted if we did this.