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.

Is an equivalent of `typeguard.check_type` available?

See original GitHub issue

I’d like to use Beartype’s custom PEP-compliant validators, but my use case requires that I make an explicit call to a function similar to typeguard.check_type(). It doesn’t seem like beartype is currently designed to expose a method like this; the decorators are all I can find. How hard would it be to expose a method like this? If I’m given some general guidance I’m happy to attempt a PR for it myself.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:10 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
TeamSpen210commented, Feb 2, 2022

That’s intentional. To allow for fast constant-time type checking, beartype doesn’t exhaustively check the entirety of lists and other collections. Instead each time it’s called, it randomly picks a position and checks it. Most of the time, you don’t tend to have lists like your example with only a few bad items, it’s usually entirely the wrong type so this will catch it immediately. If there are incorrect values, repeatedly calling will eventually produce a typecheck failure.

However this might not be a desirable behaviour for an explicit type-checking function, so it’d probably be better to default to the O(n) mode described here, once implemented: #60

0reactions
leyceccommented, Feb 3, 2022

However this might not be a desirable behaviour for an explicit type-checking function…

You are not wrong. You never are. You know this already, which is why I’m generalizing the signature of our currently implemented and thoroughly untested is_bearable() tester to read:

def is_bearable(
    obj: object, hint: object,
    *, conf: BeartypeConf = BeartypeConf()
):

For orthogonality and (more importantly) 'cause this is @beartype, conf will probably default to BeartypeConf() which defaults to our default BeartypeStrategy.O1 strategy. Gotta type-check in constant time if you’re gonna keep up with the big boys!

Now, let’s see if I actually do something and manage to test any of this. The sweat pouring off my chin is real.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Documentation - Advanced Types - TypeScript
A type guard is some expression that performs a runtime check that guarantees the type in some scope. Using type predicates. To define...
Read more >
User guide — Typeguard 2.13.3 documentation - Read the Docs
The simplest way to type checking of both argument values and the return value for a single function is to use the @typechecked...
Read more >
How to get the types you want with TypeScript type guards
TypeScript comes with some built-in type guards: typeof and instanceof . They're very useful, but have limited scope. For example, typeof can ...
Read more >
Typescript check object by type or interface at runtime with ...
And i was thinking, can this be done with user defined typeguard in a generic way for any type of object with multiple...
Read more >
How to use type guards in TypeScript - LogRocket Blog
A type guard is a TypeScript technique used to get information about the type of a variable, usually within a conditional block.
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