Title: problem with the type definition of sum · Issue #7974 · python/typeshed · GitHub
Open Graph Title: problem with the type definition of sum · Issue #7974 · python/typeshed
X Title: problem with the type definition of sum · Issue #7974 · python/typeshed
Description: Everything is explained in microsoft/pylance-release#2865. I copy it to avoid redirection. Environment data Language Server version: 2022.5.3 OS and version: macOS 10.14.6 (Mojave) Python version (& distribution if applicable, e.g. Anaco...
Open Graph Description: Everything is explained in microsoft/pylance-release#2865. I copy it to avoid redirection. Environment data Language Server version: 2022.5.3 OS and version: macOS 10.14.6 (Mojave) Python version (...
X Description: Everything is explained in microsoft/pylance-release#2865. I copy it to avoid redirection. Environment data Language Server version: 2022.5.3 OS and version: macOS 10.14.6 (Mojave) Python version (...
Opengraph URL: https://github.com/python/typeshed/issues/7974
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"problem with the type definition of sum","articleBody":"Everything is explained in https://github.com/microsoft/pylance-release/issues/2865. I copy it to avoid redirection.\r\n\r\n*Environment data*\r\n\r\nLanguage Server version: 2022.5.3\r\nOS and version: macOS 10.14.6 (Mojave)\r\nPython version (& distribution if applicable, e.g. Anaconda): 3.10.4\r\nCode Snippet\r\n\r\n```python\r\ndef test(N:int) -> bool:\r\n from random import choice\r\n return sum(a for a in [choice([True,False]) for _ in range(N)])==2\r\n```\r\n\r\n*Expected behavior*\r\n\r\nThis code should be perfectly fine.\r\n\r\n*Actual behavior*\r\n\r\nProblem detected on the last line\r\nPylance computes that it will always return False. Because it implies that sum on bool is always 0 or a bool.\r\n\r\n```\r\nCondition will always evaluate to False since the types \"bool | Literal[0]\" and \"Literal[2]\"\r\nhave no overlapPylance[reportUnnecessaryComparison]\r\n(https://github.com/microsoft/pylance-release/blob/main/DIAGNOSTIC_SEVERITY_RULES.md#diagnostic-severity-rules)\r\n```\r\n\r\nTwo problems here :\r\n1/ bool should be considered as int, because they are ! (True+True==2)\r\n2/ Even with ==1 at the end of the line, pylance seems to think that (True==1) is False.\r\n\r\n________\r\n\r\nAnswer of Eric Traut when I posted this bug report on pylance :\r\n\r\nI'm glad to see that you're using the reportUnnecessaryComparison diagnostic check! It's a strict-mode check, so most pylance users are not aware of it.\r\n\r\nThis looks like a bug in the type definition of sum, which is defined in the typeshed stubs [here](https://github.com/python/typeshed/blob/1eef7365b9f68f6af023da731369096f55437654/stdlib/builtins.pyi#L1552):\r\n\r\n```python\r\ndef sum(__iterable: Iterable[_SumT]) -> _SumT | Literal[0]: ...\r\n```\r\nThat leads to the following:\r\n\r\n```python\r\nx = sum([True, True, True])\r\nreveal_type(x) # bool | Literal[0]\r\n```\r\n\r\nHowever, the correct (runtime) type of this expression is int.\r\n\r\nSo the bug will need to be fixed in the typeshed stubs. It will probably require a special-case overload for sum to properly model its behavior. Please file a bug (or a PR) in the typeshed project.\r\n\r\nHere are a few suggested workarounds:\r\n\r\nUse ints rather than bools in your code:\r\n```python\r\ndef test(N: int) -> bool:\r\n return sum([choice([1, 0]) for _ in range(N)]) == 2\r\n```\r\nAdd a # pyright: ignore[reportUnnecessaryComparison] or # type: ignore comment to the end of the line.\r\nDisable that diagnostic check for the file by adding a # pyright: reportUnnecessaryComparison=false comment at the top of the file.","author":{"url":"https://github.com/ChristophePapazian","@type":"Person","name":"ChristophePapazian"},"datePublished":"2022-05-28T13:13:02Z","url":"https://github.com/python/typeshed/issues/7974"}
| 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:225aa568-e251-bba2-6d4f-d4de9a99fe19 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 8620:2463C6:D97099:1235638:6B005582 |
| html-safe-nonce | f4b7fe65688c8666d5377f83d8ec0e6f26f20525a53aeac2df2d8e9f6577b655 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4NjIwOjI0NjNDNjpEOTcwOTk6MTIzNTYzODo2QjAwNTU4MiIsInZpc2l0b3JfaWQiOiIyODA1NTU3ODMwMjE4NzAwMTYyIiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 56f0c1a12291bacff6ac2b373281e8da11b72acff2d299345a4a41515fdf6801 |
| hovercard-subject-tag | issue:1251629070 |
| 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/typeshed/7974/issue_layout |
| twitter:image | https://opengraph.githubassets.com/69baef48db687a89d05942a94452505e3807d256386375a1aa6574ae0ca8a86f/python/typeshed/issues/7974 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/69baef48db687a89d05942a94452505e3807d256386375a1aa6574ae0ca8a86f/python/typeshed/issues/7974 |
| og:image:alt | Everything is explained in microsoft/pylance-release#2865. I copy it to avoid redirection. Environment data Language Server version: 2022.5.3 OS and version: macOS 10.14.6 (Mojave) Python version (... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | ChristophePapazian |
| hostname | github.com |
| expected-hostname | github.com |
| None | f420f4996e6b1ff1ccd6d7683f8134f4b5f0f8a50124c210dbfe7490e5644abb |
| turbo-cache-control | no-preview |
| go-import | github.com/python/typeshed git https://github.com/python/typeshed.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 31696383 |
| octolytics-dimension-repository_nwo | python/typeshed |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 31696383 |
| octolytics-dimension-repository_network_root_nwo | python/typeshed |
| 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 | 26e00df917aba0f128135dc073ea0c5e33a70d40 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width