Title: We need a better story about Any · Issue #3194 · python/mypy · GitHub
Open Graph Title: We need a better story about Any · Issue #3194 · python/mypy
X Title: We need a better story about Any · Issue #3194 · python/mypy
Description: While working on protocols, I noticed that stubs often use Any where object will be more appropriate and vice versa. Because of how protocol inference works and how current mypy solver works, this leads to problems since sometimes mypy i...
Open Graph Description: While working on protocols, I noticed that stubs often use Any where object will be more appropriate and vice versa. Because of how protocol inference works and how current mypy solver works, this ...
X Description: While working on protocols, I noticed that stubs often use Any where object will be more appropriate and vice versa. Because of how protocol inference works and how current mypy solver works, this ...
Opengraph URL: https://github.com/python/mypy/issues/3194
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"We need a better story about Any","articleBody":"While working on protocols, I noticed that stubs often use ``Any`` where ``object`` will be more appropriate and vice versa. Because of how protocol inference works and how current mypy solver works, this leads to problems since sometimes mypy infers ``Any`` instead of giving an error (maybe we could improve this by \"tightening\" solver?)\r\n\r\nI think the root of the problem is that wee don't have a good story about ``Any``. I believe the rules for interaction between ``Any`` and other things should be like this (where ``C`` is a normal class like ``int`` or ``str``):\r\n```python\r\n(1) join(Any, object) == object\r\n(2) meet(Any, object) == Any\r\n(3) join(Any, C) == C\r\n(4) meet(Any, C) == C\r\n(5) join(Any, NoReturn) == Any\r\n(6) meet(Any, NoReturn) == NoReturn\r\n```\r\nI use ``NoReturn`` for uninhabited type, since it is how it is currently called in PEP 484. The above rules could be summarized in a simple diagram:\r\n```\r\n object\r\n |\r\n Any (if it appears in a meet)\r\n / | \\\r\n... all other types ...\r\n \\ | /\r\n Any (if it appear in a join)\r\n |\r\n NoReturn\r\n```\r\nCurrently, mypy behaves in a completely different way, and I believe this is not right, let me explain why. We could focus on rules (3) and (4). Currently, mypy returns ``Any`` in both these cases. Here is why it is not safe:\r\n```python\r\nfrom typing import Any\r\n\r\nx = 'a'\r\ny: Any = 1\r\n\r\nlst = [x, y]\r\nlst[0] >> 1 # not detected by mypy, fails at runtime, will be fixed by rule (3)\r\n\r\ndef f(x: int) -> None:\r\n x >> 1\r\ndef g(x: Any) -> None:\r\n pass\r\n\r\nfuns = [f, g]\r\nfuns[0]('a') # not detected by mypy, fails at runtime, will be fixed by rule (4)\r\n```\r\n@JukkaL I will be grateful for your opinion.","author":{"url":"https://github.com/ilevkivskyi","@type":"Person","name":"ilevkivskyi"},"datePublished":"2017-04-19T12:11:08Z","url":"https://github.com/python/mypy/issues/3194"}
| 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:977255cb-9a31-4229-4ae8-2a47d4728d4d |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | DCE6:33DDEF:17F1B9B:207BD35:6AFFF90F |
| html-safe-nonce | f2bfe91fd4b60470a247912171ef58e06966e5c8ac1fec21c14c61a8f8340635 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJEQ0U2OjMzRERFRjoxN0YxQjlCOjIwN0JEMzU6NkFGRkY5MEYiLCJ2aXNpdG9yX2lkIjoiNjA0ODI4NzY4Mzc3ODcwNTY4MCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | a61e5762538f26efa66cf52fd262fce8c0120134a034285b46282c6e05a420ee |
| hovercard-subject-tag | issue:222716848 |
| 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/3194/issue_layout |
| twitter:image | https://opengraph.githubassets.com/9bf6a5f5db0d9a43352dca6830a8dc3a69ba1185dd141d8ee6eade79cd180461/python/mypy/issues/3194 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/9bf6a5f5db0d9a43352dca6830a8dc3a69ba1185dd141d8ee6eade79cd180461/python/mypy/issues/3194 |
| og:image:alt | While working on protocols, I noticed that stubs often use Any where object will be more appropriate and vice versa. Because of how protocol inference works and how current mypy solver works, this ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | ilevkivskyi |
| 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