numpy scaleartype和键入Mypy的提示
我有一个用于给定形状的2D数组的容器类,该类别在内部使用numpy数组来存储数据。如果用户将标量传递给构造函数,我会用np.full创建一个填充该值的数组,而如果用户通过阵列或类似数组,则我检查它的形状正确,并在新数组中复制它。
为了进行我使用的检查:
if isinstance(data, np.ScalarType):
但是Mypy抱怨NP.Scalartype:
表达式类型包含“任何”(具有“ type”元组[type [int], 类型[float],类型[复杂],类型[int],type [bool],type [bytes], type [str],type [memoryView],type [bool_] ],键入[complexting [any,any],键入[complexting floing [any, ],键入[floating [any]],键入[floating [any],键入[floating [any]], 键入[floating [any] 键入[signedInteger [any] type [signedInteger [any],type [signedInteger [any],type [timeDERTA64], type [dateTime64],type [object_],type [bytes_],type [str_], type [unsedeDInteger [any] type [unsedeDInteger [any] 键入[unseDInteger [any],键入[void]]”)[misc]
是否有办法避免警告并使Mypy感到高兴,或者是一种更好的方法来进行检查?我在使用Numpy 1.22.3 Python 3.8.10
I have a container class for 2d arrays of a given shape, that internally uses a numpy array to store the data. If the user passes a scalar to the constructor I create an array filled with that value with np.full, while if the user passes an array or array-like I check that it is of the right shape and copy it in a new array.
To do the check I use:
if isinstance(data, np.ScalarType):
but mypy complains about np.ScalarType:
Expression type contains "Any" (has type "Tuple[Type[int],
Type[float], Type[complex], Type[int], Type[bool], Type[bytes],
Type[str], Type[memoryview], Type[bool_], Type[complexfloating[Any,
Any]], Type[complexfloating[Any, Any]], Type[complexfloating[Any,
Any]], Type[floating[Any]], Type[floating[Any]], Type[floating[Any]],
Type[floating[Any]], Type[signedinteger[Any]],
Type[signedinteger[Any]], Type[signedinteger[Any]],
Type[signedinteger[Any]], Type[signedinteger[Any]], Type[timedelta64],
Type[datetime64], Type[object_], Type[bytes_], Type[str_],
Type[unsignedinteger[Any]], Type[unsignedinteger[Any]],
Type[unsignedinteger[Any]], Type[unsignedinteger[Any]],
Type[unsignedinteger[Any]], Type[void]]") [misc]
Is there a way to avoid the warning and make mypy happy, or a better way to do the check in general? I am using numpy 1.22.3 on Python 3.8.10
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论