簡體   English   中英

用戶警告:不存在類型注釋——不進行類型檢查 __main__.my_func warn('不存在類型注釋——不進行類型檢查 {}'.format(functio

[英]UserWarning: no type annotations present -- not typechecking __main__.my_func warn('no type annotations present -- not typechecking {}'.format(functio

為什么 typeguard 不對 function 進行類型檢查?

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM