5 ms·
Type 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,
by dharmab 5y ago
Type 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.