Title: Allow disabling implicit Any for type aliases · Issue #2696 · python/mypy · GitHub
Open Graph Title: Allow disabling implicit Any for type aliases · Issue #2696 · python/mypy
X Title: Allow disabling implicit Any for type aliases · Issue #2696 · python/mypy
Description: The issue is somehow discussed here #2690 Here is a sample code that mypy allows and eventually fails in runtime from typing import * T = TypeVar('T', List[int], Dict) Processor = Callable[[T], None] def output(inp: T, processor: Process...
Open Graph Description: The issue is somehow discussed here #2690 Here is a sample code that mypy allows and eventually fails in runtime from typing import * T = TypeVar('T', List[int], Dict) Processor = Callable[[T], Non...
X Description: The issue is somehow discussed here #2690 Here is a sample code that mypy allows and eventually fails in runtime from typing import * T = TypeVar('T', List[int], Dict) Processor = Callable[...
Opengraph URL: https://github.com/python/mypy/issues/2696
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Allow disabling implicit Any for type aliases","articleBody":"The issue is somehow discussed here #2690 \r\n\r\nHere is a sample code that `mypy` allows and eventually fails in runtime\r\n\r\n```python\r\nfrom typing import *\r\nT = TypeVar('T', List[int], Dict)\r\nProcessor = Callable[[T], None]\r\ndef output(inp: T, processor: Processor) -> None:\r\n processor(inp)\r\ndef process_dict(inp: Dict) -> None:\r\n print(inp.keys())\r\noutput([1, 2, 3], process_dict)\r\n```\r\n\r\nAs described in #606 discussions and https://www.python.org/dev/peps/pep-0484/#type-aliases this is not a problem with `mypy`, but an overlooked mistake in non-parameterizing generic type alias`Processor` in `output`.\r\n\r\n`reveal_type` for `output`:\r\n`Revealed type is 'def [T in (builtins.list[builtins.int], builtins.dict)] (inp: T`-1, processor: def (Any))'`\r\n\r\n`Processor` is implicitly treated as `Processor[Any]` in this case.\r\n\r\nChanging `output` to `def output(inp: T, processor: Processor[T]) -> None:` resolves the issue and lets `mypy` catch the bug.\r\n\r\nThe problem is that implicit `Any` for type aliases can easily go unnoticed.\r\n\r\nSuggestions:\r\n\r\n1. As suggested by @roganov & @ilevkivskyi : Add `--disable-implicit-any` flag to mypy, which will disable this behavior and forces the user to be explicit. Drawback, users who do not know about the flag might learn about the bug the hard way (in production 😉 )\r\n\r\n2. Disable implicit `Any` by default for type aliases, and force users to be explicit (either by parameterizing `[Any]` or `[T]`). Drawback, might introduce a lot of mypy errors for existing, \"error free\" code base (confusing/annoying to users). Also, not entirely sure about how this impacts other scenarios.\r\n\r\n3. Produce `warning` when used type alias has no explicit parameterization. This will make mypy exit without failure, but just point the user to potential bugs. Drawback, `warnings` might be annoying for users who are ok with their codebase (i.e. might require `--suppress-implicit-any-warning`)\r\n\r\n4. Any other suggestion ... 😄 \r\n\r\nThanks,\r\n","author":{"url":"https://github.com/mohabusama","@type":"Person","name":"mohabusama"},"datePublished":"2017-01-16T11:11:25Z","url":"https://github.com/python/mypy/issues/2696"}
| 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:1ba2a969-d7c7-b2e1-262b-ba5aa48a153f |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | E620:26206F:316B5D4:41599B2:6B003058 |
| html-safe-nonce | a5dd28f9c202df7e5624d7d46f5643ef5116bb709b82f68e6e1442695e4cd43c |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFNjIwOjI2MjA2RjozMTZCNUQ0OjQxNTk5QjI6NkIwMDMwNTgiLCJ2aXNpdG9yX2lkIjoiODM4MjA4NzA1MTczMzkwNTQ5NiIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | b1851313694ec4bea0f5af4a8ebdb75c8e90f05f2b950e7b8d74948dadb677bb |
| hovercard-subject-tag | issue:200991295 |
| 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/2696/issue_layout |
| twitter:image | https://opengraph.githubassets.com/8d665df4f9f6350c593e454b2060615c269b266a99e5cd3820da9f3996faf04c/python/mypy/issues/2696 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/8d665df4f9f6350c593e454b2060615c269b266a99e5cd3820da9f3996faf04c/python/mypy/issues/2696 |
| og:image:alt | The issue is somehow discussed here #2690 Here is a sample code that mypy allows and eventually fails in runtime from typing import * T = TypeVar('T', List[int], Dict) Processor = Callable[[T], Non... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | mohabusama |
| hostname | github.com |
| expected-hostname | github.com |
| None | 7d0829bbde9e48b2486cb5dd6d147896eb01d32134627091f818958a935ef3f0 |
| 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 | f3f3a43ca095cdea3abbb394112e971f9fdb94fa |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width