type checking decorator
See original GitHub issueDream Feature:
Using pydantic to check types without building Models or dataclasses.
Four modes of usage:
1. decorators
@typecheck
def foobar(x: int, y: float, x: List[Dict[str, Tuple[int, int]]]):
...
The typecheck
decorator would check types when foobar
is called, optional arguments for the decorator to:
- check x percent of the time
- what to do on an error: exception, warning, logging etc.
- whether to coerce types or just check them
(Usage: at library boundaries to guarentee correct usage)
2 context manager
A context manager which uses sys.settrace
to check types on all function calls inside your code base.
This won’t be at all easy and will slow down code execution a lot, but would be a big win. (I think?)
Would need to with with existing trace functions eg. coverage and allow that to still work properly.
Also the arguments passed would need to be checked but not modified so behavior isn’t changed.
(usage: see below)
3 pytest extension
which uses the context manager above to do runtime checks during testing.
4 custom python “executable”
use runpy.run_path
to run a python program inside the context manager above and therefore with type checks.
(probably most useful as a sanity check or or adhock checks)
Is this something anyone would fine useful?
The settrace
/pytest extension approach might end up being infeasible, which would just leave the decorator.
Issue Analytics
- State:
- Created 5 years ago
- Reactions:3
- Comments:12 (9 by maintainers)
I think that’s fair.
decorator usage would be useful and I think falls into the same broad case as pydantic’s other applications.
So let’s do the decorator case but not the others.
I am at last working on this on #1179, feedback welcome.