beartype: [Bug] `beartype.door` permissively accepts unsubscripted `typing.Optional` and `typing.Union` type hint factories as akin to typing Gods!? ๐ฑ
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. ๐น
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Reactions: 2
- Comments: 15 (7 by maintainers)
Oh, wondrous day! Youโre using MIT too. I was concerned you might be tied to a GPL-like viral license. But MIT? No problem-o! MIT and BSD licensing is so permissive that it happily combines with basically everything.
Consider prefacing the existing
LICENSEfile in the root of your project with flimsy legalese that delineates the licensing: e.g.,Something like that should happily suffice.
โฆainโt that truth. Iโve been debating rescinding my U.S. citizenship for decades, but held back out of a general distrust of the U.S. government. That distrust has now proven to be justified. Manifest destiny is still manifesting. My disappointment is measurable in gigameters.
My wifeโs in a much more precarious situation. She only holds Canadian citizenship. Since she works for an American university, Iโve tried suggesting she apply for U.S. citizenship. She acknowledges that this now makes rational senseโฆ but the emotional sense is still out to lunch. She canโt bring herself to do it. I get that. Emotions beat up rationality and then steal rationalityโs lunch money.
This fragile construct we call โCanadaโ may only have a few more years of ice-encrusted life left in it. We will miss you so much when youโre gone, Canada. ๐จ๐ฆ
Sneaky, sneakyโฆ ๐ Itโs a weird time to be a dual-citizen, thatโs for sure! ๐น
But yeah, I think I might just do that! This is going in my base package, anyway! Does this license work with the MIT license, though?
โtyping_extensions/LICENSE at main ยท python/typing_extensions ยท GitHubโ https://github.com/python/typing_extensions/blob/main/LICENSE
The first footnote seems to imply it wouldโฆ
Ha-ha! I had a similar (yet perhaps even more perverse) idea. Just:
typingmodule inside your codebase before CPython 3.15 [or whatevahs] finally disembowels it.The only downside there is a potential licensing conflict, depending on whatever license youโve chosen. Butโฆ yeah. Other that licensing, thatโs probably the optimal solution. Itโs just a single file, so itโs trivially embeddable. You control itโs contents, so youโre guaranteed that it will always satisfy your use case.
Oh, andโฆ Happy 2025! ๐ฅณ
So Iโll have to scour the Python documentation for all the subscription sizes? ๐ญ Thatโs going to take forever! โฆ And so will refactoring my tests! ๐ญ
Ah, wellโฆ I can always use the
typingpackage on PyPI. ๐นDoesnโt exist, sadly. Let us commiserate together in shared misery.
typingis hell. ๐ญSuperb question! Alas, the answer is: โI am dumb and know nothing.โ Specifically:
typingmodule. Removal will occur no sooner than Python 3.9โs end of life, scheduled for October 2025.โโฆheh. Not a snowballโs chance in Hell. CPython devs hate runtime typing. Hell, CPython devs hate typing in general. There are still no type hints in CPythonโs standard library. No way theyโd ever add a public API for introspecting type hints to anything outside the
typingmodule โ which theyโre actively trying to deprecate, obsolete, and delete as fast as possible.typingis sadness. Cry, emoji cat! ๐ฟโฆheh. Ye Olโ
_nparamsTrick, huh? The problem with that is that thatโs going away โ probably by 2023, but possibly sooner. CPython devs hate most of thetypingmodule, which they now consider legacy code that should just go away and die quietly in a corner by itself already. Itโs burdensome technical debt theyโd rather not maintain. I get thatโฆ sorta.Instead of violating privacy encapsulation on obsolete attributes, consider just hard-coding a mapping from type hint factories to subscription ranges. This is what @beartype does:
As the above code suggests, the standard
contextlib.AsyncContextManagerandcontextlib.ContextManagertype hint factories accept a variable range of subscriptions conditionally depending on the current CPython version. All other type hint factories currently accept a fixed length of subscriptions. This is QA nightmare fuel.Wherever you may roam, you really want to reduce your dependence on private attributes but especially on obsolete private attributes. They will go away. Guaranteed. CPython devs are merciless about this sort of thing. Purple Karen shrugs. ๐คทโโ๏ธ
Fair enough! ๐น I still wish they included the errors regarding how many subscriptions are possible for each type, like unlimited for
tupleandUnion, 1 for other iterables likelist,set, and 2 for mappings likedict.At the moment, for my own project, Iโm just looping through types of the same name in the various typing modules and getting the subscription sizes from there; most likely thereโs some significant overhead, but hopefully newer Python versions will be fast enough that it wonโt be noticeable! ๐
And again, thanks for the heads-up about the base
Unionmatching all types! Iโm just going to returnTrueif the object is an annotation of any kind and false otherwise!lolbro. Baymax intensifies and then feels deflated. โฆget it, โdeflatedโ? you see, that was a pun becauseโฆ oh, nmind.
typingsure is super-weird, huh? You have now noticed that deprecated PEP 484-complianttypingtype hint factories behave nothing like PEP 585-compliant builtin type hint factories. Behold! True horror!PEP 585 and 484 disagree about everything. PEP 585 takes precedence, because PEP 585 destroyed PEP 484. I donโt know. Neither does anyone else. Itโs probably best to assume PEP 585 has the right of it โ especially because that assumption simplifies everything and makes everything orthogonal.
Basically:
typing.Anysingleton.Sadly, this was never actually formalized anywhere. PEP 484 strongly hints โฆget it? iโll stop now that this is the case, but never comes right out and says it. Because itโs easier to be permissive, @beartype assumes that this is the case.
Thus ends the tale of the 5th QA battle. PEP 585: You win the day! ๐
Althoughโฆ Apparently you canโt do something like
typing.List[typing.Union]โฆ?โ Baymax
Looks like Iโve got some code to revise in my own project as well, then. ๐น Iโll have to allow everything to match an unsubscripted
typing.Union! Thanks for the heads-up! ๐ธYears passโฆ before @leycec belatedly realises that @beartype is actually behaving as expected here. Although insane, @beartypesโs current behaviour is โworking as intended.โ *
* The terms โworkingโ and โintendedโ are both nebulous and ill-defined.
You are now thinking:
Yes. Unsubscripted type hint factories like
typing.Optionalandtyping.Unionare themselves valid type hints. I know. Look. This isnโt up to @beartype. This is according to the insane standards. Specifically, PEP 484, which stipulates that unsubscripted type hint factories are implicitly subscripted bytyping.Any. Ergo:typing.Unionimplicitly expands totyping.Union[typing.Any]. However, PEP 484 also stipulates that a union of one type semantically reduces to only that type! Since this implies thattyping.Union[typing.Any]semantically reduces to merelytyping.Any, the unsubscripted type factorytyping.Unionreduces totyping.Anyas well.typing.Optional, which behaves similarly.Donโt shoot the QA messenger. I donโt make the QA-destroying
typingstandards. I only ineffectually complain about the QA-destroyingtypingstandards live on GitHub.2025: ainโt off to a good start is it, folks? ๐ญ
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. ๐ญWho cares about those, though! Weโve got an unsubscripted
Unionto crush.