René's URL Explorer Experiment


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

direct link

Domain: github.com


Hey, it has json ld scripts:
{"@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-profiling0
react-import-mapreact,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-controllervoltron_issues_fragments
route-actionissue_layout
fetch-noncev2:67a6ed92-c8e0-68d4-6ba8-6f7432976799
current-catalog-service-hash81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114
request-id98A8:1756E4:24E2AA4:30F213E:6B005D8F
html-safe-nonce65a6046e337f01c34a5f941eeeebc403b0e6d6c5fcef53c662cf8720f943922e
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5OEE4OjE3NTZFNDoyNEUyQUE0OjMwRjIxM0U6NkIwMDVEOEYiLCJ2aXNpdG9yX2lkIjoiMzMxNTE3NzgwMTc5MDgwNzQzOSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9
visitor-hmac642c3254f67423e4eac53c25612389f22e5e25ec5a29b0ce08d91968d94f93a6
hovercard-subject-tagissue:196247537
github-keyboard-shortcutsrepository,issues,copilot
google-site-verificationApib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I
octolytics-urlhttps://collector.github.com/github/collect
analytics-location///voltron/issues_fragments/issue_layout
fb:app_id1401488693436528
apple-itunes-appapp-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python/mypy/2589/issue_layout
twitter:imagehttps://opengraph.githubassets.com/2bda324ffafb35b7917d351bfc67e5ff02d398c0d19b51443f4435074ee19b47/python/mypy/issues/2589
twitter:cardsummary_large_image
og:imagehttps://opengraph.githubassets.com/2bda324ffafb35b7917d351bfc67e5ff02d398c0d19b51443f4435074ee19b47/python/mypy/issues/2589
og:image:altSometimes, 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:width1200
og:image:height600
og:site_nameGitHub
og:typeobject
og:author:usernamerhettinger
hostnamegithub.com
expected-hostnamegithub.com
None29be975efff6507a5d135de9c7d634cf7ff8f0c55222a64386ab3202209aaf73
turbo-cache-controlno-preview
go-importgithub.com/python/mypy git https://github.com/python/mypy.git
octolytics-dimension-user_id1525981
octolytics-dimension-user_loginpython
octolytics-dimension-repository_id7053637
octolytics-dimension-repository_nwopython/mypy
octolytics-dimension-repository_publictrue
octolytics-dimension-repository_is_forkfalse
octolytics-dimension-repository_network_root_id7053637
octolytics-dimension-repository_network_root_nwopython/mypy
turbo-body-classeslogged-out env-production page-responsive
disable-turbofalse
browser-stats-urlhttps://api.github.com/_private/browser/stats
browser-errors-urlhttps://api.github.com/_private/browser/errors
releaseeacd927ba6112e3b01ba99c762bd699a4a4f5df7
ui-targetfull
theme-color#1e2327
color-schemelight dark
react-app-nameissues

Links:

Skip to contenthttps://github.com/python/mypy/issues/2589#start-of-content
https://github.com/
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpython%2Fmypy%2Fissues%2F2589
GitHubRepository hosting, code review, and collaborationhttps://github.com/platform
CopilotAI coding agent for tasks, projects, and automated workhttps://github.com/features/copilot
ActionsCI/CD and automationhttps://github.com/features/actions
Code QualityQuality analysis, fixes, and enforcementhttps://github.com/features/code-quality
CodespacesCloud development environmentshttps://github.com/features/codespaces
Code SecurityVulnerability detection and remediationhttps://github.com/security/advanced-security/code-security
Secret ProtectionSecret scanning and leak preventionhttps://github.com/security/advanced-security/secret-protection
All Featureshttps://github.com/features
Documentationhttps://docs.github.com/en
Traininghttps://learn.github.com/skills
Certificationshttps://learn.github.com/credentials
Build with Copilothttps://github.com/features/ai
Customize Copilothttps://docs.github.com/en/copilot/how-tos/copilot-on-github/customize-copilot/customize-copilot-overview
Integrationshttps://github.com/marketplace
Trending reposhttps://github.com/trending
GitHub Sponsorshttps://github.com/open-source/sponsors
Developer articleshttps://github.com/resources/articles
ChangelogYour source for the latest features, improvements, and fixes across GitHub products, all in one place.https://github.blog/changelog/
Enterpriseshttps://github.com/enterprise
Teamhttps://github.com/team
Startupshttps://github.com/enterprise/startups
Nonprofitshttps://github.com/solutions/industry/nonprofits
Healthcarehttps://github.com/solutions/industry/healthcare
Financial serviceshttps://github.com/solutions/industry/financial-services
Manufacturinghttps://github.com/solutions/industry/manufacturing
Governmenthttps://github.com/solutions/industry/government
See all industrieshttps://github.com/solutions/industry
Why GitHubhttps://github.com/why-github
Customer Storieshttps://github.com/customer-stories
Business insightshttps://github.com/solutions/executive-insights
Trust Centerhttps://github.com/trust-center
GitHub Advanced SecurityEnterprise-grade code securityhttps://github.com/security/advanced-security
Copilot for businessEnterprise-grade AI featureshttps://github.com/features/copilot/copilot-business
Premium SupportEnterprise-grade 24/7 supporthttps://github.com/enterprise/premium-support
Documentationhttps://docs.github.com/en
Resource centerhttps://github.com/resources
Bloghttps://github.blog/
Events & webinarshttps://github.com/resources/events
Community Forumhttps://github.com/orgs/community/discussions
Customer Storieshttps://github.com/customer-stories
Integrationshttps://github.com/marketplace
Customer supporthttps://help.github.com/
Traininghttps://learn.github.com/skills
GitHub Statushttps://www.githubstatus.com/
Abouthttps://github.com/about
Partnershttps://github.com/partners
Changeloghttps://github.blog/changelog/
GitHub UniverseThe event for developers, by developers — talks, workshops, and what’s next for GitHub.https://githubuniverse.com/
Pricinghttps://github.com/pricing
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpython%2Fmypy%2Fissues%2F2589
Sign uphttps://github.com/signup?ref_cta=Sign+up&ref_loc=header+logged+out&ref_page=%2F%3Cuser-name%3E%2F%3Crepo-name%3E%2Fvoltron%2Fissues_fragments%2Fissue_layout&source=header-repo&source_repo=python%2Fmypy
Reloadhttps://github.com/python/mypy/issues/2589
Reloadhttps://github.com/python/mypy/issues/2589
Reloadhttps://github.com/python/mypy/issues/2589
pythonhttps://github.com/python
mypyhttps://github.com/python/mypy
Codehttps://github.com/python/mypy
Issues2.7k (2.7k)https://github.com/python/mypy/issues
Pull requests545 (545)https://github.com/python/mypy/pulls
Actionshttps://github.com/python/mypy/actions
Projectshttps://github.com/python/mypy/projects
Wikihttps://github.com/python/mypy/wiki
Security and qualityhttps://github.com/python/mypy/security
Insightshttps://github.com/python/mypy/insights
https://github.com/python/mypy
New issuehttps://github.com/login?return_to=https://github.com/python/mypy/issues/2589
New issuehttps://github.com/login?return_to=https://github.com/python/mypy/issues/2589
mypy not coping well with a variable changing typehttps://github.com/python/mypy/issues/2589#top
https://github.com/rhettinger
rhettingerhttps://github.com/rhettinger
on Dec 17, 2016https://github.com/python/mypy/issues/2589#issue-196247537
https://github.com/rwbarton
rwbartonhttps://github.com/rwbarton
on Dec 17, 2016https://github.com/python/mypy/issues/2589#issuecomment-267793825
https://github.com/ambv
ambvhttps://github.com/ambv
on Dec 19, 2016https://github.com/python/mypy/issues/2589#issuecomment-268044680
@rwbartonhttps://github.com/rwbarton
@rhettingerhttps://github.com/rhettinger
https://github.com/rwbarton
rwbartonhttps://github.com/rwbarton
on Dec 19, 2016https://github.com/python/mypy/issues/2589#issuecomment-268057522
@rwbartonhttps://github.com/rwbarton
@rhettingerhttps://github.com/rhettinger
https://github.com/ambv
ambvhttps://github.com/ambv
on Dec 19, 2016https://github.com/python/mypy/issues/2589#issuecomment-268059655
@rwbartonhttps://github.com/rwbarton
https://github.com/ilevkivskyi
ilevkivskyihttps://github.com/ilevkivskyi
on Dec 19, 2016https://github.com/python/mypy/issues/2589#issuecomment-268067697
@rwbartonhttps://github.com/rwbarton
https://github.com/ambv
ambvhttps://github.com/ambv
on Dec 19, 2016https://github.com/python/mypy/issues/2589#issuecomment-268093314
#2591https://github.com/python/mypy/pull/2591
JukkaLhttps://github.com/JukkaL
completedhttps://github.com/python/mypy/issues?q=is%3Aissue%20state%3Aclosed%20archived%3Afalse%20reason%3Acompleted
ca2d2c6https://github.com/python/mypy/commit/ca2d2c65b39a241c77d7560bd5717cdc9c988b7d
on Dec 22, 2016https://github.com/python/mypy/issues/2589#event-903013075
Sign up for freehttps://github.com/signup?return_to=https://github.com/python/mypy/issues/2589
Sign in to commenthttps://github.com/login?return_to=https://github.com/python/mypy/issues/2589
https://github.com
Termshttps://docs.github.com/site-policy/github-terms/github-terms-of-service
Privacyhttps://docs.github.com/site-policy/privacy-policies/github-privacy-statement
Securityhttps://github.com/security
Statushttps://www.githubstatus.com/
Communityhttps://github.community/
Docshttps://docs.github.com/
Contacthttps://support.github.com?tags=dotcom-footer

Viewport: width=device-width


URLs of crawlers that visited me.