{"id":3416,"date":"2026-09-24T08:37:34","date_gmt":"2026-09-23T23:37:34","guid":{"rendered":"https:\/\/qol-kk.com\/wp2\/?p=3416"},"modified":"2026-09-24T08:37:34","modified_gmt":"2026-09-23T23:37:34","slug":"20260915-plan030","status":"publish","type":"post","link":"https:\/\/qol-kk.com\/wp2\/blog\/2026\/09\/24\/20260915-plan030\/","title":{"rendered":"mypy strict \u30e2\u30fc\u30c9\u3067\u578b\u5b89\u5168\u306aPython\u3092\u66f8\u304f"},"content":{"rendered":"<h2>\u6982\u8981<\/h2>\n<p>Python \u306e\u578b\u30d2\u30f3\u30c8\uff08type hints\uff09\u306f\u66f8\u3051\u308b\u3060\u3051\u3067\u306f\u610f\u5473\u304c\u306a\u304f\u3001\u9759\u7684\u89e3\u6790\u30c4\u30fc\u30eb\u3067\u691c\u8a3c\u3057\u3066\u306f\u3058\u3081\u3066\u4fa1\u5024\u304c\u51fa\u307e\u3059\u3002\u305d\u306e\u691c\u8a3c\u3092\u62c5\u3046\u306e\u304c mypy \u3067\u3059\u3002<\/p>\n<p>\u3068\u3053\u308d\u304c mypy \u3092\u30c7\u30d5\u30a9\u30eb\u30c8\u8a2d\u5b9a\u3067\u56de\u3057\u3066\u3082\u3001\u578b\u6ce8\u91c8\u306e\u306a\u3044\u95a2\u6570\u306f\u307e\u308b\u3054\u3068\u7d20\u901a\u3057\u3055\u308c\u307e\u3059\u3002\u300c\u5c0e\u5165\u3057\u305f\u306e\u306b\u30d0\u30b0\u3092\u53d6\u308a\u3053\u307c\u3059\u300d\u72b6\u614b\u306b\u306a\u308a\u3084\u3059\u3044\u3002\u3053\u306e\u8a18\u4e8b\u3067\u306f <code>--strict<\/code> \u30e2\u30fc\u30c9\u3092\u8d77\u70b9\u306b\u3001\u5b9f\u969b\u306b\u578b\u30a8\u30e9\u30fc\u3092\u691c\u51fa\u3067\u304d\u308b\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u69cb\u6210\u3092\u30cf\u30f3\u30ba\u30aa\u30f3\u3067\u4f5c\u3063\u3066\u3044\u304d\u307e\u3059\u3002<\/p>\n<h2>\u74b0\u5883<\/h2>\n<ul>\n<li>Python 3.11<\/li>\n<li>mypy 1.x<\/li>\n<\/ul>\n<pre class=\"lang:default decode:true\">python -m venv .venv\r\nsource .venv\/bin\/activate\r\npip install mypy\r\nmypy --version<\/pre>\n<h2>\u30c7\u30d5\u30a9\u30eb\u30c8 mypy \u304c\u898b\u9003\u3059\u3082\u306e<\/h2>\n<p>\u307e\u305a\u3001\u306a\u305c strict \u304c\u5fc5\u8981\u304b\u3092\u4f53\u611f\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true\">def add(a, b):\r\n    return a + b\r\n\r\nresult: int = add(\"1\", \"2\")  # \u672c\u5f53\u306f\u6587\u5b57\u5217\u9023\u7d50\u306b\u306a\u308b\r\nprint(result * 10)<\/pre>\n<p><code>add<\/code> \u306b\u578b\u6ce8\u91c8\u304c\u3042\u308a\u307e\u305b\u3093\u3002\u30c7\u30d5\u30a9\u30eb\u30c8\u306e mypy \u306f\u3053\u3046\u8a00\u3044\u307e\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true\">$ mypy sample.py\r\nSuccess: no issues found in 1 source file<\/pre>\n<p><code>add(\"1\", \"2\")<\/code> \u306f <code>\"12\"<\/code> \u3092\u8fd4\u3057\u3001<code>result: int<\/code> \u306b\u4ee3\u5165\u3055\u308c\u3066\u3044\u308b\u306e\u306b\u30a8\u30e9\u30fc\u306b\u306a\u308a\u307e\u305b\u3093\u3002<strong>\u6ce8\u91c8\u304c\u306a\u3044\u95a2\u6570\u306e\u4e2d\u8eab\u3068\u623b\u308a\u5024\u306f\u300c\u578b\u4e0d\u660e\uff08Any\uff09\u300d\u3068\u3057\u3066\u6271\u308f\u308c\u3001Any \u306f\u4f55\u306b\u3067\u3082\u4ee3\u5165\u3067\u304d\u308b<\/strong>\u304b\u3089\u3067\u3059\u3002\u3053\u308c\u304c\u300cmypy \u5165\u308c\u305f\u306e\u306b\u30d0\u30b0\u304c\u51fa\u308b\u300d\u306e\u5178\u578b\u30d1\u30bf\u30fc\u30f3\u3067\u3059\u3002<\/p>\n<h2>strict \u30e2\u30fc\u30c9\u3092\u6709\u52b9\u306b\u3059\u308b<\/h2>\n<pre class=\"lang:default decode:true\">$ mypy --strict sample.py\r\nsample.py:1: error: Function is missing a type annotation  [no-untyped-def]\r\nsample.py:4: error: Argument 1 to \"add\" has incompatible type \"str\"; expected \"int\"  [arg-type]\r\nFound 2 errors in 1 source file<\/pre>\n<p>\u4e00\u6c17\u306b\u691c\u51fa\u3055\u308c\u307e\u3057\u305f\u3002<code>--strict<\/code> \u306f\u8907\u6570\u306e\u53b3\u683c\u30c1\u30a7\u30c3\u30af\u3092\u307e\u3068\u3081\u3066\u6709\u52b9\u306b\u3059\u308b\u30b7\u30e7\u30fc\u30c8\u30ab\u30c3\u30c8\u3067\u3059\u3002\u4e3b\u306a\u3082\u306e\u306f\u6b21\u306e\u3068\u304a\u308a\u3067\u3059\u3002<\/p>\n<table>\n<thead>\n<tr>\n<th>\u30d5\u30e9\u30b0<\/th>\n<th>\u5185\u5bb9<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td><code>disallow_untyped_defs<\/code><\/td>\n<td>\u578b\u6ce8\u91c8\u306e\u306a\u3044\u95a2\u6570\u5b9a\u7fa9\u3092\u7981\u6b62<\/td>\n<\/tr>\n<tr>\n<td><code>disallow_any_generics<\/code><\/td>\n<td>\u88f8\u306e\u30b8\u30a7\u30cd\u30ea\u30af\u30b9\u3092\u7981\u6b62\uff08<code>list[int]<\/code> \u3092\u8981\u6c42\uff09<\/td>\n<\/tr>\n<tr>\n<td><code>warn_return_any<\/code><\/td>\n<td><code>Any<\/code> \u3092\u8fd4\u3059\u95a2\u6570\u306b\u8b66\u544a<\/td>\n<\/tr>\n<tr>\n<td><code>no_implicit_optional<\/code><\/td>\n<td>\u6697\u9ed9\u306e Optional \u3092\u7981\u6b62<\/td>\n<\/tr>\n<tr>\n<td><code>warn_unused_ignores<\/code><\/td>\n<td>\u4e0d\u8981\u306a <code># type: ignore<\/code> \u306b\u8b66\u544a<\/td>\n<\/tr>\n<tr>\n<td><code>check_untyped_defs<\/code><\/td>\n<td>\u6ce8\u91c8\u306e\u306a\u3044\u95a2\u6570\u306e\u4e2d\u8eab\u3082\u578b\u30c1\u30a7\u30c3\u30af<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u30b3\u30fc\u30c9\u3092\u76f4\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true\">def add(a: int, b: int) -&gt; int:\r\n    return a + b\r\n\r\nresult: int = add(1, 2)\r\nprint(result * 10)<\/pre>\n<h2>\u8a2d\u5b9a\u30d5\u30a1\u30a4\u30eb\u306b\u56fa\u5b9a\u3059\u308b<\/h2>\n<p>CLI \u30d5\u30e9\u30b0\u306f\u5fd8\u308c\u307e\u3059\u3002<code>pyproject.toml<\/code> \u306b\u66f8\u3044\u3066\u30ea\u30dd\u30b8\u30c8\u30ea\u3067\u5171\u6709\u3057\u307e\u3057\u3087\u3046\u3002<\/p>\n<pre class=\"lang:default decode:true\">[tool.mypy]\r\npython_version = \"3.11\"\r\nstrict = true\r\nwarn_unreachable = true\r\nwarn_redundant_casts = true<\/pre>\n<p>\u4ee5\u964d\u306f <code>mypy .<\/code> \u3060\u3051\u3067 strict \u304c\u52b9\u304d\u307e\u3059\u3002<\/p>\n<h2>\u5b9f\u8df51: Optional \u3092\u660e\u793a\u7684\u306b\u6271\u3046<\/h2>\n<pre class=\"lang:default decode:true\">def find_user(user_id: int) -&gt; str | None:\r\n    users = {1: \"alice\", 2: \"bob\"}\r\n    return users.get(user_id)\r\n\r\nname = find_user(3)\r\nprint(name.upper())  # None \u304b\u3082\u3057\u308c\u306a\u3044\u306e\u306b .upper() \u3092\u547c\u3093\u3067\u3044\u308b<\/pre>\n<pre class=\"lang:default decode:true\">$ mypy --strict sample.py\r\nerror: Item \"None\" of \"str | None\" has no attribute \"upper\"  [union-attr]<\/pre>\n<p>None \u30c1\u30a7\u30c3\u30af\u3092\u5f37\u5236\u3055\u308c\u307e\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true\">name = find_user(3)\r\nif name is not None:\r\n    print(name.upper())<\/pre>\n<p>mypy \u306f <code>if<\/code> \u30d6\u30ed\u30c3\u30af\u5185\u3067 <code>name<\/code> \u3092 <code>str<\/code> \u306b\u7d5e\u308a\u8fbc\u307f\uff08narrowing\uff09\u3057\u3066\u304f\u308c\u307e\u3059\u3002<\/p>\n<h2>\u5b9f\u8df52: \u6bb5\u968e\u7684\u306b strict \u3092\u5c0e\u5165\u3059\u308b<\/h2>\n<p>\u65e2\u5b58\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u306b\u3044\u304d\u306a\u308a <code>strict = true<\/code> \u3092\u5165\u308c\u308b\u3068\u3001\u6570\u767e\u4ef6\u306e\u30a8\u30e9\u30fc\u304c\u51fa\u307e\u3059\u3002\u73fe\u5b9f\u7684\u306b\u306f\u30e2\u30b8\u30e5\u30fc\u30eb\u5358\u4f4d\u3067\u6bb5\u968e\u5c0e\u5165\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true\">[tool.mypy]\r\npython_version = \"3.11\"\r\nignore_missing_imports = true\r\n\r\n[[tool.mypy.overrides]]\r\nmodule = [\"myapp.domain.*\", \"myapp.services.*\"]\r\ndisallow_untyped_defs = true\r\ndisallow_any_generics = true\r\nwarn_return_any = true<\/pre>\n<p>\u300c\u65b0\u3057\u304f\u89e6\u308b\u30b3\u30fc\u30c9\u304b\u3089\u578b\u3092\u56fa\u3081\u308b\u300d\u65b9\u91dd\u306b\u3059\u308c\u3070\u3001\u30ec\u30ac\u30b7\u30fc\u3092\u6b62\u3081\u305a\u306b\u578b\u5b89\u5168\u306a\u9818\u57df\u3092\u5e83\u3052\u3089\u308c\u307e\u3059\u3002<\/p>\n<h2>\u5b9f\u8df53: \u30b5\u30fc\u30c9\u30d1\u30fc\u30c6\u30a3\u306e\u578b\u304c\u306a\u3044\u3068\u304d<\/h2>\n<pre class=\"lang:default decode:true\">pip install types-requests  # requests \u7528\u306e\u516c\u5f0f\u30b9\u30bf\u30d6<\/pre>\n<p>\u30b9\u30bf\u30d6\u304c\u5b58\u5728\u3057\u306a\u3044\u30e9\u30a4\u30d6\u30e9\u30ea\u306f\u3001\u305d\u306e\u30e2\u30b8\u30e5\u30fc\u30eb\u3060\u3051\u7de9\u3081\u307e\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true\">[[tool.mypy.overrides]]\r\nmodule = [\"some_untyped_lib.*\"]\r\nignore_missing_imports = true<\/pre>\n<h2>\u78ba\u8a8d\u65b9\u6cd5<\/h2>\n<p>CI \u306b\u7d44\u307f\u8fbc\u3093\u3067\u3001\u578b\u30a8\u30e9\u30fc\u3067\u30d3\u30eb\u30c9\u3092\u843d\u3068\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true\">- name: Type check\r\n  run: |\r\n    pip install mypy\r\n    mypy .<\/pre>\n<p>pre-commit \u306b\u8f09\u305b\u308b\u3068\u3001\u30b3\u30df\u30c3\u30c8\u6642\u70b9\u3067\u5f3e\u3051\u307e\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true\">repos:\r\n  - repo: https:\/\/github.com\/pre-commit\/mirrors-mypy\r\n    rev: v1.10.0\r\n    hooks:\r\n      - id: mypy\r\n        args: [--strict]<\/pre>\n<h2>\u6ce8\u610f\u70b9<\/h2>\n<ul>\n<li><strong><code>Any<\/code> \u306f\u4f1d\u64ad\u3059\u308b<\/strong>\u30021\u304b\u6240\u3067\u8a31\u3059\u3068\u5468\u56f2\u306e\u578b\u30c1\u30a7\u30c3\u30af\u304c\u7121\u52b9\u5316\u3055\u308c\u307e\u3059\u3002<code>warn_return_any<\/code> \u3067\u5165\u53e3\u3092\u585e\u3050\u306e\u304c\u6709\u52b9\u3067\u3059\u3002<\/li>\n<li><strong><code># type: ignore<\/code> \u306b\u306f\u30a8\u30e9\u30fc\u30b3\u30fc\u30c9\u3092\u4ed8\u3051\u308b<\/strong>\uff08<code># type: ignore[arg-type]<\/code>\uff09\u3002\u5225\u7a2e\u306e\u30a8\u30e9\u30fc\u6df7\u5165\u3092\u691c\u51fa\u3067\u304d\u307e\u3059\u3002<\/li>\n<li><strong><code>strict = true<\/code> \u306f\u30d0\u30fc\u30b8\u30e7\u30f3\u3067\u5185\u5bb9\u304c\u5897\u3048\u308b<\/strong>\u3002mypy \u66f4\u65b0\u6642\u306f\u307e\u3068\u3081\u3066\u5bfe\u5fdc\u3059\u308b\u524d\u63d0\u3067\u3002<\/li>\n<li><strong>\u5b9f\u884c\u6642\u306e\u578b\u30c1\u30a7\u30c3\u30af\u3067\u306f\u306a\u3044<\/strong>\u3002\u5916\u90e8\u5165\u529b\u306e\u5b9f\u884c\u6642\u691c\u8a3c\u306f pydantic \u7b49\u304c\u5225\u9014\u5fc5\u8981\u3067\u3059\u3002<\/li>\n<\/ul>\n<h2>\u307e\u3068\u3081<\/h2>\n<ul>\n<li>\u30c7\u30d5\u30a9\u30eb\u30c8 mypy \u306f\u6ce8\u91c8\u306e\u306a\u3044\u95a2\u6570\u3092\u7d20\u901a\u3057\u3059\u308b\u305f\u3081\u3001\u30d0\u30b0\u3092\u53d6\u308a\u3053\u307c\u3059\u3002<\/li>\n<li><code>strict = true<\/code> \u3067\u300c\u6ce8\u91c8\u5fc5\u9808\u30fbAny \u8b66\u544a\u30fb\u6697\u9ed9 Optional \u7981\u6b62\u300d\u304c\u307e\u3068\u3081\u3066\u52b9\u304f\u3002<\/li>\n<li>\u65e2\u5b58\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u306f <code>overrides<\/code> \u3067\u30e2\u30b8\u30e5\u30fc\u30eb\u5358\u4f4d\u306b\u6bb5\u968e\u5c0e\u5165\u3059\u308b\u306e\u304c\u73fe\u5b9f\u89e3\u3002<\/li>\n<li>CI \u3068 pre-commit \u306b\u8f09\u305b\u3066\u521d\u3081\u3066\u300c\u5b88\u3089\u308c\u308b\u578b\u300d\u306b\u306a\u308b\u3002<\/li>\n<\/ul>\n<p>\u578b\u30d2\u30f3\u30c8\u306f\u66f8\u3044\u305f\u6642\u70b9\u3067\u306f\u5358\u306a\u308b\u30b3\u30e1\u30f3\u30c8\u3067\u3059\u3002strict \u30e2\u30fc\u30c9\u306e mypy \u3067\u691c\u8a3c\u3057\u3066\u306f\u3058\u3081\u3066\u3001\u30ea\u30d5\u30a1\u30af\u30bf\u30ea\u30f3\u30b0\u3092\u652f\u3048\u308b\u8cc7\u7523\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python \u306e\u578b\u30d2\u30f3\u30c8\uff08type hints\uff09\u306f\u66f8\u3051\u308b\u3060\u3051\u3067\u306f\u610f\u5473\u304c\u306a\u304f\u3001\u9759\u7684\u89e3\u6790\u30c4\u30fc\u30eb\u3067\u691c\u8a3c\u3057\u3066\u306f\u3058\u3081\u3066\u4fa1\u5024\u304c\u51fa\u307e\u3059\u3002\u305d\u306e\u691c\u8a3c\u3092\u62c5\u3046\u306e\u304c mypy \u3067\u3059\u3002<\/p>\n","protected":false},"author":1,"featured_media":3415,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"sns_share_botton_hide":"","vkExUnit_sns_title":"","_vk_print_noindex":"","sitemap_hide":"","_veu_custom_css":"","veu_display_promotion_alert":"common","vkexunit_cta_each_option":"","_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[38],"tags":[147],"class_list":["post-3416","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python-memorandum","tag-python"],"veu_head_title_object":{"title":"","add_site_title":""},"jetpack_publicize_connections":[],"jetpack-related-posts":[],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"https:\/\/qol-kk.com\/wp2\/wp-content\/uploads\/2026\/09\/20260915-plan030-thumbnail.jpg","_links":{"self":[{"href":"https:\/\/qol-kk.com\/wp2\/wp-json\/wp\/v2\/posts\/3416","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/qol-kk.com\/wp2\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/qol-kk.com\/wp2\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/qol-kk.com\/wp2\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/qol-kk.com\/wp2\/wp-json\/wp\/v2\/comments?post=3416"}],"version-history":[{"count":1,"href":"https:\/\/qol-kk.com\/wp2\/wp-json\/wp\/v2\/posts\/3416\/revisions"}],"predecessor-version":[{"id":3438,"href":"https:\/\/qol-kk.com\/wp2\/wp-json\/wp\/v2\/posts\/3416\/revisions\/3438"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/qol-kk.com\/wp2\/wp-json\/wp\/v2\/media\/3415"}],"wp:attachment":[{"href":"https:\/\/qol-kk.com\/wp2\/wp-json\/wp\/v2\/media?parent=3416"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/qol-kk.com\/wp2\/wp-json\/wp\/v2\/categories?post=3416"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/qol-kk.com\/wp2\/wp-json\/wp\/v2\/tags?post=3416"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}