fix!: python models not handling circular model dependencies #1946
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.
Description
This PR fixes two major problems with the Python generator; self-referencing models give errors and circular model dependencies do not work.
Self-referencing models
If a model depends on itself the current dependency setup will not work in Python. This fixes it by changing the import style to using forward references (
forward references: when a type hint contains names that have not been defined yet, that definition needs to be expressed as a string literal;
) through the new Postponed Evaluation of Annotations.I tried to experiment with all kinds of different solutions, such as those highlighted here: https://stackoverflow.com/a/55344418/6803886
However, this one was the most straightforward solution. It does mean that we add the requirement of using at least Python 3.7, which is why this is breaking.
Circular model dependencies
If we have two models, that each other reference each other in a circular manner Python will complain about it because of the current import style. Going forward we only support explicit import style to allow for this use-case.
Related Issues
Fixes #1940
Fixes #1814