[英]UserWarning: no type annotations present -- not typechecking __main__.my_func warn('no type annotations present -- not typechecking {}'.format(functio
function 在沒有@typechecked
裝飾器的情況下工作。 使用它運行會發出警告,說明未執行類型檢查。
from typeguard import typechecked
@typechecked
def my_func(x, y):
z = x + y
return z
a = my_func(1, 2)
print(a)
(venv) me@ubuntu-pcs:~/PycharmProjects/project$ python3 foo/bar.py
/home/me/miniconda3/envs/venv/lib/python3.9/site-packages/typeguard/__init__.py:1016: UserWarning: no type annotations present -- not typechecking __main__.my_func
warn('no type annotations present -- not typechecking {}'.format(function_name(func)))
1 2
None
按照設計, typeguard將忽略任何沒有類型注釋的 function 對象的內容。
為確保類型檢查,您必須在函數定義的typechecking
中包含類型提示。
from typeguard import typechecked
@typechecked
def my_func(x: int, y: int):
z = x + y
return z
a = my_func(1, 2)
print(a)
(venv) me@ubuntu-pcs:~/PycharmProjects/project$ python3 foo/bar.py
3
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.