fix(jsii-pacmak): inheritance between nested types fails#4993
Merged
mergify[bot] merged 2 commits intomainfrom Nov 27, 2025
Merged
fix(jsii-pacmak): inheritance between nested types fails#4993mergify[bot] merged 2 commits intomainfrom
mergify[bot] merged 2 commits intomainfrom
Conversation
In Python, two types that are nested inside the same other class
couldn't inherit from each other; the type references were rendered
incorrectly.
Specifically, the following:
```ts
class A { }
namespace A {
interface B { }
interface C extends B { }
}
```
Would produce could that doesn't compile correctly. It turns out that
the referencing rules are different during the initialization of
classes:
```py
class A:
class B: pass
class C(B): # <-- short
def some_method(self, b: "A.B"): # <-- long (in strings)
return A.B() # <-- long
```
This PR fixes that, and introduces some simplifications and refactoring
of the type handling code along the way:
- Reversed the order of 2 code blocks in the `UserType.pythonType()` function, to
consistently structure the code as "early exit" paths (`if
(weird_condition) { return ...; }`). This makes the diff bigger,
sorry, but the long-term maintainability of this code better (imo).
- Moved the implicit `typeAnnotation?: boolean` property of the naming
context to an explicit parameter of the rendering function
`pythonType()`.
- We now distinguish 3 ways of rendering types: as type annotations,
as values in methods, as values during class initialization.
- Strictly render all type annotations using quotes. This always works
and is simpler: it used to allow us to get rid of `emittedTypes` which
tracks types we should emit quotes for and types which we can
reference directly... but there are no benefits to direct references,
so why would we?
mrgrain
approved these changes
Nov 27, 2025
Contributor
|
Thank you for contributing! ❤️ I will now look into making sure the PR is up-to-date, then proceed to try and merge it! |
Contributor
Contributor
|
Merging (with squash)... |
Contributor
|
Merging (with squash)... |
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.
In Python, two types that are nested inside the same other class couldn't inherit from each other; the type references were rendered incorrectly.
Specifically, the following:
Would produce could that doesn't compile correctly. It turns out that the referencing rules are different during the initialization of classes:
This PR fixes that, and introduces some simplifications and refactoring of the type handling code along the way:
UserType.pythonType()function, to consistently structure the code as "early exit" paths (if (weird_condition) { return ...; }). This makes the diff bigger, sorry, but the long-term maintainability of this code better (imo).typeAnnotation?: booleanproperty of the naming context to an explicit parameter of the rendering functionpythonType().emittedTypeswhich tracks types we should emit quotes for and types which we can reference directly... but there are no benefits to direct references, so why would we?By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.