You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm trying to add type hints to an existing code base. I currently have a function (simplified):
defarray_to_file(
data,
fileobj,
out_dtype,
intercept=0.0,
divslope=1.0,
):
ifnotnp.all(np.isfinite((intercept, 1.0ifdivslopeisNoneelsedivslope))):
raiseValueError('divslope and intercept must be finite')
...
We have written it to accept numpy or Python types. The general use case is Python types, but if someone wants to avoid doubling their memory by making sure all passed values are float32, they can. I want to make this:
Scalar=np.number|floatdefarray_to_file(
data: npt.ArrayLike,
fileobj: io.IOBase,
out_dtype: np.dtype|None=None,
intercept: Scalar=0.0,
divslope: Scalar|None=1.0,
):
ifnotnp.all(np.isfinite((intercept, 1.0ifdivslopeisNoneelsedivslope))):
raiseValueError('divslope and intercept must be finite')
...
This is because np.ArrayLike assumes that possible compatible types are one of the following:
a numpy ndarray of numpy dtypes (_SupportsArray[_DType])
a python sequence (_NestedSequence) of numpy dtypes (_NestedSequence[_SupportsArray[np.dtype[Any]]])
a python sequence with python numeric type constituents (_NestedSequence[bool, int, float, ...])
a bare python numeric type. (bool, int, float, ...)
None of these capture the possibility of a python list which could have python numeric types or numpy dtypes since it crosses two cases.
I'm working on fixing this by adding a separate case that would cover both situations such as _NestedSequence[Union[dtype[Any], bool, int, float, ...]].
Describe the issue:
I'm trying to add type hints to an existing code base. I currently have a function (simplified):
We have written it to accept numpy or Python types. The general use case is Python types, but if someone wants to avoid doubling their memory by making sure all passed values are float32, they can. I want to make this:
But I get the error below.
Reproduce the code example:
Error message:
Runtime information:
Context for the issue:
This is not terribly high priority. I can rewrite this in a way that will type check, but it seems like a bug.
If
numpy.typing
has better ways of writingScalar
that I haven't seen, happy to switch to that.The text was updated successfully, but these errors were encountered: