question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

type checking decorator

See original GitHub issue

Dream 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:closed
  • Created 5 years ago
  • Reactions:3
  • Comments:12 (9 by maintainers)

github_iconTop GitHub Comments

4reactions
samuelcolvincommented, Jan 22, 2019

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.

3reactions
samuelcolvincommented, Jan 19, 2020

I am at last working on this on #1179, feedback welcome.

Read more comments on GitHub >

github_iconTop Results From Across the Web

prechelt/typecheck-decorator: For Python3, e.g. ... - GitHub
A decorator for functions, @tc.typecheck , to be used together with Python3 annotations on function parameters and function results. The decorator will perform ......
Read more >
Python: type checking decorator - Stack Overflow
You can pass as many types as you want and it checks if the types of the parameters passed to func match the...
Read more >
typecheck-decorator - PyPI
A decorator for functions, @tc.typecheck, to be used together with Python3 annotations on function parameters and function results.
Read more >
The Power of Decorators. Let's build a strict type checking…
The Power of Decorators. Let's build a strict type-checker and an awesome logging tool in Python from scratch.
Read more >
Python simple type checking decorator
Python simple type checking decorator · Custom and detailed description about the wrong argument · Later, you can extend this for custom ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found