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
Domain: github.com
{"@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-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:d55478c5-4adf-65ee-1dbc-4834fe8b0527 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 9A58:1BAA33:197096C:21BE1D5:6B0024EA |
| html-safe-nonce | 2c1839369abfecfffccf1f11c6fce1c9512c73f6b1dfdbdc6f889ec26c3e9967 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5QTU4OjFCQUEzMzoxOTcwOTZDOjIxQkUxRDU6NkIwMDI0RUEiLCJ2aXNpdG9yX2lkIjoiMjA0MTc1MzgyMTM5NDI0ODkzOCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 7859f54c72450dc7091eadf0786d2aff8ada92f0f8593b990c68d6fe8f3180d2 |
| hovercard-subject-tag | issue:991454630 |
| 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/11076/issue_layout |
| twitter:image | https://opengraph.githubassets.com/e474c8ab8228b766349b49703c72bb281b7795486ebceb63a9a11097efe5d905/python/mypy/issues/11076 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/e474c8ab8228b766349b49703c72bb281b7795486ebceb63a9a11097efe5d905/python/mypy/issues/11076 |
| og:image:alt | 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 ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | MadLittleMods |
| hostname | github.com |
| expected-hostname | github.com |
| None | 4eb4cba0d69f045374ac15de9666d22b0961e1d555cc3824f36b7734c00c78e0 |
| 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 | 44c658b24abf00491fb7ae5f56d42c9f38cbeab2 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width