Skip to content
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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug] beartype.door permissively accepts unsubscripted typing.Optional and typing.Union type hint factories as akin to typing Gods!? 馃槺 #376

Open
sylvorg opened this issue Apr 27, 2024 · 2 comments

Comments

@sylvorg
Copy link

sylvorg commented Apr 27, 2024

Hello! Guess who's back! ... I'm not doing that joke again, you can't make me! 馃槫

Get ready for a deluge of replies, starting with this issue! ... At least I think it's an issue!

Is everything supposed to match a Union generic when using beartype.door.is_bearable and beartype.door.is_subhint? For example, with the following block:

from beartype.door import is_bearable, is_subhint
from collections.abc import Callable, Iterable
from typing import Literal, Union

for t in (
    str | int,
    Union,
    Union[str, int],
    Callable,
    Callable[..., ...],
    Iterable,
    Iterable[str],
    Literal,
    Literal[1, 2],
):
    assert is_bearable(t, Union)
    assert is_subhint(t, Union)

No AssertionError occurs. I'd really like to switch to is_subhint, because then I wouldn't have to worry about issubclass erroring out on annotations like Union[...], etc., and using typing.get_origin all the time! 馃槄

Thank you kindly for the help!


P.S.: I passed the graphics course! 馃樆 I got a D, but still. A win is a win. 馃樄

@leycec
Copy link
Member

leycec commented Apr 27, 2024

Yay!!! It's my favourite silver Borg, @sylvorg. He's right about everything, of course. @beartype should definitely be rejecting unsubscripted type hint factories that otherwise have no semantic meaning like:

  • typing.Literal. Think we nailed this one recently? I dimly recall nailing this. Stay down when I put you down, bugs!
  • typing.Optional. 馃槩
  • typing.Union. 馃槶
  • Probably others.

Who cares about those, though! We've got an unsubscripted Union to crush.

using typing.get_origin all the time!

P.S.: I passed the graphics course! 馃樆 I got a D, but still. A win is a win. 馃樄

@leycec leycec changed the title Everything matches a Union generic!? 馃槺 [Bug] beartype.door permissively accepts unsubscripted typing.Optional and typing.Union type hint factories as akin to typing Gods!? 馃槺 Apr 27, 2024
@sylvorg
Copy link
Author

sylvorg commented Apr 27, 2024

Yay!!! It's my favourite silver Borg, @sylvorg.

Technically, Sylveon plus:
borg

But yes. 馃樄

This issue is quite exciting because it could reduce quite a bit of code on my end! 馃樆

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants