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.

[Feature Request] validate a given object against a type annotation as a function

See original GitHub issue

Really 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:closed
  • Created 2 years ago
  • Reactions:3
  • Comments:10 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
rsoklcommented, Jan 7, 2022

+1 for this feature! I will almost certainly have use for this in hydra-zen down the line.

1reaction
qiujiangkuncommented, Jan 1, 2022

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 of pydantic, 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 with typedmodel, 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.

Read more comments on GitHub >

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

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