Skip to content
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

nidaqmx: Fix mypy errors #523

Merged
merged 10 commits into from
Mar 5, 2024
Merged

nidaqmx: Fix mypy errors #523

merged 10 commits into from
Mar 5, 2024

Conversation

bkeryan
Copy link
Collaborator

@bkeryan bkeryan commented Mar 1, 2024

What does this Pull Request accomplish?

Enable running mypy on the actual nidaqmx package without errors. For details, see commits.

Update PR build workflow to run mypy on generated/nidaqmx.

Why should this Pull Request be merged?

Even without type hints, running mypy on the nidaqmx package can catch bugs, such as passing the wrong arguments to a function.

Running mypy on the nidaqmx package is a prerequisite for adding type hints.

Note

Adding type hints will force us to stop reassigning to parameters, like val = numpy.array(val, dtype=numpy.float64). These currently pass because they are untyped and thus are assumed to be typing.Any.

What testing has been done?

Ran poetry run pytest -v
Ran poetry run mypy generated/nidaqmx and poetry run mypy tests

Copy link

github-actions bot commented Mar 1, 2024

Test Results

    30 files  ±0      30 suites  ±0   37m 31s ⏱️ -26s
 1 898 tests ±0   1 631 ✅ ±0    267 💤 ±0  0 ❌ ±0 
30 200 runs  ±0  26 280 ✅ ±0  3 920 💤 ±0  0 ❌ ±0 

Results for commit d54cceb. ± Comparison against base commit 5f3dd8f.

♻️ This comment has been updated with latest results.

Copy link
Collaborator

@zhindes zhindes left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lots of great improvements!

generated\nidaqmx\utils.py:78: error: Unsupported operand types for + ("object" and "int")  [operator]
generated\nidaqmx\utils.py:79: error: Unsupported left operand type for >= ("object")  [operator]
generated\nidaqmx\utils.py:80: error: Unsupported operand types for - ("object" and "int")  [operator]
generated\nidaqmx\utils.py:81: error: Unsupported left operand type for <= ("object")  [operator]
generated\nidaqmx\scale.py:548: error: Incompatible types in assignment (expression has type "type[Scale]", variable has type "type[_ScaleAlternateConstructor]")  [assignment]
generated\nidaqmx\system\physical_channel.py:696: error: Incompatible types in assignment (expression has type "type[PhysicalChannel]", variable has type "type[_PhysicalChannelAlternateConstructor]")  [assignment]
generated\nidaqmx\_time.py:12: error: Cannot find implementation or library stub for module named "backports"  [import-not-found]
generated\nidaqmx\_time.py:12: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
generated\nidaqmx\_time.py:12: error: Name "zoneinfo" already defined (by an import)  [no-redef]
https://numpy.org/doc/stable/user/basics.types.html says:
> Data-types can be used as functions to convert python numbers to array scalars...

However, numpy/numpy#23400 says:

> Closing for now, as long as the consensus on these types of array-from-scalar constructions remains "do not use; use np.array(..., dtype=...) instead".

generated\nidaqmx\system\watchdog.py:244: error: Argument 1 to "floating" has incompatible type "list[Any]"; expected "Union[None, Union[str, bytes], SupportsFloat, SupportsIndex]"  [arg-type]
generated\nidaqmx\system\watchdog.py:246: error: Argument 1 to "signedinteger" has incompatible type "list[Any]"; expected "Union[SupportsInt, Union[str, bytes], SupportsIndex]"  [arg-type]
generated\nidaqmx\system\watchdog.py:278: error: Argument 1 to "signedinteger" has incompatible type "list[Any]"; expected "Union[SupportsInt, Union[str, bytes], SupportsIndex]"  [arg-type]
generated\nidaqmx\system\watchdog.py:310: error: Argument 1 to "signedinteger" has incompatible type "list[Any]"; expected "Union[SupportsInt, Union[str, bytes], SupportsIndex]"  [arg-type]
generated\nidaqmx\system\system.py:408: error: Argument 1 to "floating" has incompatible type "list[Any]"; expected "Union[None, Union[str, bytes], SupportsFloat, SupportsIndex]"  [arg-type]
generated\nidaqmx\system\system.py:410: error: Argument 1 to "signedinteger" has incompatible type "list[Any]"; expected "Union[SupportsInt, Union[str, bytes], SupportsIndex]"  [arg-type]
generated\nidaqmx\_task_modules\in_stream.py:1063: error: Incompatible types in assignment (expression has type "type[unsignedinteger[Any]]", variable has type "type[signedinteger[Any]]")  [assignment]
generated\nidaqmx\_task_modules\in_stream.py:1066: error: Incompatible types in assignment (expression has type "type[signedinteger[Any]]", variable has type "type[signedinteger[Any]]")  [assignment]
generated\nidaqmx\_task_modules\in_stream.py:1068: error: Incompatible types in assignment (expression has type "type[unsignedinteger[Any]]", variable has type "type[signedinteger[Any]]")  [assignment]
generated\nidaqmx\_task_modules\in_stream.py:1071: error: Incompatible types in assignment (expression has type "type[signedinteger[Any]]", variable has type "type[signedinteger[Any]]")  [assignment]
generated\nidaqmx\_task_modules\in_stream.py:1073: error: Incompatible types in assignment (expression has type "type[unsignedinteger[Any]]", variable has type "type[signedinteger[Any]]")  [assignment]
generated\nidaqmx\stream_readers.py:77: error: Incompatible types in assignment (expression has type "tuple[int]", variable has type "Optional[tuple[int, Any]]")  [assignment]
generated\nidaqmx\stream_readers.py:80: error: Incompatible types in assignment (expression has type "tuple[Any]", variable has type "Optional[tuple[int, Any]]")  [assignment]
generated\nidaqmx\stream_readers.py:121: error: Incompatible types in assignment (expression has type "tuple[int]", variable has type "Optional[tuple[int, Any]]")  [assignment]
generated\nidaqmx\stream_readers.py:124: error: Incompatible types in assignment (expression has type "tuple[Any]", variable has type "Optional[tuple[int, Any]]")  [assignment]
`read()` and `write()` had a lot of mypy errors:
- `array_shape` is either a tuple of ints or a single int. Change to be a variable-sized tuple of ints.
- `data` is a list of named tuples for counter pulse and power, and an ndarray for everything else. Extract `_read_ctr_pulse` and `_read_power` helper methods to clean this up.
- `data` and other variables are reused to build lists with for-loops. Convert these to use list comprehensions.
- Convert the power meas_type check to use a generator expression.
- `numpy.asarray` is for converting values that may already be an ndarray. Use `numpy.array` when the input is definitely not an ndarray.
- Add some else cases to catch unexpected meas/output types.

