Allow `Final` in type arguments to avoid shadowing arguments - disallow reassignment of function parameters
まだ誰も着手していません。
- 主要言語
- Python
- スター
- 20.6k
- フォーク
- 3.3k
- PR マージ指標
- PR 指標を取得中
説明
Feature
As 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 and seemed like a great solution but I ran into the limitation around function arguments:
Final can’t be used in annotations for function arguments:
https://mypy.readthedocs.io/en/stable/final_attrs.html#details-of-using-final
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.
Two more things worth mentioning in rejected ideas: [...]
- allow
Finalin function argument annotationsFor the second point, it would cause too much confusion with function not mutating the argument.
-- https://github.com/python/peps/pull/990#commitcomment-33164572
In mypy, I could find that Final was added in https://github.com/python/mypy/pull/5522 which mentions:
I don't allow
Finalin 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.
In a previous revision of the documentation, it mentioned this example:
Finalcan be only used as an outermost type in assignments, using it in
any other position is an error. In particular,Finalcan't be used in
annotations for function arguments because this may cause confusions about
what are the guarantees in this case:: List[Final[int]] = [] # Error! def fun(x: Final[List[int]]) -> None: # Error!
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):
def ensure_correct_context(event: EventBase, context: EventContext):
remote_auth_chain = get_event_auth(event)
for auth_event in remote_auth_chain:
# XXX: Problem is here!
# We are introducing a bug because `context` which corresponds to `event`
# is being re-assigned to the `context` for the `auth_event`
context = compute_event_context(auth_event)
persist_event(auth_event, context)
# logic for context_needs_updating
if context_needs_updating:
return compute_event_context(event)
return context
Fixed code:
def ensure_correct_context(event: EventBase, context: EventContext):
remote_auth_chain = get_event_auth(event)
for auth_event in remote_auth_chain:
auth_event_context = compute_event_context(auth_event)
persist_event(auth_event, auth_event_context)
# logic for context_needs_updating
if context_needs_updating:
return compute_event_context(event)
return context
Proposed mypy code that would trigger a violation for the bug to catch it in the first place when writing the original code:
def ensure_correct_context(event: EventBase, context: Final[EventContext]):
remote_auth_chain = get_event_auth(event)
for auth_event in remote_auth_chain:
# XXX: Violation shold trigger here!
# We are introducing a bug because `context` which corresponds to `event`
# is being re-assigned to the `context` for the `auth_event`
context = compute_event_context(auth_event)
persist_event(auth_event, context)
# logic for context_needs_updating
if context_needs_updating:
return compute_event_context(event)
return context
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.
Assignment to variables declared as function parameters can be misleading and lead to confusing behavior,
as modifying function parameters will also mutate the. Often, assignment to function parameters is unintended and indicative of a mistake or programmer error.argumentsobject
Why? Manipulating objects passed in as parameters can cause unwanted variable side effects in the original caller.
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.
https://github.com/eslint/website/issues/686#issuecomment-577419721
Related discussion, https://github.com/eslint/eslint/issues/5306
- shadowing
- masking
- overriding
- overwriting
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
まず、PEP 591 の制約と、PR #5522 で説明されている mypy の既存の Final 実装を読みます。関数引数のアノテーションと再代入のセマンティクスに関するリンク先の例および議論を確認します。Final の関数パラメーターにおける動作が決定され、再代入違反が一貫して報告されれば完了です。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- python
- 領域
- tooling
- issue の種類
- 機能追加
- 難易度
- 5/5
- 見積もり時間
- 1週間以上
- 活発さ
- 停滞
- 明瞭さ
- おおむね明確
- 初心者へのやさしさ
- 35/100