Title: Assignment of Any does not get rid of Optional (add `AnyUnion` type?) · Issue #3526 · python/mypy · GitHub
Open Graph Title: Assignment of Any does not get rid of Optional (add `AnyUnion` type?) · Issue #3526 · python/mypy
X Title: Assignment of Any does not get rid of Optional (add `AnyUnion` type?) · Issue #3526 · python/mypy
Description: Consider this code (run with --strict-optional): from typing import Optional def f(): # untyped function; often from silent import return 0 # always returns an int def g(x: Optional[int]) -> int if x is None: x = f() return x # E: Incomp...
Open Graph Description: Consider this code (run with --strict-optional): from typing import Optional def f(): # untyped function; often from silent import return 0 # always returns an int def g(x: Optional[int]) -> int if...
X Description: Consider this code (run with --strict-optional): from typing import Optional def f(): # untyped function; often from silent import return 0 # always returns an int def g(x: Optional[int]) -> int...
Opengraph URL: https://github.com/python/mypy/issues/3526
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Assignment of Any does not get rid of Optional (add `AnyUnion` type?)","articleBody":"Consider this code (run with `--strict-optional`):\r\n```\r\nfrom typing import Optional\r\ndef f():\r\n # untyped function; often from silent import\r\n return 0 # always returns an int\r\n\r\ndef g(x: Optional[int]) -> int\r\n if x is None:\r\n x = f()\r\n return x # E: Incompatible return value type (got \"Optional[int]\", expected \"int\")\r\n```\r\nMypy doesn't infer that `x` is not `None` in `g`. I think this a bug -- mypy \r\nshould be permissive here.\r\n\r\nIf we wanted to fix this, we'd run into a problem: what type should `x` have \r\nafter `x = f()`? We don't want it to be `Any`, because we don't want that \r\nbehavior in the general case. It shouldn't be `int` -- `f()` could be intended \r\nto unconditionally return `None` instead. To fix this, I think we need to \r\nintroduce a new special type, which I will call `AnyUnion`. `AnyUnion[A, \r\nB, C]` would represent a type which is allowed to be used as any of `A`, `B`, or \r\n`C` (similarly to how `Any` is allowed to be used as any type). In this case, \r\nwe could have `x`'s type bound to `AnyUnion[int, None]`, which would fix this \r\nerror.\r\n\r\nOther helpful uses of `AnyUnion`:\r\n- `pow` will no longer need to return `Any` in builtins. Instead, it could \r\n\treturn the much less broad `AnyUnion[int, float]`.\r\n- `AnyUnion[X, None]` would provide a per-variable opt-out of strict None \r\n\tchecking. This could be helpful for classes with delayed initialization where \r\n\ta lot of effort would be needed to refactor them into a state that would work \r\n\tproperly with strict None checking.\r\n\r\nCons:\r\n- Additional complexity of the type system affects implementation complexity and \r\n\tmakes things slightly harder for users to understand.\r\n- The motivating problem also exists with subclasses (with the way the binder \r\n\tcurrently works), but `AnyUnion` doesn't help much with that.\r\n","author":{"url":"https://github.com/ddfisher","@type":"Person","name":"ddfisher"},"datePublished":"2017-06-12T23:13:24Z","url":"https://github.com/python/mypy/issues/3526"}
| 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:e69f79cb-7fdc-0515-c1f5-7f57a1fce96f |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 9076:24D752:50F17C:6D0A14:6B002FE8 |
| html-safe-nonce | a970846dc83cce3188efc72d4b5fd888e2cc1572e3a4b066e8aecbde6d6f0e7d |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5MDc2OjI0RDc1Mjo1MEYxN0M6NkQwQTE0OjZCMDAyRkU4IiwidmlzaXRvcl9pZCI6IjUzOTIwMzYzNjQ3ODUwMzcyODgiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | b5802c75e06345b9beee2fd236e729f96b4a8d87fb83afa1c9a75304cbf2dd1e |
| hovercard-subject-tag | issue:235391960 |
| 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/3526/issue_layout |
| twitter:image | https://opengraph.githubassets.com/4269adcfc29adcdd69772655cd0ee62029691ee6643142f2fd1ab26d198a1d55/python/mypy/issues/3526 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/4269adcfc29adcdd69772655cd0ee62029691ee6643142f2fd1ab26d198a1d55/python/mypy/issues/3526 |
| og:image:alt | Consider this code (run with --strict-optional): from typing import Optional def f(): # untyped function; often from silent import return 0 # always returns an int def g(x: Optional[int]) -> int if... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | ddfisher |
| 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 | canary-2 |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width