agronholm / agronholm/typeguard

Using typecheck decorator slows module import dramatically

未關閉
#468 5 則留言 2 個 reaction 已指派 0 人 在 GitHub 檢視
enhancement
主要語言
Python
星號
1.8k
分支
145
平均合併
8 天 12 小時
30 天內合併 PR
1

描述

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

貢獻指南

開啟貢獻指南

評估

這個 Issue 還沒有評估資料。

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。