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

[Feature Request] Generalize beartype.door.{is_bearable,die_if_unbearable} to dynamically resolve relative forward references, even if doing so is slower than watching paint dry 😄 #364

Open
uriyasama opened this issue Apr 11, 2024 · 2 comments

Comments

@uriyasama
Copy link

Starting with the latest beartype version I started getting this error:

beartype.roar.BeartypeDecorHintForwardRefException: Die_if_unbearable() type hint typing.Dict[str, ForwardRef('XXXX')] type hint relative forward reference "XXXX" currently only type-checkable in type hints annotating @beartype-decorated callables and classes. For your own safety and those of the codebases you love, consider canonicalizing this relative forward reference into an absolute forward reference (e.g., replace "XXXX" with "{your_package}.{your_submodule}.XXXX").

The suggested solution to this error is to change the format of our ForwardRefs. Sadly not all ForwardRefs are inside our codebase, and this is highly limiting.

I suggest Adding a "local_classes" param to beartype.door functions which maps ForwardRef string to the class itself.

Handcrafted example where this issue arises.

from typing import Dict
from dataclasses import dataclass

@dataclass
class Settings:
    overrides: Dict[str, "Settings"]

if __name__ == "__main__":
    a = {"hello": Settings(overrides={})}
    die_if_unbearable(a, Dict[str, "Settings"])```
    
This ticket follows #356 
@leycec
Copy link
Member

leycec commented Apr 12, 2024

Totally right, @uriyasama. That said:

Starting with the latest beartype version I started getting this error:

Really? Wild. The beartype.door.is_bearable() and beartype.door.die_if_unbearable() functions should always have been raising this exception. If older @beartype versions failed to raise this exception, that's somehow worse; these functions were silently bugged and definitely not doing what everybody thought they were doing. That is the worst of all possible worlds.

In any case, this is terribad! I've been meaning to generalize these functions to support relative forward references for a few years now – but nobody ever complained. So, I did nothing instead. Also...

I suggest Adding a "local_classes" param to beartype.door functions which maps ForwardRef string to the class itself.

Excellent suggestion! Thankfully, @beartype can do much better than that. By the non-portable black magic of call stack introspection, @beartype can dynamically crawl up the call stack until it finds the class referenced by each relative forward reference. This isn't so much hard as it sloooooooooow. But code that works slowly beats code that doesn't work at all. 🥲

Thanks for complaining. Without complaints, I just play video games. That's bad for @beartype and it's bad for your codebase.

@leycec leycec changed the title [Feature Request] better support beartype.door functions for ForwardRef [Feature Request] Generalize beartype.door.{is_bearable,die_if_unbearable} to dynamically resolve relative forward references Apr 12, 2024
@leycec leycec changed the title [Feature Request] Generalize beartype.door.{is_bearable,die_if_unbearable} to dynamically resolve relative forward references [Feature Request] Generalize beartype.door.{is_bearable,die_if_unbearable} to dynamically resolve relative forward references, even if doing so is slower than watching paint dry Apr 12, 2024
@leycec leycec changed the title [Feature Request] Generalize beartype.door.{is_bearable,die_if_unbearable} to dynamically resolve relative forward references, even if doing so is slower than watching paint dry [Feature Request] Generalize beartype.door.{is_bearable,die_if_unbearable} to dynamically resolve relative forward references, even if doing so is slower than watching paint dry 😄 Apr 12, 2024
@uriyasama
Copy link
Author

uriyasama commented Apr 15, 2024

@leycec Perfect!
But if I knew you were busy video-gaming I wouldn't have bothered you :)

Yeah your idea sound pretty resilient, and slower than honey.

Regarding this issue appearing only following 0.18.0, this is because starting that version you are deep checking dictionaries.
So our forwardRef case Dict[str, "SomethingSomethingSomething"] started failing only after that

@uriyasama uriyasama reopened this Apr 15, 2024
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