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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

| causes error while typing.Union works #111

Closed
twoertwein opened this issue Mar 11, 2022 · 2 comments
Closed

| causes error while typing.Union works #111

twoertwein opened this issue Mar 11, 2022 · 2 comments

Comments

@twoertwein
Copy link

I just moved to python 3.10. The following code works when using typing.Union, but when replacing it with | it fails

from collections.abc import Callable
from typing import Union

from beartype import beartype


class Test:
    @beartype
    def __init__(
        self,
        test: Callable[[int, int], int] | str = ...,  # fails
        # test: Union[Callable[[int, int], int], str] = ..., # works
    ):
        ...
...
  File "/pool01/home/twoertwe/.cache/pypoetry/virtualenvs/python-tools-sgDWaTGJ-py3.10/lib/python3.10/site-packages/beartype/_decor/main.py", line 149, in beartype
    func_wrapper_code = generate_code(func_data)
  File "/pool01/home/twoertwe/.cache/pypoetry/virtualenvs/python-tools-sgDWaTGJ-py3.10/lib/python3.10/site-packages/beartype/_decor/_code/codemain.py", line 166, in generate_code
    code_check_params = _code_check_params(data)
  File "/pool01/home/twoertwe/.cache/pypoetry/virtualenvs/python-tools-sgDWaTGJ-py3.10/lib/python3.10/site-packages/beartype/_decor/_code/codemain.py", line 373, in _code_check_params
    func_wrapper_code_param = pep_code_check_param(
  File "/pool01/home/twoertwe/.cache/pypoetry/virtualenvs/python-tools-sgDWaTGJ-py3.10/lib/python3.10/site-packages/beartype/_decor/_code/_pep/pepcode.py", line 149, in pep_code_check_param
    reraise_exception_cached(
  File "/pool01/home/twoertwe/.cache/pypoetry/virtualenvs/python-tools-sgDWaTGJ-py3.10/lib/python3.10/site-packages/beartype/_util/cache/utilcacheerror.py", line 200, in reraise_exception_cached
    raise exception.with_traceback(exception.__traceback__)
  File "/pool01/home/twoertwe/.cache/pypoetry/virtualenvs/python-tools-sgDWaTGJ-py3.10/lib/python3.10/site-packages/beartype/_decor/_code/_pep/pepcode.py", line 132, in pep_code_check_param
    ) = pep_code_check_hint(hint)
  File "/pool01/home/twoertwe/.cache/pypoetry/virtualenvs/python-tools-sgDWaTGJ-py3.10/lib/python3.10/site-packages/beartype/_util/cache/utilcachecall.py", line 351, in _callable_cached
    raise exception
  File "/pool01/home/twoertwe/.cache/pypoetry/virtualenvs/python-tools-sgDWaTGJ-py3.10/lib/python3.10/site-packages/beartype/_util/cache/utilcachecall.py", line 343, in _callable_cached
    return_value = params_flat_to_return_value[params_flat] = func(
  File "/pool01/home/twoertwe/.cache/pypoetry/virtualenvs/python-tools-sgDWaTGJ-py3.10/lib/python3.10/site-packages/beartype/_decor/_code/_pep/_pephint.py", line 908, in pep_code_check_hint
    cls=get_hint_pep_origin_type_isinstanceable(hint_curr),
  File "/pool01/home/twoertwe/.cache/pypoetry/virtualenvs/python-tools-sgDWaTGJ-py3.10/lib/python3.10/site-packages/beartype/_util/hint/pep/utilpepget.py", line 821, in get_hint_pep_origin_type_isinstanceable
    raise BeartypeDecorHintPepException(
beartype.roar.BeartypeDecorHintPepException: Type hint collections.abc.Callable[[int, int], int] | str not isinstanceable (i.e., does not originate from isinstanceable class).
@twoertwein
Copy link
Author

sorry, was using an old version of beartype (0.9.1), it works with 0.10.2

@leycec
Copy link
Member

leycec commented Mar 11, 2022

Almost had me anxiously dripping sweat everywhere there. 😅

leycec added a commit that referenced this issue Mar 12, 2022
This commit improves compatibility with recently released mypy 0.940,
resolving issues #111 and #112 dual-reported concurrently by
cutting-edge Microsoft luminary @daxpryce and German typing bad-ass
@twoertwein. Specifically, this commit fundamentally refactors (and in
so doing mildly optimizes) our private `beartype._decor.main` submodule
to leverage conditional overloads under the astute tutelage
of mypy maestro @cdce8p; the `@beartype.beartype` decorator itself now
resides in a new private
`beartype._decor.cache.decor.cachedecor` submodule, because obfuscation
is the key to all successful open-source efforts. Relatedly, this
commit also partially resolves issue #94 (kindly requested by typing
monster @matanster) by optimizing the definition of the
`@beartype.beartype` decorator when that decorator reduces to a noop
(e.g., due to `python3 -O` optimization). Unrelatedly, this commit also
resolves a critical defect in our new functional API (i.e., the pair of
`beartype.abby.is_bearable()` and `beartype.abby.die_if_unbearable()`
functions), which previously reduced to a noop when the
`@beartype.beartype` decorator reduced to a noop; extricating the
`@beartype.beartype` decorator into the
`beartype._decor.cache.decor.cachedecor` submodule enables our
functional API to unconditionally depend upon that decorator regardless
of what we present to external users. Thanks so much for the rapid
response time, everyone! `beartype 0.10.4` will be en-route to a
cheeseshop near you shortly. (*Incorrigibly durable didgeridoo!*)
leycec added a commit that referenced this issue Mar 15, 2022
This patch release adumbrates with breathless support for **mypy ≥
0.940,** the static type checker formerly known as "The Static Type
Checker Whose Name Shall not Be Spoken."

This patch release resolves **5 issues** and merges **0 pull requests.**
Noteworthy changes include:

## Compatibility Improved

* **mypy ≥ 0.940.** The `beartype` codebase now sports improved
  compatibility with recently released mypy 0.94x series, which
  previously outted the `@beartype` decorator with a "Condition can't be
  inferred, unable to merge overloads [misc]" fatal error at static
  type-checking time. Specifically, this commit fundamentally refactors
  (and in so doing mildly optimizes) our private `beartype._decor.main`
  submodule to leverage conditional overloads under the astute tutelage
  of mypy maestro @cdce8p; the `@beartype.beartype` decorator itself now
  resides in a new private `beartype._decor.cache.cachedecor` submodule,
  because obfuscation is the key to all successful open-source efforts.
  Doing so resolves issues #111 and #112 dual-reported concurrently by
  cutting-edge Microsoft luminary @daxpryce and German typing bad-ass
  @twoertwein.

## Features Optimized

* **Importation time.** The first external importation from the
  `beartype` codebase is now significantly faster when the definition of
  the `@beartype.beartype` decorator reduces to a noop (e.g., due to
  `python3 -O` optimization), partially resolves issue #94 kindly
  requested by the well-tanned and -toned typing star @matanster.

## Issues Resolved

* **`beartype.abby` under `python3 -O`.** This release resolves an
  unreported critical defect in our new functional API (i.e., the pair
  of `beartype.abby.is_bearable()` and
  `beartype.abby.die_if_unbearable()` functions), which previously
  reduced to a noop when the `@beartype.beartype` decorator reduced to a
  noop (e.g., due to `python3 -O` optimization). By extricating the
  `@beartype.beartype` decorator into the
  `beartype._decor.cache.cachedecor` submodule (as described above),
  our functional API now directly defers to that decorator regardless of
  what the `beartype` package externally presents to third-party code.

(*Ironwrought irony untaught!*)
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