-
Notifications
You must be signed in to change notification settings - Fork 245
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(python): type-checking may require incorrect type (#3820)
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]. [Apache 2.0 license]: https://www.apache.org/licenses/LICENSE-2.0
- Loading branch information
1 parent
2dd0209
commit e9d4084
Showing
17 changed files
with
2,162 additions
and
74 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
Verifies homonymous forward references don't trip the Python type checker | ||
|
||
This has been an issue when stub functions were introduced to create a reliable source for type checking | ||
information, which was reported in https://github.com/aws/jsii/issues/3818. |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
export interface Homonymous { | ||
readonly numericProperty: number; | ||
} | ||
|
||
export interface ConsumerProps { | ||
readonly homonymous: Homonymous; | ||
} | ||
|
||
export class Consumer { | ||
public static consume(props: ConsumerProps) { | ||
return props.homonymous; | ||
} | ||
|
||
private constructor() {} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
export interface Homonymous { | ||
readonly stringProperty: string; | ||
} | ||
|
||
export interface ConsumerProps { | ||
readonly homonymous: Homonymous; | ||
} | ||
|
||
export class Consumer { | ||
public static consume(props: ConsumerProps) { | ||
return props.homonymous; | ||
} | ||
|
||
private constructor() {} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * as bar from './bar'; | ||
export * as foo from './foo'; |
This file contains 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
This file contains 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
This file contains 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
Oops, something went wrong.