Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
gumyr opened this issue May 21, 2025 · 0 comments
Open
Labels
enhancement New feature or request

Comments

@gumyr
Copy link
Owner

gumyr commented May 21, 2025

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.

@gumyr gumyr added this to the Not Gating Release 1.0.0 milestone May 21, 2025
@gumyr gumyr added the enhancement New feature or request label May 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant