agronholm / agronholm/typeguard

Using typecheck decorator slows module import dramatically

Aberta
#468 5 comentários 2 reações 0 responsáveis Ver no GitHub
enhancement
Linguagem predominante
Python
Estrelas
1.8k
Forks
146
Merge médio
8d 12h
PRs com merge (30d)
1

Descrição

### 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:

```python
@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

Guia de contribuição

Abrir o guia de contribuição

Avaliação

Esta issue ainda não foi avaliada.

Receba novas issues na sua caixa de entrada

Um resumo curto de issues do GitHub para quem está começando.