如何检查NewType值的类型?

发布于 2025-02-12 01:35:46 字数 1639 浏览 1 评论 0原文

如何在不铸造和混合类型检查的情况下检查newType的值 str ?

我声明了一种新类型:

BoardId = NewType("BoardId", str)

str之外,我将其用作方法参数作为单个值或迭代

我使用实例检查类型是否为str而不是tuple,然后我将其转换为元组,否则请按原样使用它。 问题pyright事物boardIdstr,因为isinstance失败了类型检查。 另一方面,我不能使用isinstance newType以来

离开时

    async def get_stickies(
        self, board_id: Union[BoardId, Iterable[BoardId]]
    ) -> List[Sticky]:
        board_ids: Iterable[BoardId] = (
            (board_id,) if isinstance(board_id, str) else board_id
        )

要出现这个问题:

Diagnostics:                                                                                                                      
1. Expression of type "tuple[BoardId | str] | Iterable[BoardId]" cannot be assigned to declared type "Iterable[BoardId]"
     Type "tuple[BoardId | str] | Iterable[BoardId]" cannot be assigned to type "Iterable[BoardId]"
       TypeVar "_T_co@Iterable" is covariant
         Type "BoardId | str" cannot be assigned to type "BoardId"
           "str" is incompatible with "BoardId"

”在此处输入图像说明”

How do you check the value of NewType against its basic type str without casting and mixing up type checking?

I declared a new type:

BoardId = NewType("BoardId", str)

that I use as a method parameter either as a single value or as an Iteration other than str.

I use instanceof to check if the type is str and not Tuple then I convert it to a tuple otherwise use it as it is.
The problem pyright things BoardId is str because of isinstance which fails the type checking.
On the other hand, I cannot use isinstance against NewType since:

TypeError: isinstance() arg 2 must be a type, a tuple of types, or a union

Leaving as it is

    async def get_stickies(
        self, board_id: Union[BoardId, Iterable[BoardId]]
    ) -> List[Sticky]:
        board_ids: Iterable[BoardId] = (
            (board_id,) if isinstance(board_id, str) else board_id
        )

causes this issue:

Diagnostics:                                                                                                                      
1. Expression of type "tuple[BoardId | str] | Iterable[BoardId]" cannot be assigned to declared type "Iterable[BoardId]"
     Type "tuple[BoardId | str] | Iterable[BoardId]" cannot be assigned to type "Iterable[BoardId]"
       TypeVar "_T_co@Iterable" is covariant
         Type "BoardId | str" cannot be assigned to type "BoardId"
           "str" is incompatible with "BoardId"

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

毁梦 2025-02-19 01:35:46

中提出了一种解决方案。

from typing import Iterable, TypeAlias, NewType

_BoardIdT: TypeAlias = str
BoardId = NewType('BoardId', _BoardIdT)

def get_stickies(board_id: BoardId | Iterable[BoardId]) -> None:
    board_ids: Iterable[BoardId] = (
        (board_id,) if isinstance(board_id, _BoardIdT) else board_id
    )

mypy不喜欢此无证件属性。

One solution was proposed in this issue:

from typing import Iterable, TypeAlias, NewType

_BoardIdT: TypeAlias = str
BoardId = NewType('BoardId', _BoardIdT)

def get_stickies(board_id: BoardId | Iterable[BoardId]) -> None:
    board_ids: Iterable[BoardId] = (
        (board_id,) if isinstance(board_id, _BoardIdT) else board_id
    )

Playground link

The underlying type is accessible via BoardId.__supertype__, but mypy doesn't like this undocumented attribute.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文