agronholm / typeguard

Run-time type checker for Python
Other
1.48k stars 108 forks source link

Using typecheck decorator slows module import dramatically #468

Open alonme opened 1 month ago

alonme commented 1 month ago

Things to check first

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

hoorelbeke-jimmy commented 3 weeks ago

I also have performance issues when importing code using typeguard. I switched back to typeguard==2.13.3 and it's much faster

agronholm commented 3 weeks ago

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.

agronholm commented 3 weeks ago

Oh, sorry, indeed with @typechecked it would be slowed down – only the full import hook would benefit from the byte code cache.

alonme commented 3 weeks ago

@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

bswck commented 2 weeks ago

@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.