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)

Most upvoted comments

Does this license work with the MIT license, though?

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 LICENSE file in the root of your project with flimsy legalese that delineates the licensing: e.g.,

All files *EXCEPT* "{muh_package_name}/typing.py" are MIT-licensed.
"{muh_package_name}/typing.py" is licensed under the Python Software
Foundation License Version 2.

Something like that should happily suffice.

Itโ€™s a weird time to be a dual-citizen, thatโ€™s for sure! ๐Ÿ˜น

โ€ฆ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. ๐Ÿ‡จ๐Ÿ‡ฆ

Oh, andโ€ฆ Happy 2025! ๐Ÿฅณ

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โ€ฆ

I can always use the typing package on PyPI.

Ha-ha! I had a similar (yet perhaps even more perverse) idea. Just:

  • Embed a copy of the existing typing module 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 typing package on PyPI. ๐Ÿ˜น

I need an automated way of doing itโ€ฆ

Doesnโ€™t exist, sadly. Let us commiserate together in shared misery. typing is hell. ๐Ÿ˜ญ

Do you know when the typing types are going away?

Superb question! Alas, the answer is: โ€œI am dumb and know nothing.โ€ Specifically:

  • The best-case answer is 2030. CPython devs want this logic gone now, but they also donโ€™t want to enrage everyone still stuck with legacy type hints.
  • The worst-case answer is later this year. To quote PEP 585: โ€œThe deprecated functionality may eventually be removed from the typing module. Removal will occur no sooner than Python 3.9โ€™s end of life, scheduled for October 2025.โ€

And if the subscription sizes are going to be added to the official types?

โ€ฆ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 typing module โ€“ which theyโ€™re actively trying to deprecate, obsolete, and delete as fast as possible.

typing is sadness. Cry, emoji cat! ๐Ÿ˜ฟ

Iโ€™m just looping through types of the same name in the various typing modules and getting the subscription sizes from there

โ€ฆheh. Ye Olโ€™ _nparams Trick, huh? The problem with that is that thatโ€™s going away โ€“ probably by 2023, but possibly sooner. CPython devs hate most of the typing module, 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:

# ....................{ PRIVATE ~ globals                  }....................
# Note that the builtin "range" class:
# * Initializer "range.__init__(start, stop)" is effectively instantiated as
#   [start, stop) -- that is to say, such that:
#   * The initial "start" integer is *INCLUSIVE* (i.e., the instantiated range
#     includes this integer).
#   * The final "stop" integer is *EXCLUSIVE* (i.e., the instantiated range
#     excludes this integer).
# * Publicizes these integers as the instance variables "start" and "stop" such
#   that these invariants are guaranteed:
#       >>> range(min, max).start == min
#       True
#       >>> range(min, max).stop == max
#       True

_ARGS_LEN_0 = range(0, 1)  # == [0, 1) == [0, 0]
'''
**Zero-argument length range** (i.e., :class:`range` instance effectively
equivalent to the integer ``0``, describing type hint factories subscriptable by
*no* child type hints).
'''


_ARGS_LEN_1 = range(1, 2)  # == [1, 2) == [1, 1]
'''
**One-argument length range** (i.e., :class:`range` instance effectively
equivalent to the integer ``1``, describing type hint factories subscriptable by
exactly one child type hint).
'''


_ARGS_LEN_2 = range(2, 3)  # == [2, 3) == [2, 2]
'''
**Two-argument length range** (i.e., :class:`range` instance effectively
equivalent to the integer ``2``, describing type hint factories subscriptable by
exactly two child type hints).
'''


_ARGS_LEN_3 = range(3, 4)  # == [3, 4) == [3, 3]
'''
**Three-argument length range** (i.e., :class:`range` instance effectively
equivalent to the integer ``3``, describing type hint factories subscriptable by
exactly three child type hints).
'''


_ARGS_LEN_1_OR_2 = range(1, 3)  # == [1, 3) == [1, 2]
'''
**One- or two-argument length range** (i.e., :class:`range` instance effectively
equivalent to the integer range ``[1, 2]``, describing type hint factories
subscriptable by either one or two child type hints).
'''