generated\nidaqmx\task.py:614: error: Incompatible types in assignment (expression has type "int", variable has type "tuple[int, Any]")  [assignment]
generated\nidaqmx\task.py:650: error: Argument 1 to "append" of "list" has incompatible type "list[PowerMeasurement]"; expected "PowerMeasurement"  [arg-type]
generated\nidaqmx\task.py:663: error: Incompatible types in assignment (expression has type "ndarray[Any, dtype[floating[_64Bit]]]", variable has type "list[PowerMeasurement]")  [assignment]
generated\nidaqmx\task.py:671: error: Incompatible types in assignment (expression has type "ndarray[Any, dtype[Any]]", variable has type "list[PowerMeasurement]")  [assignment]
generated\nidaqmx\task.py:676: error: Incompatible types in assignment (expression has type "ndarray[Any, dtype[unsignedinteger[_32Bit]]]", variable has type "list[PowerMeasurement]")  [assignment]
generated\nidaqmx\task.py:694: error: Argument 1 to "append" of "list" has incompatible type "CtrFreq"; expected "PowerMeasurement"  [arg-type]
generated\nidaqmx\task.py:705: error: Argument 1 to "append" of "list" has incompatible type "CtrTime"; expected "PowerMeasurement"  [arg-type]
generated\nidaqmx\task.py:716: error: Argument 1 to "append" of "list" has incompatible type "CtrTick"; expected "PowerMeasurement"  [arg-type]
generated\nidaqmx\task.py:719: error: Incompatible types in assignment (expression has type "ndarray[Any, dtype[unsignedinteger[_32Bit]]]", variable has type "list[PowerMeasurement]")  [assignment]
generated\nidaqmx\task.py:726: error: Incompatible types in assignment (expression has type "ndarray[Any, dtype[floating[_64Bit]]]", variable has type "list[PowerMeasurement]")  [assignment]
generated\nidaqmx\task.py:753: error: "list[PowerMeasurement]" has no attribute "tolist"  [attr-defined]
generated\nidaqmx\task.py:757: error: No overload variant of "__getitem__" of "list" matches argument type "tuple[slice, slice]"  [call-overload]
generated\nidaqmx\task.py:757: note: Possible overload variants:
generated\nidaqmx\task.py:757: note:     def __getitem__(self, SupportsIndex, /) -> PowerMeasurement
generated\nidaqmx\task.py:757: note:     def __getitem__(self, slice, /) -> list[PowerMeasurement]
generated\nidaqmx\task.py:759: error: "list[PowerMeasurement]" has no attribute "tolist"  [attr-defined]
generated\nidaqmx\task.py:761: error: "list[PowerMeasurement]" has no attribute "tolist"  [attr-defined]
generated\nidaqmx\task.py:1261: error: Incompatible types in assignment (expression has type "ndarray[Any, dtype[floating[_64Bit]]]", variable has type "list[Any]")  [assignment]
generated\nidaqmx\task.py:1262: error: Incompatible types in assignment (expression has type "ndarray[Any, dtype[floating[_64Bit]]]", variable has type "list[Any]")  [assignment]
generated\nidaqmx\task.py:1275: error: Incompatible types in assignment (expression has type "ndarray[Any, dtype[floating[_64Bit]]]", variable has type "list[Any]")  [assignment]
generated\nidaqmx\task.py:1276: error: Incompatible types in assignment (expression has type "ndarray[Any, dtype[floating[_64Bit]]]", variable has type "list[Any]")  [assignment]
generated\nidaqmx\task.py:1289: error: Incompatible types in assignment (expression has type "ndarray[Any, dtype[unsignedinteger[_32Bit]]]", variable has type "list[Any]")  [assignment]
generated\nidaqmx\task.py:1290: error: Incompatible types in assignment (expression has type "ndarray[Any, dtype[unsignedinteger[_32Bit]]]", variable has type "list[Any]")  [assignment]
generated\nidaqmx\_lib.py:175: error: Argument 1 to "LoadLibrary" of "LibraryLoader" has incompatible type "Optional[str]"; expected "str"  [arg-type]
generated\nidaqmx\_lib.py:10: error: Skipping analyzing "decouple": module is installed, but missing library stubs or py.typed marker  [import-untyped]
generated\nidaqmx\_lib.py:10: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
@bkeryan bkeryan merged commit 57b0001 into master Mar 5, 2024
17 checks passed
@bkeryan bkeryan deleted the users/bkeryan/mcd-mypy branch March 5, 2024 01:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants