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): duplicated kwargs when field is multi-inherited #2654

Merged
merged 13 commits into from
Mar 18, 2021

Conversation

RomainMuller
Copy link
Contributor

When a struct field is inherited from more than one parent (this could
be twice from the same type - in the case of a diamond shape; or from
two distinct parents that declare the same field), the lifted keyword
arguments in python would be duplicated for this field.

This is because the method that performs the keyword argument lifting
did not perform name-based de-duplication, and operates directly on the
"raw" assembly (whereby it must traverse the inheritance tree itself,
as opposed to the Go generator which uses jsii-reflect and has the
field collection done by that library).

Added the necessary de-duplication logic and confirmed the produced code
now looks correct using the exact same test harness as I had introduced
in #2650.

Fixes #2653


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

When a struct field is inherited from more than one parent (this could
be twice from the same type - in the case of a diamond shape; or from
two distinct parents that declare the same field), the lifted keyword
arguments in python would be duplicated for this field.

This is because the method that performs the keyword argument lifting
did not perform name-based de-duplication, and operates directly on the
"raw" assembly (whereby it must traverse the inheritance tree itself,
as opposed to the Go generator which uses `jsii-reflect` and has the
field collection done by that library).

Added the necessary de-duplication logic and confirmed the produced code
now looks correct using the exact same test harness as I had introduced
in #2650.

Fixes #2653
@RomainMuller RomainMuller requested a review from a team March 5, 2021 09:24
@RomainMuller RomainMuller self-assigned this Mar 5, 2021
@mergify mergify bot added the contribution/core This is a PR that came from AWS. label Mar 5, 2021
@nija-at nija-at added the pr/do-not-merge This PR should not be merged at this time. label Mar 8, 2021
const knownProps = new Set<string>();
for (
let current = stack.shift();
current != null;
Copy link
Contributor

Choose a reason for hiding this comment

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

Why change from undefined to null. Per docs, this will return undefined when empty?

Maybe just check for truthiness instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

x != null is the same as x !== nul && x !== undefined, but more compact.

And I don't like truthiness checks, because this is ripe for bugs.

@@ -64,6 +64,7 @@
"@types/yargs": "^16.0.0",
"eslint": "^7.21.0",
"jest": "^26.6.3",
"jsii": "^0.0.0",
Copy link
Contributor

Choose a reason for hiding this comment

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

Intentional? pacmak depends on jsii?

Not familiar with the jsii layering but smells fishy.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

New tests actually use jsii to compile some tiny projects to validate code-gen output. This is very practical.

@@ -0,0 +1,1437 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
Copy link
Contributor

Choose a reason for hiding this comment

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

Does this require another new snapshot test?

Did this not manifest in the previous snapshot test for diamond dependencies - https://github.com/aws/jsii/pull/2591/files#diff-305aec203005012473bdfe3d0c5d9c734c7ee318a0e813759c82aed7067323e3 ?

Snapshot tests make it really hard to evaluate if a problem was actually fixed. It's useful to detect unintended regressions but perhaps it shouldn't be the sole testing mechanism.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The alternative is to build (I mean compile) the generated code, for possibly many small projects, to ensure they "work" -- this is bound to be very slow/expensive. Also - next up is "but the fact the code compiles is not enough - we have to run it, too", and then it becomes a full time job?

@mergify
Copy link
Contributor

mergify bot commented Mar 12, 2021

The title of this Pull Request does not conform with [Conventional Commits] guidelines. It will need to be adjusted before the PR can be merged.
[Conventional Commits]: https://www.conventionalcommits.org

# Conflicts:
#	packages/jsii-pacmak/test/generated-code/__snapshots__/examples.test.ts.snap
#	packages/jsii-pacmak/test/generated-code/examples.test.ts
@RomainMuller RomainMuller force-pushed the rmuller/diamond-python branch from 7a11eb7 to 08ccae9 Compare March 15, 2021 12:46
@RomainMuller RomainMuller removed the pr/do-not-merge This PR should not be merged at this time. label Mar 16, 2021
@mergify
Copy link
Contributor

mergify bot commented Mar 16, 2021

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 Mar 16, 2021
@mergify
Copy link
Contributor

mergify bot commented Mar 16, 2021

Merging (with squash)...

@mergify
Copy link
Contributor

mergify bot commented Mar 17, 2021

Merging (with squash)...

@RomainMuller RomainMuller merged commit 3cd9d19 into main Mar 18, 2021
@RomainMuller RomainMuller deleted the rmuller/diamond-python branch March 18, 2021 09:39
@mergify mergify bot removed the pr/ready-to-merge This PR is ready to be merged. label Mar 18, 2021
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.

Struct multi-inheritance generates wrong Python code
2 participants