question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Feature Request] Generalize @beartype to support `class` decoration

See original GitHub issue

Apologies if this is a dup. This is interesting…

# test_case.py
from __future__ import annotations
from typing import Union
from beartype import beartype

class Test:
    @beartype
    def okay(self) -> Test:
        return Test()

    def okay_too(self, other: Union[int, str, Test]) -> Test:
        return Test()

    @beartype
    def also_okay(self, other: Union[int, str, "Test"]) -> Test:
        return Test()

    @beartype
    def also_also_okay(self, other: Union[int, str, "Test"]) -> "Test":
        return Test()

    # @beartype  # uncomment me for ...
    def a_kaboom(self, other: Union[int, str, Test]) -> Test:
        return Test()

    @beartype  # uncomment me for ...
    def another_kaboom(self, other: Union[int, str, Test]) -> "Test":
        return Test()

As-is:

% python --version  # also happens with 3.9.13
Python 3.10.5
% python -c 'import beartype ; print(beartype.__version__)'
0.10.4
% python test_case.py
[no output]

Now uncomment @beartype decorating a_kaboom:

% python test_case.py
Traceback (most recent call last):
  File "/…/lib/python3.10/site-packages/beartype/_decor/_pep/pep563.py", line 338, in resolve_hints_pep563_if_active
    func_hints_resolved[pith_name] = eval(
  File "<string>", line 1, in <module>
NameError: name 'Test' is not defined

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/…/test_case.py", line 6, in <module>
    class Test:
  File "/…/test_case.py", line 23, in Test
    def a_kaboom(self, other: Union[int, str, Test]) -> Test:
  File "/…/lib/python3.10/site-packages/beartype/_decor/cache/cachedecor.py", line 77, in beartype
    return beartype_args_mandatory(obj, conf)
  File "/…/lib/python3.10/site-packages/beartype/_decor/_core.py", line 140, in beartype_args_mandatory
    return _beartype_func(func=obj, conf=conf)
  File "/…/lib/python3.10/site-packages/beartype/_decor/_core.py", line 168, in _beartype_func
    func_data.reinit(func, conf)
  File "/…/lib/python3.10/site-packages/beartype/_decor/_call.py", line 285, in reinit
    resolve_hints_pep563_if_active(self)
  File "/…/lib/python3.10/site-packages/beartype/_decor/_pep/pep563.py", line 375, in resolve_hints_pep563_if_active
    raise BeartypeDecorHintPep563Exception(
beartype.roar.BeartypeDecorHintPep563Exception: @beartyped Test.a_kaboom() parameter "other" PEP 563-postponed type hint 'Union[int, str, Test]' syntactically invalid (i.e., "name 'Test' is not defined") under:
~~~~[ GLOBAL SCOPE ]~~~~
{'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <_frozen_importlib_external.SourceFileLoader object at 0x10c09d2a0>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>, '__file__': '/…/test_case.py', '__cached__': None, 'annotations': _Feature((3, 7, 0, 'beta', 1), (3, 11, 0, 'alpha', 0), 16777216), 'Union': typing.Union, 'beartype': <function beartype at 0x10c7a7250>}
~~~~[ LOCAL SCOPE  ]~~~~
{'__module__': '__main__', '__qualname__': 'Test', 'okay': <function Test.okay at 0x10c1284c0>, 'okay_too': <function Test.okay_too at 0x10c8c8e50>, 'also_okay': <function Test.also_okay at 0x10c8c9000>, 'also_also_okay': <function Test.also_also_okay at 0x10c8c9120>}

You’ll get something similar when uncommenting another_kaboom as well.

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:2
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
positacommented, Jul 27, 2022

"Oh..... SHIT"! {Carol Cleveland)

If it’s convenient, I’m happy to pretend I never saw this. 😉

Nothing to see here!

1reaction
leyceccommented, Aug 26, 2022

I feel it is my duty to declare your attempt to Locate Your Objective in your Delve of this Haunted Icereach as resolved with a Strong Hit. I also wish you luck in returning to your game of Call of Savage Dragon Worlds in the Dark Powered by Numenuma Fate™, 4th Edition by West Palladium Jackson Games®.

Holy dragon balls. I read that in the thick, gravel-throated intonation of Christopher Lee’s Saruman, who was also required by Jackson’s dubious legal counsel to slowly read out each of the ™ and ® Unicode symbols. Thanks so much for the morning chuckles. 😆

Read more comments on GitHub >

github_iconTop Results From Across the Web

[Feature Request] Human-readable exception messages for ...
...is to generalize @beartype to support decorating properties, class methods, and static methods in any arbitrary decorator order. Everyone was right!
Read more >
Beartype: Unbearably fast O(1) runtime type-checking in pure ...
Beartype thus performs one check for each possibly nested type hint for each annotated parameter or return object for each call to each...
Read more >
Python's “type hints” are a bit of a disappointment to me
Honestly, I've recently written a few 300-500 line programs in Python using type hints and I'm never going back. And I'm not even...
Read more >
beartype/beartype v0.11.0 on GitHub - NewReleases.io
Class decoration. The @beartype decorator now decorates both higher-level classes and lower-level callables (i.e., functions, methods), resolving feature ...
Read more >
beartype: Versions - Openbase
Class decoration. The @beartype decorator now decorates both higher-level classes and lower-level callables (i.e., functions, methods), resolving feature ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found