Rock Solid Python with Python Typing Transcripts
Chapter: Tools Based on Typing
Lecture: Runtime Type Checking with Beartype
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
So far, throughout almost all of this course, what we've seen is that Python type hints are gradual and optional.
0:10
The exception being Pydantic, basically, that says if something's wrong, we're going to use that information at run time.
0:16
But generally what you see when we add type hints, that's for mypy, that's for the editor, that's for the person reading the code,
0:24
not so much for enforcing. In a static language, remember we saw several examples of those at the start, if we compile
0:32
C#, C++, Swift, Dart, all of those, they check, make sure that things go together before it'll
0:40
even build the program, and unless the types line up correctly every time, it won't even build the program, which means you can't run it at all.
0:50
Python typing is not that way. Python typing allows you to work through these issues and it gives you a sense of what it should do.
1:00
But if it doesn't do that, you know, we gave it a good shot. What if you want to have that level of verifiability?
1:08
Maybe not at compile time in the closest you'll get with that, I guess is my pie, but at runtime
1:14
to make sure that things at runtime always work and always hold together kind of like a compiled language.
1:21
Meet the bear, bear type, the bear metal type checker. So bear type is a pretty interesting project. It's really easy to use.
1:30
You just annotate your code with typements as you have been throughout this course and then you add a decorator
1:37
and those hints become runtime type enforcements. Now, Pydantic had this thing called validators and they had a runtime type of validator thing.
1:47
I'm not sure if it ever made it out of beta. out of beta, it was in beta for a long time. So this one is super fast and super interesting.
1:54
Let's dive into it.