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.

`beartype`d modules with forward references can't be `importlib.reload`ed

See original GitHub issue

This 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)

Juggling Bear

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:13 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
positacommented, Oct 28, 2021

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.

1reaction
positacommented, Sep 25, 2021

Oh, no! It was great fun, actually. I was reminded nostalgically of dead-tree logic puzzle books I repetitiously solved like a child automaton in the fecund back seat of the family car during purgatorial car trip vacations across the vast featureless void of the American Midwest. Little did I know, but I was training then for a promising future in… type-checking. </chuckles>

Can’t … breathe … . 🤣 OMG, this is priceless. Tragic. But priceless.

Read more comments on GitHub >

github_iconTop 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 >

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