-
-
Notifications
You must be signed in to change notification settings - Fork 62
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
Add ImportAnnotations configuration option #653
Conversation
This allows schemas having collisions between class members name and type to generate correct Python bindings, by enabling Postponed Evaluation of Annotations (see PEP 563). This is done by adding __future__.annotations as a default import to all generated modules when this option is enabled.
This test generates bindings having a collision between a class member name and its type, but uses the ImportAnnotations configuration option to postpone the evaluation of the type hint. The test then verifies that the binding can be loaded without errors, and that its colliding member can be accessed.
This helps users diagnose and fix name/type collisions by using the ImportAnnotations configuration option.
Codecov Report
@@ Coverage Diff @@
## master #653 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 94 94
Lines 8214 8224 +10
Branches 1583 1585 +2
=========================================
+ Hits 8214 8224 +10
Continue to review full report at Codecov.
|
So |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Awesome job, @teobouvard
Can we please change the name of the new option and avoid adding a new project settings file .xsdata.xml
for the tests and maybe update the docstring because it will appear in the cli,
schema = filepath.joinpath("model.xsd") | ||
runner = CliRunner() | ||
result = runner.invoke( | ||
cli, [str(schema), f"--config={str(filepath.joinpath('xsdata.xml'))}"] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All GeneratorOutput options appear in the cli automatically, no need to add a new config file.
❯ xsdata generate --help
...
...
...
Options:
-r, --recursive Search files recursively in the source
directory
-c, --config TEXT Project configuration
-pp, --print Print output
-p, --package TEXT Target package, default: generated
-o, --output [dataclasses] Output format name, default: dataclasses
--repr / --no-repr Generate __repr__ method, default: true
--eq / --no-eq Generate __eq__ method, default: true
...
...
...
...
--import-annotations / --no-import-annotations
Import annotations from :obj:`__future__` in
generated modules, default: false
--help Show this message and exit.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And fix the help text as well, it comes fromt the docstring
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it possible to document it in such a way that the HTML docs have the link for the __future__
module, but the CLI help is only text ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe there is no need to have implementations details in the docstring. I think the following should be sufficient.
:param postponed_annotations: Enable postponed evaluation of annotations, default: false
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All GeneratorOutput options appear in the cli automatically, no need to add a new config file.
That's awesome ! However, I think I still need the configuration file to set the namecase, which should be originalCase
for the purpose of the test.
Check the xsdata/xsdata/models/config.py Lines 177 to 192 in bdae3a3
|
This informs users that enabling Postponed Evaluation of Annotations is only available for Python 3.7+.
This commit renames references to the newly introduced feature to describe _what_ this feature does rather than _how_ it does it.
This allows users to be informed that PostponedAnnotations are only available in Python 3.7+ by displaying a warning.
Extracting the Python version into a local variable prevents pyupgrade from refactoring version checks and changing the test semantics.
Kudos, SonarCloud Quality Gate passed! 0 Bugs No Coverage information |
Awesome job @teobouvard thanks for contributing! |
📒 Description
This PR adds a new element
ImportAnnotations
to theOutput
section of the configuration file. The goal of this configuration option is to allow schemas introducing collisions between a class member name and its type in the generated bindings to be imported without errors.Resolves #646
🔗 What I've Done
As discussed in the second part of #646, name collisions originally raised a TypeError because type annotations could not be resolved. However, PEP 563 discusses a way to postpone the evaluation of type hints in such a way that name collisions are acceptable. This behavior is optional in Python
3.7.0b1
, and will be mandatory in Python3.10
(see Future statement definitions).This configuration option is disabled by default, and enabling it will add the following import at the top of all generated modules.
This is done by simply prepending the import statement to the
default_imports
filter used in themodule
Jinja template.💬 Comments
Thank you for this library and the effort you're putting towards code quality. Let me know if more tests should be added for this feature.
🛫 Checklist