Repository navigation
mypyc: Use widened parameter types in native signatures - #22228
Open
charan-rathore wants to merge 3 commits into
Open
charan-rathore wants to merge 3 commits into
charan-rathore wants to merge 3 commits into
Conversation
Updated argument type retrieval in fdef_to_sig to use variable types instead of declared types, addressing issue python#22221.
Add a test case to verify widening of native signatures under redefinition.
for more information, see https://pre-commit.ci
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #22221.
With
--allow-redefinition, mypy widens a redefined parameter's variable type to the join of the declaration and every redefinition (e.g.n: intlater assignedobject()becomesunion[int, object]). mypyc creates the argument's local register and any closure environment attribute with that widened type, butMapper.fdef_to_sigbuilt the native signature from the declared Callable type. The generated C entry then received the narrow declared type (CPyTagged) and stored it straight into the wider environment attribute (PyObject *), which fails to compile:Now each argument's runtime type comes from the parameter variable's type, falling back to the declared Callable type when the variable has none. Without
--allow-redefinitionthe variable type equals the declared type, so existing signatures are unchanged.One behavior note: the native wrapper for such a function now accepts any object for a widened parameter (mypy still rejects non-conforming calls statically). That matches the flag's semantics - the variable can hold non-declared values at runtime.
Tested:
f(3) == 2).pytest mypyc/test/test_irbuild.py: 783 passed.Prepared with AI assistance (Instinct assisted).