Skip to content

Q: Can @beartype check annotated variable assignments like foo: list[str] = [42, 24]? A: Nopers. beartype_this_package(), instead. Never @beartype. Just never. #315

Answered by leycec
reikdas asked this question in Q&A
Discussion options

You must be logged in to vote

Excellent question! The tl;dr (i.e., short answer) is that you probably want to do this instead:

# In the "{your_package}.__init__" submodule:
from beartype.claw import beartype_this_package
beartype_this_package()

And then...

# In the "{your_package}.__main__" submodule (or whatevahs):
from typing import Annotated
from annotated_types import Gt
from beartype.vale import Is

def main():
    foo: list[Annotated[int, Is[Gt(0)]]] = [-1, 0, 10]
    print(foo)

main()

You should now receive joy. You may also be thinking to yourself:

"This is balls. I hate this. What is happening here?"

Boring Stuff Follows: It Is Boring

Allow me to now boringly explicate.

foo: list[Annotated[int, Is[Gt(0)]]]…

Replies: 3 comments 2 replies

Comment options

You must be logged in to vote
1 reply
@reikdas
Comment options

Answer selected by reikdas
Comment options

You must be logged in to vote
1 reply
@reikdas
Comment options

Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants