Skip to content

Using typecheck decorator slows module import dramatically #468

Description

@alonme

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

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions