numpy scaleartype和键入Mypy的提示

发布于 2025-02-02 19:35:46 字数 850 浏览 2 评论 0原文

我有一个用于给定形状的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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文