Title: No errors on typed function internals if function definition itself is untyped; I'd argue that's probably incorrect behavior, but at the very least it's *super* confusing and *very* hard to diagnose when it happens · Issue #10895 · python/mypy · GitHub
Open Graph Title: No errors on typed function internals if function definition itself is untyped; I'd argue that's probably incorrect behavior, but at the very least it's *super* confusing and *very* hard to diagnose when it happens · Issue #10895 · python/mypy
X Title: No errors on typed function internals if function definition itself is untyped; I'd argue that's probably incorrect behavior, but at the very least it's *super* confusing and *very* hard to diagnose when it happens · Issue #10895 · python/mypy
Description: Issue description This doesn't generate an error: # test_case7.py from typing import List def untyped_func_def_with_typed_body(): a: List[int] = [0, 1, 2] key: str = "asdf" a[key] # <- No error, despite internals being typed This does: #...
Open Graph Description: Issue description This doesn't generate an error: # test_case7.py from typing import List def untyped_func_def_with_typed_body(): a: List[int] = [0, 1, 2] key: str = "asdf" a[key] # <- No error, de...
X Description: Issue description This doesn't generate an error: # test_case7.py from typing import List def untyped_func_def_with_typed_body(): a: List[int] = [0, 1, 2] key: str = "asdf" a[key] # &...
Opengraph URL: https://github.com/python/mypy/issues/10895
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"No errors on typed function internals if function definition itself is untyped; I'd argue that's probably incorrect behavior, but at the very least it's *super* confusing and *very* hard to diagnose when it happens","articleBody":"### Issue description\r\n\r\nThis doesn't generate an error:\r\n\r\n```python\r\n# test_case7.py\r\nfrom typing import List\r\n\r\ndef untyped_func_def_with_typed_body():\r\n a: List[int] = [0, 1, 2]\r\n key: str = \"asdf\"\r\n a[key] # \u003c- No error, despite internals being typed\r\n```\r\n\r\nThis does:\r\n\r\n```python\r\n# test_case8.py\r\nfrom typing import List\r\n\r\ndef typed_func_def_with_typed_body() -> None:\r\n a: List[int] = [0, 1, 2]\r\n key: str = \"asdf\"\r\n a[key] # \u003c- *Now* this generates an error\r\n```\r\n\r\nThat maybe _**technically**_ \"[documented](https://mypy.readthedocs.io/en/stable/common_issues.html#no-errors-reported-for-obviously-wrong-code)\", but that's _**really**_ esoteric and completely unintuitive. I've spent _**hours**_ chasing this down.\r\n\r\n---\r\n\r\n### Archived original flailing to arrive at core problem\r\n\r\n_UPDATE 2: Okay, this just keeps getting weirder and weirder. Consider:_\r\n\r\n```python\r\n# test_case3.py\r\n\r\ndef func():\r\n a = [0, 1, 2]\r\n a[\"asdf\"] # \u003c- This doesn't generate an error\r\n\r\nb = [0, 1, 2]\r\nb[\"asdf\"] # \u003c- But this does?!\r\n```\r\n\r\nRuntime:\r\n\r\n```\r\n% mypy --config=/dev/null test_case3.py\r\n/dev/null: No [mypy] section in config file\r\ntest_case3.py:8: error: No overload variant of \"__getitem__\" of \"list\" matches argument type \"str\"\r\ntest_case3.py:8: note: Possible overload variants:\r\ntest_case3.py:8: note: def __getitem__(self, SupportsIndex) -> int\r\ntest_case3.py:8: note: def __getitem__(self, slice) -> List[int]\r\nFound 1 error in 1 file (checked 1 source file)\r\n```\r\n\r\n_UPDATE 1: You don't need the `@overload`s. It_ **still** _doesn't work without them. Here's the simplified case:_\r\n\r\n```python\r\n# test_case2.py\r\nfrom typing import List, Union\r\n\r\nclass GetItemTest:\r\n def __getitem__(self, key: Union[int, slice]) -> Union[int, List[int]]:\r\n return list(range(10))[key]\r\n\r\ndef test_a():\r\n a = GetItemTest()\r\n a[\"asdf\"] # \u003c- Why isn't this a typing error?\r\n```\r\n\r\nOriginal case (same effect):\r\n\r\n```python\r\n# test_case1.py\r\nfrom typing import List, Union, overload\r\n\r\nclass GetItemTest:\r\n @overload\r\n def __getitem__(self, key: int) -> int:\r\n ...\r\n @overload\r\n def __getitem__(self, key: slice) -> List[int]:\r\n ...\r\n def __getitem__(self, key: Union[int, slice]) -> Union[int, List[int]]:\r\n return list(range(10))[key]\r\n\r\ndef test_a():\r\n a = GetItemTest()\r\n a[\"asdf\"] # \u003c- Why isn't this a typing error?\r\n```\r\n\r\nRuntime:\r\n\r\n```\r\n% python --version\r\nPython 3.9.6\r\n% pip list\r\nPackage Version\r\n----------------- --------\r\nmypy 0.910\r\nmypy-extensions 0.4.3\r\npip 21.2.1\r\nsetuptools 57.4.0\r\ntoml 0.10.2\r\ntyping-extensions 3.10.0.0\r\n% mypy --version\r\nmypy 0.910\r\n% mypy --config=/dev/null test_case1.py\r\n/dev/null: No [mypy] section in config file\r\nSuccess: no issues found in 1 source file\r\n```\r\n\r\nI can't prove it, but I **_feel_** like this used to work. It seems pretty basic.","author":{"url":"https://github.com/posita","@type":"Person","name":"posita"},"datePublished":"2021-07-30T13:54:13Z","url":"https://github.com/python/mypy/issues/10895"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:6663efec-cc6a-35c0-7090-eda0ac75fac0 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 8D42:100874:115B204:16CBE89:6B000A64 |
| html-safe-nonce | 43245ae877823f40b0c201c1817a9af956fb5d36c91b03502c4265c45b3c62dd |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4RDQyOjEwMDg3NDoxMTVCMjA0OjE2Q0JFODk6NkIwMDBBNjQiLCJ2aXNpdG9yX2lkIjoiMzE5MDM3MzU3Njk1MTcyODc0MCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | c213a28245375b40fa3187bcb7dda193a788fd0543aa52e208e9efa6b95ca3aa |
| hovercard-subject-tag | issue:956767716 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python/mypy/10895/issue_layout |
| twitter:image | https://opengraph.githubassets.com/918072c3f97170e9acbc0042056b01c68c494833f157334fbbad96840f044aab/python/mypy/issues/10895 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/918072c3f97170e9acbc0042056b01c68c494833f157334fbbad96840f044aab/python/mypy/issues/10895 |
| og:image:alt | Issue description This doesn't generate an error: # test_case7.py from typing import List def untyped_func_def_with_typed_body(): a: List[int] = [0, 1, 2] key: str = "asdf" a[key] # <- No error, de... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | posita |
| hostname | github.com |
| expected-hostname | github.com |
| None | 0a0d8686a27478df474dd7415776a7a080cde8838b3cd92ea7601187572e5729 |
| turbo-cache-control | no-preview |
| go-import | github.com/python/mypy git https://github.com/python/mypy.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 7053637 |
| octolytics-dimension-repository_nwo | python/mypy |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 7053637 |
| octolytics-dimension-repository_network_root_nwo | python/mypy |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 16a11879678aaf97e2a42c33ee70830aa084fa37 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width