Skip to content

Comments

Allow redefinitions for function arguments#20853

Merged
ilevkivskyi merged 3 commits intopython:masterfrom
ilevkivskyi:func-arg-redefine
Feb 25, 2026
Merged

Allow redefinitions for function arguments#20853
ilevkivskyi merged 3 commits intopython:masterfrom
ilevkivskyi:func-arg-redefine

Conversation

@ilevkivskyi
Copy link
Member

@ilevkivskyi ilevkivskyi commented Feb 20, 2026

Fixes #19918

Implementation is straightforward, but logic behind is essentially a compromise. Both empty context and full type context for redefinition have pros and cons. We select full context first for function arguments because:

  • It avoids false positives in at least some of the "covariant normalization" patterns. For example list[str | None] to list[str] in a branch will infer list[str | None] instead of list[str | None] | list[str]. Unconditional normalization like list[str | None] to list[str] still doesn't work, but it doesn't work for empty context either.
  • Conceptually function arguments are in between annotated and annotated variables, in the sense that user doesn't really have an option to leave them unannotated if they want the redefinition semantics. So we allow redefinition but use type context behavior like for annotated variables.

cc @JukkaL

@github-actions

This comment has been minimized.

@ilevkivskyi
Copy link
Member Author

As I suspected, for argument types, defaulting to full type context will be probably better (with a fallback to empty context in case of errors only).

@ilevkivskyi ilevkivskyi changed the title Try allowing redefinitions for function arguments Allow redefinitions for function arguments Feb 24, 2026
@ilevkivskyi ilevkivskyi marked this pull request as ready for review February 24, 2026 23:44
@github-actions
Copy link
Contributor

According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅

@ilevkivskyi
Copy link
Member Author

@JukkaL To speed things up I am going to merge this soon. Please feel free to take a look later, but as I mentioned, after I refactored/unified the fallback logic into a separate helper method, the implementation is trivial (and we seem to agree on the desired semantics).

@ilevkivskyi ilevkivskyi merged commit c35cbd6 into python:master Feb 25, 2026
24 checks passed
@ilevkivskyi ilevkivskyi deleted the func-arg-redefine branch February 25, 2026 00:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

--allow-redefinition-new does not mean "new" implementation but "new" variables

1 participant