Title: Overload fallback for literal booleans allows forbidden literal signatures · Issue #13893 · python/mypy · GitHub
Open Graph Title: Overload fallback for literal booleans allows forbidden literal signatures · Issue #13893 · python/mypy
X Title: Overload fallback for literal booleans allows forbidden literal signatures · Issue #13893 · python/mypy
Description: I am not sure if this is a mypy issue or is a limitation of PEP 586. I want to use boolean literals to forbid a specific combination of function parameter types and boolean flag values. In my example data is AnyStr, but if data_must_be_b...
Open Graph Description: I am not sure if this is a mypy issue or is a limitation of PEP 586. I want to use boolean literals to forbid a specific combination of function parameter types and boolean flag values. In my examp...
X Description: I am not sure if this is a mypy issue or is a limitation of PEP 586. I want to use boolean literals to forbid a specific combination of function parameter types and boolean flag values. In my examp...
Opengraph URL: https://github.com/python/mypy/issues/13893
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Overload fallback for literal booleans allows forbidden literal signatures","articleBody":"I am not sure if this is a mypy issue or is a limitation of PEP 586. I want to use boolean literals to forbid a specific combination of function parameter types and boolean flag values. In my example `data` is `AnyStr`, but if `data_must_be_bytes=True` it must be `bytes`. (In my real use case `data_must_be_bytes` is actually `data_is_image`; and I want to allow either `bytes` for images, or `AnyStr` for non images)\r\n\r\nAs indicated in [PEP 586](https://peps.python.org/pep-0586/#interactions-with-overloads) and [mypy docs](https://mypy.readthedocs.io/en/stable/literal_types.html#literal-types), I need to add a fallback to allow dealing with the original bool type. However, this fallback will also allow the forbidden combination where `data` is `str` and `data_must_be_bytes=True`\r\n\r\nAssuming that the the necessity of the fallback is out of this discussion's scope, I guess I either need a way explicitly state in the fallback that `data_must_be_bytes` is `bool` except `Literal[True]`, or have mypy not do the fallback from `Literal[True]` to `bool` when a narrower overload is given. Without a way to overcome this issue, there seems to be no benefit in using overloading and literals here, over simpler type annotations. It would also be useful to have the docs call out such cases where the added complexity is redundant. \r\n\r\n```\r\nfrom typing import overload, Literal, AnyStr\r\n\r\n\r\n@overload\r\ndef f(data: bytes, data_must_be_bytes: Literal[True]) -> int: ...\r\n\r\n\r\n# redundant due to following fallback:\r\n#@overload\r\n#def f(data: AnyStr, data_must_be_bytes: Literal[False]) -> int: ...\r\n\r\n\r\n# necessary fallback for non literals:\r\n@overload\r\ndef f(data: AnyStr, data_must_be_bytes: bool) -> int: ...\r\n\r\n\r\ndef f(data, data_must_be_bytes):\r\n return 0\r\n\r\nf(data='string', data_must_be_bytes=False)\r\nf(data=b'bytes', data_must_be_bytes=False)\r\nf(data='string', data_must_be_bytes=True) # this passes but i want it to fail\r\nf(data=b'bytes', data_must_be_bytes=True)\r\nf(data='string', data_must_be_bytes=bool(0))\r\nf(data=b'bytes', data_must_be_bytes=bool(0))\r\nf(data='string', data_must_be_bytes=bool(1))\r\nf(data=b'bytes', data_must_be_bytes=bool(1))\r\n```\r\n\r\n[mypy-play](https://mypy-play.net/?mypy=0.981&python=3.10&gist=8cb62c8b34a329369db31d7ec524c37c)","author":{"url":"https://github.com/eyaler","@type":"Person","name":"eyaler"},"datePublished":"2022-10-14T11:27:27Z","url":"https://github.com/python/mypy/issues/13893"}
| 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:8fd8f7f0-efee-3d92-43bd-4ad40bb0d3f1 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | B27E:59CDA:33476C2:43AA6D3:6B00385B |
| html-safe-nonce | 62745e64654b3c675b303c4361f0f624de539459db2fad874378717e42f7389c |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCMjdFOjU5Q0RBOjMzNDc2QzI6NDNBQTZEMzo2QjAwMzg1QiIsInZpc2l0b3JfaWQiOiI1Nzk5MTk2NDg5ODk2OTY2MjM1IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | b60d7f799d8020ce470bec06451eb5b0c1e186ee49fed9a2945a127ef3cb4260 |
| hovercard-subject-tag | issue:1409204538 |
| 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/13893/issue_layout |
| twitter:image | https://opengraph.githubassets.com/3566108b18568f8d7da1b9ce0c0d395a28236fe3cc83f35a1fdddf3f54070744/python/mypy/issues/13893 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/3566108b18568f8d7da1b9ce0c0d395a28236fe3cc83f35a1fdddf3f54070744/python/mypy/issues/13893 |
| og:image:alt | I am not sure if this is a mypy issue or is a limitation of PEP 586. I want to use boolean literals to forbid a specific combination of function parameter types and boolean flag values. In my examp... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | eyaler |
| 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