Title: mypy not coping well with a variable changing type · Issue #2589 · python/mypy · GitHub
Open Graph Title: mypy not coping well with a variable changing type · Issue #2589 · python/mypy
X Title: mypy not coping well with a variable changing type · Issue #2589 · python/mypy
Description: Sometimes, it is necessary to accumulate data using a mutable structure and then recast the data into an immutable object suitable for use as a dictionary key. If the transformation switching data types while using the same variable name...
Open Graph Description: Sometimes, it is necessary to accumulate data using a mutable structure and then recast the data into an immutable object suitable for use as a dictionary key. If the transformation switching data ...
X Description: Sometimes, it is necessary to accumulate data using a mutable structure and then recast the data into an immutable object suitable for use as a dictionary key. If the transformation switching data ...
Opengraph URL: https://github.com/python/mypy/issues/2589
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"mypy not coping well with a variable changing type","articleBody":"Sometimes, it is necessary to accumulate data using a mutable structure and then recast the data into an immutable object suitable for use as a dictionary key. If the transformation switching data types while using the same variable name, mypy emits an inscrutable error message that doesn't hint at what the actual problem is. \r\n\r\nThe solution was to use another variable name for the transmuted data.\r\n\r\nRecommendation: Let variables be rebound to different types as needed. Barring that, provide a better error message.\r\n\r\nSimplified example:\r\n\r\n```\r\nfrom typing import List, DefaultDict, Tuple, Dict\r\nfrom collections import defaultdict\r\nfrom pprint import pprint\r\n\r\nscores = [95, 86, 64, 92, 71, 68, 79, 92]\r\n\r\nDecile = int\r\nScore = int\r\n\r\ndeciles = defaultdict(list) # type: DefaultDict[Decile, List[Score]]\r\nfor score in scores:\r\n deciles[score // 10].append(score)\r\n\r\n# turn deciles into a plain dict mapping to tuples\r\ndeciles = {decile : tuple(group) for decile, group in deciles.items()} # type: Dict[Decile, Tuple[Score]]\r\n\r\npprint(deciles)\r\n\r\n```\r\n\r\nThe error message is: \r\n\r\n```\r\ntmp.py:10: error: Argument 1 to \"defaultdict\" has incompatible type List[_T]; expected Callable[[], Tuple[int]]\r\ntmp.py:12: error: \"Tuple[int]\" has no attribute \"append\"\r\ntmp.py:15: error: Name 'deciles' already defined\r\ntmp.py:15: error: Value expression in dictionary comprehension has incompatible type Tuple[int, ...]; expected type \"Tuple[int]\"\r\n```\r\n\r\nNote, the message for line 10 makes no sense until you see the later message for line 15. In my client's code base, these two we very far apart and made it difficult to figure out why line 10 gets an error message even though it is correct code.\r\n\r\nThe solution was to change the last lines to use new variable names:\r\n\r\n```\r\n# turn deciles into a plain dict mapping to tuples\r\nnewdeciles = {decile : tuple(group) for decile, group in deciles.items()} # type: Dict[Decile, Tuple[Score]]\r\n\r\npprint(newdeciles)\r\n```\r\n\r\nIt would be nice if the error message applied only to the redefinition line rather than the initial correct declaration. It would be even nicer if mypy supported patterns that allowed a tranformation in-place or re-use of a variable name for cases like converting a list of lists into a list of tuples. The latter example is not uncommon in my client's code base (because the inner lists need to be used as dict keys).\r\n\r\n","author":{"url":"https://github.com/rhettinger","@type":"Person","name":"rhettinger"},"datePublished":"2016-12-17T22:58:12Z","url":"https://github.com/python/mypy/issues/2589"}
| 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:67a6ed92-c8e0-68d4-6ba8-6f7432976799 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 98A8:1756E4:24E2AA4:30F213E:6B005D8F |
| html-safe-nonce | 65a6046e337f01c34a5f941eeeebc403b0e6d6c5fcef53c662cf8720f943922e |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5OEE4OjE3NTZFNDoyNEUyQUE0OjMwRjIxM0U6NkIwMDVEOEYiLCJ2aXNpdG9yX2lkIjoiMzMxNTE3NzgwMTc5MDgwNzQzOSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 642c3254f67423e4eac53c25612389f22e5e25ec5a29b0ce08d91968d94f93a6 |
| hovercard-subject-tag | issue:196247537 |
| 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/2589/issue_layout |
| twitter:image | https://opengraph.githubassets.com/2bda324ffafb35b7917d351bfc67e5ff02d398c0d19b51443f4435074ee19b47/python/mypy/issues/2589 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/2bda324ffafb35b7917d351bfc67e5ff02d398c0d19b51443f4435074ee19b47/python/mypy/issues/2589 |
| og:image:alt | Sometimes, it is necessary to accumulate data using a mutable structure and then recast the data into an immutable object suitable for use as a dictionary key. If the transformation switching data ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | rhettinger |
| hostname | github.com |
| expected-hostname | github.com |
| None | 29be975efff6507a5d135de9c7d634cf7ff8f0c55222a64386ab3202209aaf73 |
| 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 | eacd927ba6112e3b01ba99c762bd699a4a4f5df7 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width