# ....................{ SIGNS ~ origin : args              }....................
# Fully initialized by the _init() function below.
HINT_SIGN_ORIGIN_ISINSTANCEABLE_TO_ARGS_LEN_RANGE: Dict[HintSign, range] = {
    # Type hint factories subscriptable by exactly one child type hint.
    HintSignAbstractSet: _ARGS_LEN_1,
    HintSignAsyncIterable: _ARGS_LEN_1,
    HintSignAsyncIterator: _ARGS_LEN_1,
    HintSignAwaitable: _ARGS_LEN_1,
    # HintSignByteString: _ARGS_LEN_1,
    HintSignCollection: _ARGS_LEN_1,
    HintSignContainer: _ARGS_LEN_1,
    HintSignCounter: _ARGS_LEN_1,
    HintSignDeque: _ARGS_LEN_1,
    HintSignFrozenSet: _ARGS_LEN_1,
    HintSignIterable: _ARGS_LEN_1,
    HintSignIterator: _ARGS_LEN_1,
    HintSignKeysView: _ARGS_LEN_1,
    HintSignList: _ARGS_LEN_1,
    HintSignMatch: _ARGS_LEN_1,
    HintSignMappingView: _ARGS_LEN_1,
    HintSignMutableSequence: _ARGS_LEN_1,
    HintSignMutableSet: _ARGS_LEN_1,
    HintSignPattern: _ARGS_LEN_1,
    HintSignReversible: _ARGS_LEN_1,
    HintSignSequence: _ARGS_LEN_1,
    HintSignSet: _ARGS_LEN_1,
    HintSignType: _ARGS_LEN_1,
    HintSignValuesView: _ARGS_LEN_1,

    # Type hint factories subscriptable by exactly two child type hints.
    HintSignAsyncGenerator: _ARGS_LEN_2,
    HintSignChainMap: _ARGS_LEN_2,
    HintSignDefaultDict: _ARGS_LEN_2,
    HintSignDict: _ARGS_LEN_2,
    HintSignItemsView: _ARGS_LEN_2,
    HintSignMapping: _ARGS_LEN_2,
    HintSignMutableMapping: _ARGS_LEN_2,
    HintSignOrderedDict: _ARGS_LEN_2,
    HintSignTuple: _ARGS_LEN_2,

    # Type hint factories subscriptable by exactly three child type hints.
    HintSignCoroutine: _ARGS_LEN_3,
    HintSignGenerator: _ARGS_LEN_3,
}
'''
Dictionary mapping from each sign uniquely identifying a PEP-compliant type hint
factory originating from an **isinstanceable origin type** (i.e., isinstanceable
class such that *all* objects satisfying type hints created by subscripting this
factory are instances of this class) to this factory's **argument length range**
(i.e., :class:`range` instance describing the minimum and maximum number of
child type hints that may subscript this factory).
'''

# ....................{ PRIVATE ~ main                     }....................
def _init() -> None:
    '''
    Initialize this submodule.
    '''

    # Defer function-specific imports.
    from beartype._util.py.utilpyversion import IS_PYTHON_AT_LEAST_3_13

    # If the active Python interpreter targets Python >= 3.13...
    if IS_PYTHON_AT_LEAST_3_13:
        # Add all signs uniquely identifying one- or two-argument type hint
        # factories under Python >= 3.13, which generalized various one-argument
        # type hint factories to accept an additional optional child type hint
        # via "PEP 696 โ€“ Type Defaults for Type Parameters".
        HINT_SIGN_ORIGIN_ISINSTANCEABLE_TO_ARGS_LEN_RANGE.update({
            HintSignAsyncContextManager: _ARGS_LEN_1_OR_2,
            HintSignContextManager: _ARGS_LEN_1_OR_2,
        })
    # Else, the active Python interpreter targets Python <= 3.12. In this
    # case...
    else:
        # Add all signs uniquely identifying two-argument type hint factories
        # under Python <= 3.12.
        HINT_SIGN_ORIGIN_ISINSTANCEABLE_TO_ARGS_LEN_RANGE.update({
            HintSignAsyncContextManager: _ARGS_LEN_1,
            HintSignContextManager: _ARGS_LEN_1,
        })

As the above code suggests, the standard contextlib.AsyncContextManager and contextlib.ContextManager type 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 tuple and Union, 1 for other iterables like list, set, and 2 for mappings like dict.

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 Union matching all types! Iโ€™m just going to return True if 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.

typing sure is super-weird, huh? You have now noticed that deprecated PEP 484-compliant typing type hint factories behave nothing like PEP 585-compliant builtin type hint factories. Behold! True horror!

