Skip to content

Instantly share code, notes, and snippets.

@shiumachi
Created February 22, 2023 07:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shiumachi/a5174eddce73d6178428449b6f96bf0e to your computer and use it in GitHub Desktop.
Save shiumachi/a5174eddce73d6178428449b6f96bf0e to your computer and use it in GitHub Desktop.
Pythonで誤って関数に括弧をつけず呼び出した場合にどこまで検知できるか
# Pythonで誤って関数に括弧をつけず呼び出した場合にどこまで検知できるか
# @shiumachi, 2023/02/22
# Python 3.11.2, mypy 1.0.1
def main():
def func(x: int = 1) -> int:
return x
# 検知できない例1: 元々callableでも受け付けられる使い方をする場合
print(f"{func=}")
# 検知できない例2: 型ヒントをつけていない変数に代入する場合
# vscodeならPylanceのInlay Hints: Variable Typesを有効にすると目視でチェックしやすくなる
a = func
print(f"{a=}")
# 検知できる例: 変数に型ヒントをつける
# ただしデフォルトではこれも検知できない
# vscode: PylanceのType Checking Modeをbasic以上にする
# mypy: check_untyped_defs = true を設定
b: int = func
print(f"{b=}")
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment