`beartype`d modules with forward references can't be `importlib.reload`ed
See original GitHub issueThis is probably a super esoteric use case, but I stumbled across this (quite on accident):
# karma.py
from __future__ import annotations
from beartype import beartype
class Chameleon:
def __init__(self, colors: str):
self.colors = colors
@classmethod
@beartype
def like_my_dreams(cls) -> Chameleon:
return Chameleon("red, gold, and green")
# test_karma.py
import unittest
class TestReloadBearTyped(unittest.TestCase):
def test_1_imported(self) -> None:
import karma
assert karma.Chameleon.like_my_dreams().colors == "red, gold, and green"
def test_2_reloaded(self) -> None:
import karma
from importlib import reload
karma = reload(karma) # <-- this pulls the rug out from under the bear (comment this out and all tests pass)
assert karma.Chameleon.like_my_dreams().colors == "red, gold, and green"
def test_3_imported_again(self) -> None:
self.test_1_imported() # <-- once the beartyped module is reloaded in the prior test, this fails too
if __name__ == "__main__":
unittest.main()
Results:
% python test_karma.py
.EE
======================================================================
ERROR: test_2_reloaded (__main__.TestReloadBearTyped)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/…/test_karma.py", line 14, in test_2_reloaded
assert karma.Chameleon.like_my_dreams().colors == "red, gold, and green"
File "<string>", line 18, in like_my_dreams
File "/…/lib/python3.9/site-packages/beartype/_decor/_error/errormain.py", line 316, in raise_pep_call_exception
raise exception_cls( # type: ignore[misc]
beartype.roar.BeartypeCallHintPepReturnException: @beartyped Chameleon.like_my_dreams() return <karma.Chameleon object at 0x105c30eb0> violates type hint <class 'karma.Chameleon'>, as <karma.Chameleon object at 0x105c30eb0> not <class "karma.Chameleon">.
======================================================================
ERROR: test_3_imported_again (__main__.TestReloadBearTyped)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/…/test_karma.py", line 17, in test_3_imported_again
self.test_1_imported()
File "/…/test_karma.py", line 8, in test_1_imported
assert karma.Chameleon.like_my_dreams().colors == "red, gold, and green"
File "<string>", line 18, in like_my_dreams
File "/…/lib/python3.9/site-packages/beartype/_decor/_error/errormain.py", line 316, in raise_pep_call_exception
raise exception_cls( # type: ignore[misc]
beartype.roar.BeartypeCallHintPepReturnException: @beartyped Chameleon.like_my_dreams() return <karma.Chameleon object at 0x105c30ee0> violates type hint <class 'karma.Chameleon'>, as <karma.Chameleon object at 0x105c30ee0> not <class "karma.Chameleon">.
----------------------------------------------------------------------
Ran 3 tests in 0.043s
FAILED (errors=2)
Issue Analytics
- State:
- Created 2 years ago
- Reactions:1
- Comments:13 (7 by maintainers)
Top Results From Across the Web
Have importlib.reload() raise ModuleNotFoundError when a ...
Second, the issue is that you're trying to import a module under a name which doesn't match the file specified. That's causing reload()...
Read more >Forward references across modules - python - Stack Overflow
As I recently suggested you can use TYPE_CHECKING variable: # a.py from typing import TYPE_CHECKING if TYPE_CHECKING: from .b import B class ...
Read more >Have importlib.reload() raise ModuleNotFoundError when a ...
Second, the issue is that you're trying to import a module under a name which doesn't match the file specified. That's causing reload()...
Read more >beartype - PyPI
Beartype is an open-source PEP-compliant constant-time pure-Python runtime type checker emphasizing efficiency, usability, and thrilling puns.
Read more >Compare Packages Between Distributions - DistroWatch.com
In this way we can not only compare two competing projects, but also track the progress of ... ansible-modules-hashivault 4.6.8 ... Apache-Reload 0.130.0...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
My heart soars like a hawk at seeing ol’ Chad featured so prominently. I just hope it doesn’t go to his head too quickly. Fame is a fickle mistress.
Can’t … breathe … . 🤣 OMG, this is priceless. Tragic. But priceless.