>>> from typing import List, Union
>>> list[Union]
list[Union]  # <-- guess that's okay, i guess? thanks, pep 585!
>>> List[Union]
...
TypeError: Plain typing.Union is not valid as type argument
# ^-- thanks fer nuthin', pep 484.

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:

  • Any type hint factory can be unsubscripted.
  • When unsubscripted, a type hint factory defaults to subscription by the typing.Any singleton.

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]โ€ฆ?

โ•ญโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ Traceback (most recent call last) โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฎ
โ”‚ in <module>                                                    โ”‚
โ”‚ โ•ญโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ locals โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฎ โ”‚
โ”‚ โ”‚ install = <function install at 0x76ff574ea0>               โ”‚ โ”‚
โ”‚ โ”‚  typing = <module 'typing' from                            โ”‚ โ”‚
โ”‚ โ”‚           '/data/data/com.termux/files/usr/lib/python3.11โ€ฆ โ”‚ โ”‚
โ”‚ โ•ฐโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฏ โ”‚
โ”‚                                                                โ”‚
โ”‚ /data/data/com.termux/files/usr/lib/python3.11/typing.py:344   โ”‚
โ”‚ in inner                                                       โ”‚
โ”‚                                                                โ”‚
โ”‚    341 โ”‚   โ”‚   โ”‚   โ”‚   return cached(*args, **kwds)            โ”‚
โ”‚    342 โ”‚   โ”‚   โ”‚   except TypeError:                           โ”‚
โ”‚    343 โ”‚   โ”‚   โ”‚   โ”‚   pass  # All real errors (not unhashable โ”‚
โ”‚ โฑ  344 โ”‚   โ”‚   โ”‚   return func(*args, **kwds)                  โ”‚
โ”‚    345 โ”‚   โ”‚   return inner                                    โ”‚
โ”‚    346 โ”‚                                                       โ”‚
โ”‚    347 โ”‚   if func is not None:                                โ”‚
โ”‚                                                                โ”‚
โ”‚ โ•ญโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ locals โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฎ โ”‚
โ”‚ โ”‚   args = (typing.List, typing.Union)                       โ”‚ โ”‚
โ”‚ โ”‚ cached = <functools._lru_cache_wrapper object at           โ”‚ โ”‚
โ”‚ โ”‚          0x76ff4d1380>                                     โ”‚ โ”‚
โ”‚ โ”‚   func = <function _SpecialGenericAlias.__getitem__ at     โ”‚ โ”‚
โ”‚ โ”‚          0x76ff562f20>                                     โ”‚ โ”‚
โ”‚ โ”‚   kwds = {}                                                โ”‚ โ”‚
โ”‚ โ•ฐโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฏ โ”‚
โ”‚                                                                โ”‚
โ”‚ /data/data/com.termux/files/usr/lib/python3.11/typing.py:1533  โ”‚
โ”‚ in __getitem__                                                 โ”‚
โ”‚                                                                โ”‚
โ”‚   1530 โ”‚   โ”‚   if not isinstance(params, tuple):               โ”‚
โ”‚   1531 โ”‚   โ”‚   โ”‚   params = (params,)                          โ”‚
โ”‚   1532 โ”‚   โ”‚   msg = "Parameters to generic types must be type โ”‚
โ”‚ โฑ 1533 โ”‚   โ”‚   params = tuple(_type_check(p, msg) for p in par โ”‚
โ”‚   1534 โ”‚   โ”‚   _check_generic(self, params, self._nparams)     โ”‚
โ”‚   1535 โ”‚   โ”‚   return self.copy_with(params)                   โ”‚
โ”‚   1536                                                         โ”‚
โ”‚                                                                โ”‚
โ”‚ โ•ญโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ locals โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฎ      โ”‚
โ”‚ โ”‚    msg = 'Parameters to generic types must be types.' โ”‚      โ”‚
โ”‚ โ”‚ params = (typing.Union,)                              โ”‚      โ”‚
โ”‚ โ”‚   self = typing.List                                  โ”‚      โ”‚
โ”‚ โ•ฐโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฏ      โ”‚
โ”‚                                                                โ”‚
โ”‚ /data/data/com.termux/files/usr/lib/python3.11/typing.py:1533  โ”‚
โ”‚ in <genexpr>                                                   โ”‚
โ”‚                                                                โ”‚
โ”‚   1530 โ”‚   โ”‚   if not isinstance(params, tuple):               โ”‚
โ”‚   1531 โ”‚   โ”‚   โ”‚   params = (params,)                          โ”‚
โ”‚   1532 โ”‚   โ”‚   msg = "Parameters to generic types must be type โ”‚
โ”‚ โฑ 1533 โ”‚   โ”‚   params = tuple(_type_check(p, msg) for p in par โ”‚
โ”‚   1534 โ”‚   โ”‚   _check_generic(self, params, self._nparams)     โ”‚
โ”‚   1535 โ”‚   โ”‚   return self.copy_with(params)                   โ”‚
โ”‚   1536                                                         โ”‚
โ”‚                                                                โ”‚
โ”‚ โ•ญโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ locals โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฎ         โ”‚
โ”‚ โ”‚  .0 = <tuple_iterator object at 0x76febbe5f0>      โ”‚         โ”‚
โ”‚ โ”‚ msg = 'Parameters to generic types must be types.' โ”‚         โ”‚
โ”‚ โ”‚   p = typing.Union                                 โ”‚         โ”‚
โ”‚ โ•ฐโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฏ         โ”‚
โ”‚                                                                โ”‚
โ”‚ /data/data/com.termux/files/usr/lib/python3.11/typing.py:195   โ”‚
โ”‚ in _type_check                                                 โ”‚
โ”‚                                                                โ”‚
โ”‚    192 โ”‚   if allow_special_forms and arg in (ClassVar, Final) โ”‚
โ”‚    193 โ”‚   โ”‚   return arg                                      โ”‚
โ”‚    194 โ”‚   if isinstance(arg, _SpecialForm) or arg in (Generic โ”‚
โ”‚ โฑ  195 โ”‚   โ”‚   raise TypeError(f"Plain {arg} is not valid as t โ”‚
โ”‚    196 โ”‚   if type(arg) is tuple:                              โ”‚
โ”‚    197 โ”‚   โ”‚   raise TypeError(f"{msg} Got {arg!r:.100}.")     โ”‚
โ”‚    198 โ”‚   return arg                                          โ”‚
โ”‚                                                                โ”‚
โ”‚ โ•ญโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ locals โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฎ โ”‚
โ”‚ โ”‚   allow_special_forms = False                              โ”‚ โ”‚
โ”‚ โ”‚                   arg = typing.Union                       โ”‚ โ”‚
โ”‚ โ”‚ invalid_generic_forms = (                                  โ”‚ โ”‚
โ”‚ โ”‚                         โ”‚   <class 'typing.Generic'>,      โ”‚ โ”‚
โ”‚ โ”‚                         โ”‚   <class 'typing.Protocol'>,     โ”‚ โ”‚
โ”‚ โ”‚                         โ”‚   typing.ClassVar,               โ”‚ โ”‚
โ”‚ โ”‚                         โ”‚   typing.Final                   โ”‚ โ”‚
โ”‚ โ”‚                         )                                  โ”‚ โ”‚
โ”‚ โ”‚           is_argument = True                               โ”‚ โ”‚
โ”‚ โ”‚                module = None                               โ”‚ โ”‚
โ”‚ โ”‚                   msg = 'Parameters to generic types must  โ”‚ โ”‚
โ”‚ โ”‚                         be types.'                         โ”‚ โ”‚
โ”‚ โ•ฐโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฏ โ”‚
โ•ฐโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฏ
TypeError: Plain typing.Union is not valid as type argument

Oh no.

โ€” 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:

โ€œWHAT YOU SAY!?!?โ€

Yes. Unsubscripted type hint factories like typing.Optional and typing.Union are 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 by typing.Any. Ergo:

  • typing.Union implicitly expands to typing.Union[typing.Any]. However, PEP 484 also stipulates that a union of one type semantically reduces to only that type! Since this implies that typing.Union[typing.Any] semantically reduces to merely typing.Any, the unsubscripted type factory typing.Union reduces to typing.Any as well.
  • Similar logic applies to typing.Optional, which behaves similarly.

Donโ€™t shoot the QA messenger. I donโ€™t make the QA-destroying typing standards. I only ineffectually complain about the QA-destroying typing standards 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. ๐Ÿ˜ญ
  • 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. ๐Ÿ˜น