Skip to content

Enable an isinstance like check of a TypeAlias with beartype is_bearable #994

Open
@gumyr

Description

@gumyr

As Python doesn't allow an isinstance check of a TypeAlias there are checks in the code for the sub-components of the TypeAlias. Unfortunately these are not all consistent which could result in unexpected behavior. The beartype package has a is_bearable function that could be used for this purpose as shown here:

from typing import TypeAlias
from collections.abc import Sequence
from build123d import Vector
from beartype.door import is_bearable

VectorLike: TypeAlias = Vector | Sequence[float | int]
for v in [Vector(1, 2, 3), "Vector", (1, 2, 3)]:
    if is_bearable(v, VectorLike):
        print(f"{v} is a VectorLike")
    else:
        print(f"{v} is not a VectorLike")
Vector(1, 2, 3) is a VectorLike
Vector is not a VectorLike
(1, 2, 3) is a VectorLike

To enable portability it might be a good idea to wrap is_bearable into a build123d is_type_alias (or similar) function.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions