René's URL Explorer Experiment


Title: Allow `Final` in type arguments to avoid shadowing arguments - disallow reassignment of function parameters · Issue #11076 · python/mypy · GitHub

Open Graph Title: Allow `Final` in type arguments to avoid shadowing arguments - disallow reassignment of function parameters · Issue #11076 · python/mypy

X Title: Allow `Final` in type arguments to avoid shadowing arguments - disallow reassignment of function parameters · Issue #11076 · python/mypy

Description: Feature As proposed in wemake-services/wemake-python-styleguide#2128, I am looking to find a way to disallow reassignment of function parameters. During the discussion, I discovered that Final was introduced in PEP 591 and seemed like a ...

Open Graph Description: Feature As proposed in wemake-services/wemake-python-styleguide#2128, I am looking to find a way to disallow reassignment of function parameters. During the discussion, I discovered that Final was ...

X Description: Feature As proposed in wemake-services/wemake-python-styleguide#2128, I am looking to find a way to disallow reassignment of function parameters. During the discussion, I discovered that Final was ...

Opengraph URL: https://github.com/python/mypy/issues/11076

X: @github

direct link

Domain: github.com


Hey, it has json ld scripts:
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Allow `Final` in type arguments to avoid shadowing arguments - disallow reassignment of function parameters","articleBody":"**Feature**\r\n\r\nAs proposed in https://github.com/wemake-services/wemake-python-styleguide/issues/2128, I am looking to find a way to disallow reassignment of function parameters. During the discussion, I discovered that `Final` was introduced in [PEP 591](https://www.python.org/dev/peps/pep-0591/) and seemed like a great solution but I ran into the limitation around function arguments:\r\n\r\n> Final can’t be used in annotations for function arguments:\r\n>\r\n> https://mypy.readthedocs.io/en/stable/final_attrs.html#details-of-using-final\r\n\r\n\r\n---\r\n\r\nI was curious on why the limitation was there in the first place but all I could really find was from the [PEP 591 PR on GitHub](https://github.com/python/peps/pull/990). And was unable to find the pre-discussion on the mailing list or that GitHub project.\r\n\r\n> Two more things worth mentioning in rejected ideas: [...]\r\n>\r\n>  2. allow `Final` in function argument annotations\r\n>\r\n> For the second point, it would cause too much confusion with function not mutating the argument.\r\n>\r\n> *-- https://github.com/python/peps/pull/990#commitcomment-33164572*\r\n\r\n---\r\n\r\nIn `mypy`, I could find that `Final` was added in https://github.com/python/mypy/pull/5522 which mentions:\r\n\r\n> I don't allow `Final` in function argument types. One argument is simplicity, another is I didn't see many bugs related to shadowing an argument in function bodies, finally people might have quite different expectations for this. If people will ask, this would be easy to implement.\r\n>\r\n> *-- https://github.com/python/mypy/pull/5522*\r\n\r\n\r\nIn a previous revision of the documentation, it mentioned this example:\r\n\r\n> * ``Final`` can be only used as an outermost type in assignments, using it in\r\n>   any other position is an error. In particular, ``Final`` can't be used in\r\n>   annotations for function arguments because this may cause confusions about\r\n>   what are the guarantees in this case:\r\n> \r\n> ```python\r\n> : List[Final[int]] = []  # Error!\r\n> def fun(x: Final[List[int]]) ->  None:  # Error!\r\n> ```\r\n>\r\n> *-- https://github.com/python/mypy/pull/5522/files/c9abd9db835240962467a9ee164f4bbb50d56545#diff-3b595f6f83800d78a304cf528ed39aff352c8cd8282edda26bafced79646baad*\r\n\r\n\r\n\r\n\r\n**Pitch**\r\n\r\nAllow `Final` in type arguments.\r\n\r\n\r\nRecently ran into a real-life bug because of accidentally re-assigning a function parameter, https://github.com/matrix-org/synapse/pull/10439/files/a94217ee34840237867d037cf1133f3a9bf6b95a\r\n\r\nSimplified reproduction case (original code):\r\n\r\n```python\r\ndef ensure_correct_context(event: EventBase, context: EventContext):\r\n  remote_auth_chain = get_event_auth(event)\r\n  for auth_event in remote_auth_chain:\r\n    # XXX: Problem is here!\r\n    # We are introducing a bug because `context` which corresponds to `event`\r\n    # is being re-assigned to the `context` for the `auth_event`\r\n    context = compute_event_context(auth_event)\r\n    persist_event(auth_event, context)\r\n\r\n  # logic for context_needs_updating\r\n\r\n  if context_needs_updating:\r\n    return compute_event_context(event)\r\n\r\n  return context\r\n```\r\n\r\nFixed code:\r\n\r\n```python\r\ndef ensure_correct_context(event: EventBase, context: EventContext):\r\n  remote_auth_chain = get_event_auth(event)\r\n  for auth_event in remote_auth_chain:\r\n    auth_event_context = compute_event_context(auth_event)\r\n    persist_event(auth_event, auth_event_context)\r\n\r\n  # logic for context_needs_updating\r\n\r\n  if context_needs_updating:\r\n    return compute_event_context(event)\r\n\r\n  return context\r\n```\r\n\r\nProposed `mypy` code that would trigger a violation for the bug to catch it in the first place when writing the original code:\r\n\r\n```python\r\ndef ensure_correct_context(event: EventBase, context: Final[EventContext]):\r\n  remote_auth_chain = get_event_auth(event)\r\n  for auth_event in remote_auth_chain:\r\n    # XXX: Violation shold trigger here!\r\n    # We are introducing a bug because `context` which corresponds to `event`\r\n    # is being re-assigned to the `context` for the `auth_event`\r\n    context = compute_event_context(auth_event)\r\n    persist_event(auth_event, context)\r\n\r\n  # logic for context_needs_updating\r\n\r\n  if context_needs_updating:\r\n    return compute_event_context(event)\r\n\r\n  return context\r\n```\r\n\r\n---\r\n\r\nI am coming at this from a JavaScript background where ESlint has a [`no-param-reassign` rule](https://eslint.org/docs/rules/no-param-reassign) for JavaScript which disallows reassignment of function parameters.\r\n\r\n> Assignment to variables declared as function parameters can be misleading and lead to confusing behavior, ~~as modifying function parameters will also mutate the `arguments` object~~. Often, assignment to function parameters is unintended and indicative of a mistake or programmer error.\r\n>\r\n> https://eslint.org/docs/rules/no-param-reassign\r\n\r\n> Why? Manipulating objects passed in as parameters can cause unwanted variable side effects in the original caller.\r\n>\r\n> https://airbnb.io/javascript/#functions--mutate-params\r\n\r\n> It's also about the clarity of mutating and reassigning - it's easier to understand functions when the arguments are static and constant throughout the life of the function.\r\n>\r\n> https://github.com/eslint/website/issues/686#issuecomment-577419721\r\n> Related discussion, https://github.com/eslint/eslint/issues/5306\r\n\r\n\r\n---\r\n\r\n - shadowing\r\n - masking\r\n - overriding\r\n - overwriting\r\n \r\n","author":{"url":"https://github.com/MadLittleMods","@type":"Person","name":"MadLittleMods"},"datePublished":"2021-09-08T19:23:16Z","url":"https://github.com/python/mypy/issues/11076"}

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:d55478c5-4adf-65ee-1dbc-4834fe8b0527
current-catalog-service-hash81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114
request-id9A58:1BAA33:197096C:21BE1D5:6B0024EA
html-safe-nonce2c1839369abfecfffccf1f11c6fce1c9512c73f6b1dfdbdc6f889ec26c3e9967
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5QTU4OjFCQUEzMzoxOTcwOTZDOjIxQkUxRDU6NkIwMDI0RUEiLCJ2aXNpdG9yX2lkIjoiMjA0MTc1MzgyMTM5NDI0ODkzOCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9
visitor-hmac7859f54c72450dc7091eadf0786d2aff8ada92f0f8593b990c68d6fe8f3180d2
hovercard-subject-tagissue:991454630
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/11076/issue_layout
twitter:imagehttps://opengraph.githubassets.com/e474c8ab8228b766349b49703c72bb281b7795486ebceb63a9a11097efe5d905/python/mypy/issues/11076
twitter:cardsummary_large_image
og:imagehttps://opengraph.githubassets.com/e474c8ab8228b766349b49703c72bb281b7795486ebceb63a9a11097efe5d905/python/mypy/issues/11076
og:image:altFeature As proposed in wemake-services/wemake-python-styleguide#2128, I am looking to find a way to disallow reassignment of function parameters. During the discussion, I discovered that Final was ...
og:image:width1200
og:image:height600
og:site_nameGitHub
og:typeobject
og:author:usernameMadLittleMods
hostnamegithub.com
expected-hostnamegithub.com
None4eb4cba0d69f045374ac15de9666d22b0961e1d555cc3824f36b7734c00c78e0
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
release44c658b24abf00491fb7ae5f56d42c9f38cbeab2
ui-targetfull
theme-color#1e2327
color-schemelight dark
react-app-nameissues

Links:

Skip to contenthttps://github.com/python/mypy/issues/11076#start-of-content
https://github.com/
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpython%2Fmypy%2Fissues%2F11076
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%2F11076
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/11076
Reloadhttps://github.com/python/mypy/issues/11076
Reloadhttps://github.com/python/mypy/issues/11076
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 requests546 (546)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/11076
New issuehttps://github.com/login?return_to=https://github.com/python/mypy/issues/11076
Allow Final in type arguments to avoid shadowing arguments - disallow reassignment of function parametershttps://github.com/python/mypy/issues/11076#top
featurehttps://github.com/python/mypy/issues?q=state%3Aopen%20label%3A%22feature%22
topic-finalPEP 591https://github.com/python/mypy/issues?q=state%3Aopen%20label%3A%22topic-final%22
https://github.com/MadLittleMods
MadLittleModshttps://github.com/MadLittleMods
on Sep 8, 2021https://github.com/python/mypy/issues/11076#issue-991454630
wemake-services/wemake-python-styleguide#2128https://github.com/wemake-services/wemake-python-styleguide/issues/2128
PEP 591https://www.python.org/dev/peps/pep-0591/
https://mypy.readthedocs.io/en/stable/final_attrs.html#details-of-using-finalhttps://mypy.readthedocs.io/en/stable/final_attrs.html#details-of-using-final
PEP 591 PR on GitHubhttps://github.com/python/peps/pull/990
python/peps#990 (comment)https://github.com/python/peps/pull/990#commitcomment-33164572
#5522https://github.com/python/mypy/pull/5522
#5522https://github.com/python/mypy/pull/5522
https://github.com/python/mypy/pull/5522/files/c9abd9db835240962467a9ee164f4bbb50d56545#diff-3b595f6f83800d78a304cf528ed39aff352c8cd8282edda26bafced79646baadhttps://github.com/python/mypy/pull/5522/files/c9abd9db835240962467a9ee164f4bbb50d56545#diff-3b595f6f83800d78a304cf528ed39aff352c8cd8282edda26bafced79646baad
https://github.com/matrix-org/synapse/pull/10439/files/a94217ee34840237867d037cf1133f3a9bf6b95ahttps://github.com/matrix-org/synapse/pull/10439/files/a94217ee34840237867d037cf1133f3a9bf6b95a
no-param-reassign rulehttps://eslint.org/docs/rules/no-param-reassign
https://eslint.org/docs/rules/no-param-reassignhttps://eslint.org/docs/rules/no-param-reassign
https://airbnb.io/javascript/#functions--mutate-paramshttps://airbnb.io/javascript/#functions--mutate-params
eslint/archive-website#686 (comment)https://github.com/eslint/archive-website/issues/686#issuecomment-577419721
eslint/eslint#5306https://github.com/eslint/eslint/issues/5306
MadLittleModshttps://github.com/MadLittleMods
featurehttps://github.com/python/mypy/issues?q=state%3Aopen%20label%3A%22feature%22
on Sep 8, 2021https://github.com/python/mypy/issues/11076#event-5274306783
https://github.com/erictraut
erictrauthttps://github.com/erictraut
on Sep 8, 2021https://github.com/python/mypy/issues/11076#issuecomment-915528377
https://github.com/MadLittleMods
MadLittleModshttps://github.com/MadLittleMods
on Sep 8, 2021https://github.com/python/mypy/issues/11076#issuecomment-915550286
Synapsehttps://github.com/matrix-org/synapse
proposal in the other projecthttps://github.com/wemake-services/wemake-python-styleguide/issues/2128#issuecomment-892020950
https://florimond.dev/en/posts/2018/08/python-mutable-defaults-are-the-source-of-all-evil/https://florimond.dev/en/posts/2018/08/python-mutable-defaults-are-the-source-of-all-evil/
wemake-services/wemake-python-styleguide#2128 (comment)https://github.com/wemake-services/wemake-python-styleguide/issues/2128#issuecomment-892020950
"noqa is also not an option here 😞 Because this pattern is really common and it is something you can hardly replace."https://github.com/wemake-services/wemake-python-styleguide/issues/2128#issuecomment-892020950
AlexWaygoodhttps://github.com/AlexWaygood
topic-finalPEP 591https://github.com/python/mypy/issues?q=state%3Aopen%20label%3A%22topic-final%22
on Mar 26, 2022https://github.com/python/mypy/issues/11076#event-6311881763
https://github.com/fabioz
fabiozhttps://github.com/fabioz
on Jul 25, 2023https://github.com/python/mypy/issues/11076#issuecomment-1649649148
https://github.com/MadLittleMods
MadLittleModshttps://github.com/MadLittleMods
on Jul 3, 2025https://github.com/python/mypy/issues/11076#issuecomment-3032932506
redefined-argument-from-local (R1704)https://pylint.readthedocs.io/en/latest/user_guide/messages/refactor/redefined-argument-from-local.html
ruff as PLR1704https://docs.astral.sh/ruff/rules/redefined-argument-from-local/#redefined-argument-from-local-plr1704
Sign up for freehttps://github.com/signup?return_to=https://github.com/python/mypy/issues/11076
Sign in to commenthttps://github.com/login?return_to=https://github.com/python/mypy/issues/11076
featurehttps://github.com/python/mypy/issues?q=state%3Aopen%20label%3A%22feature%22
topic-finalPEP 591https://github.com/python/mypy/issues?q=state%3Aopen%20label%3A%22topic-final%22
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.