4 ms·
Could someone explain the benefit of runtime type checking? I thought the whole point of type checking was to catch errors before runtime. If it's already runn
by dec0dedab0de 5y ago
Could someone explain the benefit of runtime type checking? I thought the whole point of type checking was to catch errors before runtime. If it's already running, wouldn't it be better to just let it keep running and see what happens?
Is the idea that you would run this during your tests and it would give you more insight than a static type checker?
EDIT:
The answer is in the FAQ https://github.com/beartype/beartype#frequently-asked-questions-faq https://github.com/beartype/beartype#frequently-asked-questi...
It seems like it would never cause an error, and it's really for type checking dynamic code. I'm still trying to wrap my head around it, but it seems promising so far.
- dharmab 5y agoType hints aren't actually enforced in Python. So for example a third party can import your code and call functions with the wrong type. Because of duck typing, sometimes this won't result in an error but could still cause a bug. You can explicitly check types with isinstance(), this library automates that.
- dec0dedab0de 5y agoType hints aren't actually enforced in Python. So for example a third party can import your code and call functions with the wrong type. Because of duck typing, sometimes this won't result in an error but could still cause a bug. Sure, but it also might work, if you've already made it through testing and deployment, surely you don't want to cause a new production bug because there might be another bug. What I would really want at runtime is something that automatically casts and validates. As in, I don't care if it's a string, I care that if I wrap it in str it matches this regex.
- mixmastamyk 5y agoFinding bugs as early as possible, rather than soldiering on, is most often what you want. The reason is that the farther the code executes, the details and error messages of what went wrong in the past can get fuzzier and harder to diagnose. If you're doing ducktyping... it should be specified explicitly, believe Python typing calls it a "protocol."
- ensignavenger 5y ago> something that automatically casts and validates This is what Pydantic does.
- dharmab 5y agoI've been using Pydantic to validate inputs for one of my projects and really like it.