Using typecheck decorator slows module import dramatically
Things to check first
- [X] I have searched the existing issues and didn't find my feature already requested there
Feature description
As a result of researching why the import of the inflect library was slow I found out that the @typechecked decorator was the main reason.
See https://github.com/jaraco/inflect/issues/212
I believe that the same solution suggested in this issue can also be applied generally, the solution being to defer running the typechecked decorator until the decorated function is actually called, and to cache the result of the "decoration" as otherwise every call to the decorated function will be slow
Something like this:
@functools.cache
def cached_typechecked(func: Callable) -> Callable:
return typechecked(func)
def lazy_typechecked(func: Callable) -> Callable:
def wrapper(*args, **kwargs) -> Any:
typechecked_func = cached_typechecked(func)
return typechecked_func(*args, **kwargs)
return wrapper
Use case
Faster imports for all direct and indirect users of typeguard
I also have performance issues when importing code using typeguard. I switched back to typeguard==2.13.3 and it's much faster
It slows down the first import of a module. Subsequent imports should be fine when loaded from bytecode cache. Let me know if this is not the case.
Oh, sorry, indeed with @typechecked it would be slowed down – only the full import hook would benefit from the byte code cache.
@agronholm this is the case, however this is an issue nontheless
I encountered this issue while using inflect and the import took more than a second (I mean, more than a second was only running typeguards decorators)
Edit: was written after your first comment
Edit 2: ok now I understand what you mean by first, So yea youre second comment gets it
@agronholm Hi! I reached out to you via e-mail. Let me know if you're interested in me taking the full responsibility of resolving this.