-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Allow Final
in type arguments to avoid shadowing arguments - disallow reassignment of function parameters
#11076
Comments
Do you want to mark select function parameters as |
🤔 I think that would be good. I am trying to think what would most likely get included in Synapse (where I want to end up using it). And around the mutable default argument pattern which was brought up in the proposal in the other project, https://florimond.dev/en/posts/2018/08/python-mutable-defaults-are-the-source-of-all-evil/ (via wemake-services/wemake-python-styleguide#2128 (comment)) class NotOurClass(object):
def method(self, a: Union[List[str], None] = None):
"""default impl"""
class OurClass(NotOurClass):
def method(self, a: Union[List[str], None] = None):
if a is None:
a = []
# our own impl The following could work if class OurClass(NotOurClass):
def method(self, a: Union[List[str], None] = None):
sanitizedA = a
if a is None:
sanitizedA = []
# our own impl Or explicitly allow when we want to use it with ignore comments. Although this one had a detractor: " class OurClass(NotOurClass):
def method(self, a: Union[List[str], None] = None):
if a is None:
# type: ignore[no-param-reassign] # noqa
a = []
# our own impl |
I'm a bit confused... the description seems to be for marking selected parameters as I think that this request (from title and description) should cover the |
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 great solution but I ran into the limitation around function arguments:I 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. And was unable to find the pre-discussion on the mailing list or that GitHub project.
In
mypy
, I could find thatFinal
was added in #5522 which mentions:In a previous revision of the documentation, it mentioned this example:
Pitch
Allow
Final
in type arguments.Recently ran into a real-life bug because of accidentally re-assigning a function parameter, https://github.com/matrix-org/synapse/pull/10439/files/a94217ee34840237867d037cf1133f3a9bf6b95a
Simplified reproduction case (original code):
Fixed code:
Proposed
mypy
code that would trigger a violation for the bug to catch it in the first place when writing the original code:I am coming at this from a JavaScript background where ESlint has a
no-param-reassign
rule for JavaScript which disallows reassignment of function parameters.The text was updated successfully, but these errors were encountered: