Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(python): type-checking may require incorrect type #3820

Merged
merged 2 commits into from
Nov 2, 2022

Conversation

RomainMuller
Copy link
Contributor

Since dae724c, Python type-checking
relies on a nested stub function as a type annotations source. The
parameter signature of that stub was copied verbatim from the
surrounding function, including any forward type references.

However, the forward references can safely be replaced with regular type
references as the module is guaranteed to be fully loaded by the time
the stub is created, and using forward-references there results in
typeguard possibly evaluating those in a different context than the
one where the surrounding function was defined. The consequence of this
is that multiple different foward references by the same name may be
incorrectly treated as referring to the same type, despite coming from
different modules.

This is fixed by turning forward type references in the stub with
regular type references (in other words, removing any " from the
parameter signature of the stub), which lets the type be resolved from
the local definition context instead of the final runtime context in
which the function is called.

Fixes #3818


By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

Since dae724c, Python type-checking
relies on a nested stub function as a type annotations source. The
parameter signature of that stub was copied verbatim from the
surrounding function, including any forward type references.

However, the forward references can safely be replaced with regular type
references as the module is guaranteed to be fully loaded by the time
the stub is created, and using forward-references there results in
`typeguard` possibly evaluating those in a different context than the
one where the surrounding function was defined. The consequence of this
is that multiple different foward references by the same name may be
incorrectly treated as referring to the same type, despite coming from
different modules.

This is fixed by turning forward type references in the stub with
regular type references (in other words, removing any `"` from the
parameter signature of the stub), which lets the type be resolved from
the local definition context instead of the final runtime context in
which the function is called.

Fixes #3818
@RomainMuller RomainMuller requested a review from a team November 2, 2022 09:33
@RomainMuller RomainMuller self-assigned this Nov 2, 2022
@mergify mergify bot added the contribution/core This is a PR that came from AWS. label Nov 2, 2022
Copy link

@Naumel Naumel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🪞

Copy link
Contributor

@rix0rrr rix0rrr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two questions:

  • Can you quantify the effect on load time of the CDK library with this change?
  • Since this was a run-time error, shouldn't there be a test that calls the generated Python code?

@rix0rrr rix0rrr added the pr/do-not-merge This PR should not be merged at this time. label Nov 2, 2022
@rix0rrr
Copy link
Contributor

rix0rrr commented Nov 2, 2022

Romain indicated to me in private chat that he believes this will not cause performance impact.

Don't want to hold up the fix if it's legit, since people are waiting for it.

@rix0rrr rix0rrr removed the pr/do-not-merge This PR should not be merged at this time. label Nov 2, 2022
@mergify
Copy link
Contributor

mergify bot commented Nov 2, 2022

Thank you for contributing! ❤️ I will now look into making sure the PR is up-to-date, then proceed to try and merge it!

@mergify mergify bot added the pr/ready-to-merge This PR is ready to be merged. label Nov 2, 2022
@mergify
Copy link
Contributor

mergify bot commented Nov 2, 2022

Merging (with squash)...

@mergify mergify bot merged commit e9d4084 into main Nov 2, 2022
@mergify mergify bot deleted the rmuller/fix-python-decorated-type-checking branch November 2, 2022 14:17
@mergify mergify bot removed the pr/ready-to-merge This PR is ready to be merged. label Nov 2, 2022
@RomainMuller
Copy link
Contributor Author

  • Can you quantify the effect on load time of the CDK library with this change?

I have no infrastructure to produce a reliable, comparable, reproductible benchmark of load times. But I could give it a shot in a somewhat un-scientific manner... But what are you concerned about here? That the new code will take significantly longer to load than the previous code? I don't see how this could be the case (or else, the Python VM would be utter rubbish, which it isn't)

  • Since this was a run-time error, shouldn't there be a test that calls the generated Python code?

There is already one added as part of the PR in packages/@jsii/python-runtime/tests/test_runtime_type_checking.py.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
contribution/core This is a PR that came from AWS.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

jsii requires the wrong type in some cases
3 participants