Title: TypingError: type checker will emit error with a message specified in typing stubs · Issue #1043 · python/typing · GitHub
Open Graph Title: TypingError: type checker will emit error with a message specified in typing stubs · Issue #1043 · python/typing
X Title: TypingError: type checker will emit error with a message specified in typing stubs · Issue #1043 · python/typing
Description: Some ways to call certain functions are just wrong, and are known to be a bug. For example, passing type=bool to argparse's add_argument method doesn't work. To make type checkers warn about this, typeshed can currently do something like...
Open Graph Description: Some ways to call certain functions are just wrong, and are known to be a bug. For example, passing type=bool to argparse's add_argument method doesn't work. To make type checkers warn about this, ...
X Description: Some ways to call certain functions are just wrong, and are known to be a bug. For example, passing type=bool to argparse's add_argument method doesn't work. To make type checkers warn abou...
Opengraph URL: https://github.com/python/typing/issues/1043
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"TypingError: type checker will emit error with a message specified in typing stubs","articleBody":"Some ways to call certain functions are just wrong, and are known to be a bug. For example, [passing type=bool to argparse's add_argument method doesn't work](https://stackoverflow.com/q/15008758). To make type checkers warn about this, typeshed can currently do something like this:\r\n\r\n```python3\r\n@overload\r\ndef add_argument(self, unrelated args, type: Type[bool], more unrelated args) -> NoReturn: ...\r\n@overload\r\ndef add_argument(self, unrelated args) -> None: ...\r\n```\r\n\r\nWhen add_argument is used wrongly, you would then get errors:\r\n\r\n```\r\nimport argparse\r\nparser = argparse.ArgumentParser(description=\"My parser\")\r\nparser.add_argument(\"--my_bool\", type=bool)\r\nprint(parser.parse_args()) # Error: statement is unreachable\r\n```\r\n\r\nThis approach has a couple problems:\r\n- The type checker's error message doesn't say \"you passed in type=bool and it's wrong\". It says \"statement is unreachable\".\r\n- The type checker's error message appears on a different line than where the buggy code is.\r\n- Unreachable code warnings are disabled by default in mypy, even with `--strict`.\r\n\r\nThe solution I would like: if you declare the return type as `TypingError[\"foo bar\"]`, the type checker will display an error message `foo bar`. This is similar to how `#error` works in the C preprocessor.\r\n\r\nStub:\r\n\r\n```python3\r\n@overload\r\ndef add_argument(\r\n self, unrelated args, type: Type[bool], more unrelated args\r\n) -> TypingError[\"type=bool doesn't work, see https://stackoverflow.com/q/15008758\"]: ...\r\n@overload\r\ndef add_argument(self, unrelated args) -> None: ...\r\n```\r\n\r\nPython file:\r\n\r\n```python3\r\nimport argparse\r\nparser = argparse.ArgumentParser(description=\"My parser\")\r\nparser.add_argument(\"--my_bool\", type=bool) # Error: type=bool doesn't work, see https://stackoverflow.com/q/15008758\r\nprint(parser.parse_args())\r\n```","author":{"url":"https://github.com/Akuli","@type":"Person","name":"Akuli"},"datePublished":"2022-01-24T09:15:21Z","url":"https://github.com/python/typing/issues/1043"}
| 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:a9624578-7f82-87db-b338-667cfddbab95 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | C7FA:30CC09:224BAF6:2E5593E:6B001F95 |
| html-safe-nonce | a1f7c2b33fa4739c104d8fcb96545504330ba126ff861da48c0b24e9406f48b3 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDN0ZBOjMwQ0MwOToyMjRCQUY2OjJFNTU5M0U6NkIwMDFGOTUiLCJ2aXNpdG9yX2lkIjoiNjgzMzEwNDkyMzE3ODA0OTQyOSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 3057e5708fea01f3bedf3cb5360ea79df13c1fadaec98163cdd65d308bc5a994 |
| hovercard-subject-tag | issue:1112367476 |
| 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/typing/1043/issue_layout |
| twitter:image | https://opengraph.githubassets.com/9ce357ce62153240a28e673ce036692421f1eefb22922532e869a66d5fceaf7c/python/typing/issues/1043 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/9ce357ce62153240a28e673ce036692421f1eefb22922532e869a66d5fceaf7c/python/typing/issues/1043 |
| og:image:alt | Some ways to call certain functions are just wrong, and are known to be a bug. For example, passing type=bool to argparse's add_argument method doesn't work. To make type checkers warn about this, ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | Akuli |
| hostname | github.com |
| expected-hostname | github.com |
| None | 4eb4cba0d69f045374ac15de9666d22b0961e1d555cc3824f36b7734c00c78e0 |
| turbo-cache-control | no-preview |
| go-import | github.com/python/typing git https://github.com/python/typing.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 24604008 |
| octolytics-dimension-repository_nwo | python/typing |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 24604008 |
| octolytics-dimension-repository_network_root_nwo | python/typing |
| 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 | 44c658b24abf00491fb7ae5f56d42c9f38cbeab2 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width