[Feature Request] validate a given object against a type annotation as a function
See original GitHub issueReally love beartype. However, I couldn’t find the method for validating a given object against a type annotation. I have to use some tricks. Is this available somewhere?
def check_pep_type_raise_exception(obj, annotation):
import beartype.roar
import beartype
@beartype.beartype
def check(o) -> annotation:
return o
try:
check(obj)
reason = None
except beartype.roar.BeartypeCallHintPepReturnException as e:
reason = e.args[0].split('return ')[-1]
if reason:
raise TypeException(reason)
return True
Issue Analytics
- State:
- Created 2 years ago
- Reactions:3
- Comments:10 (5 by maintainers)
Top Results From Across the Web
Dynamic type validation in TypeScript - LogRocket Blog
As you can see, a new function has been defined, called validate , which receives as a parameter an ExampleType object, with which...
Read more >Validation of a list of objects in Spring - Stack Overflow
It's a working solution, but isn't spring violating JSR-303 which explicitly says Lists should be validated? Collection-valued, array-valued and generally ...
Read more >"satisfies" operator to ensure an expression matches some ...
I had to explain that by including the type annotation, they were asking the compiler to forget about the actual contents of the...
Read more >Validation with the Data Annotation Validators (C#)
In this tutorial, you learn how to use the Data Annotation validators to perform validation in an ASP.NET MVC application.
Read more >7. Validation, Data Binding, and Type Conversion - Spring
Spring features a Validator interface that you can use to validate objects. The Validator interface works using an Errors object so that while...
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
+1 for this feature! I will almost certainly have use for this in hydra-zen down the line.
Oh, the API sounds good enough for me. I’m implementing https://github.com/qiujiangkun/typedmodel with
beartype
as the only dependency, as a replacement ofpydantic
, but its type-validation is stupid and full of surprise, like treating type annotation standalone and in list differently, when standalone it raises an exception, but in a list, it converts something false negatively. Now withtypedmodel
, I have a similar experience as I’m working with Rust structs.I’m in a research group about compiler optimization, code generation, and type inference.