New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Runtime type validation? #174
Comments
Hi @crypdick , Typeguard seems like a good solution for the use case when all you want is to check the type -- can you maybe elaborate why you wouldn't use it? (You might also want to check if it supports subtyping properly.) |
@mristin we make extensive use of |
@crypdick I personally don't find the following snippet too terrible:
You can also use material implication if you need to condition constraints on the type:
You can also alias isinstance simply with is_a to abbreviate it:
but I would personally recommend readability over brevity and avoid abbreviations. |
Thanks @mristin. One issue with that solution is that it violates the DRY principle -- we're already type hinting our args, and we'd have to maintain consistency with the types again in each decorator. It's also a ton of boilerplate: I can slap a |
@crypdick I see -- I missed that part that you already do type annotations but want them enforced at runtime. I think that a decorator like Apart from depending on two libraries instead of one, do I understand it correctly that there are actually no other blockers to using icontract and typechecked in conjunction? Please do check if typechecked supports subtyping correctly in your use cases. If it does not, that would be a strong case for its adoption in icontract. I could come up with an extension given a couple of concrete use cases (which I'd also like to use in testing). |
I am not sure, I've never used icontract.
I am not sure if this qualifies, but we use PyDantic dataclasses and they play nicely with typeguard. |
Update: I tested icontract with typeguard and I haven't found blockers. |
Hi @crypdick , Should we close the issue or there are still open points? |
@mristin I think the explicit tests are great. Thanks! |
We are assessing a switch from typeguard to icontract for stricter runtime validation. However, I don't see anything akin to typeguard's @typechecked decorator. Correct me if I'm wrong, is the only way to do runtime type validation by making an
@icontract.require(lambda arg: isinstance(arg, <type>)
for all args?The text was updated successfully, but these errors were encountered: