Skip to content
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

Using typecheck decorator slows module import dramatically #468

Open
1 task done
alonme opened this issue Jun 20, 2024 · 5 comments
Open
1 task done

Using typecheck decorator slows module import dramatically #468

alonme opened this issue Jun 20, 2024 · 5 comments

Comments

@alonme
Copy link

alonme commented Jun 20, 2024

Things to check first

  • 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 jaraco/inflect#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
Copy link

hoorelbeke-jimmy commented Jul 3, 2024

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

@agronholm
Copy link
Owner

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
Copy link
Owner

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

@alonme
Copy link
Author

alonme commented Jul 5, 2024

@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
Copy link

bswck commented Jul 11, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants