Repository navigation
Conversation
Class names are turned into module names by snake_casing them, so schemas named `VLAN` and `Vlan` were both generated into `vlan.py`. The second module silently overwrote the first one, but `models/__init__.py` still imported both classes, so the generated client could not be imported at all. Give every model and enum a module name that no other generated class is using, appending a numeric suffix to conflicts, and emit a warning describing the rename.
Looking up the module name of every already-parsed class for each new class made parsing quadratic in the number of schemas. Keep the used module names in a set on `Schemas` instead.
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.
What was wrong
Module names are snake_case versions of class names, so schemas named
VLANandVlanwere both generated intomodels/vlan.py._build_modelswrites that file once per class, so the second model overwrote the first one, whilemodels/__init__.pystill imported both classes frommodels.vlan:Importing the generated client then failed with
ImportError: cannot import name 'VLAN' from '...models.vlan'.Fix
ensure_unique_module_namenow runs whenever a model or enum class is created: if another generated class alreadyuses the resulting module name, the new class gets a numeric suffix (
vlan_1,vlan_2, ...) and a warning is emitted,as suggested in the issue:
The rename happens before the class is added to
Schemas, so every import generated for references, endpoints andmodels/__init__.pyuses the final module name. Two classes with the exact same name keep the existing behaviour,since those are reported as duplicate model/enum errors.
The
class_overridessection of the README documents the new suffixing behaviour.Tests
tests/test_parser/test_properties/test_schemas.py::TestEnsureUniqueModuleNamefor the helper: no conflict,class name already present, conflict renamed, and picking the next free suffix when
_1is already taken.VLAN,Vlanand a model referencing both, thenimports the three generated models and round-trips data through them
(
end_to_end_tests/functional_tests/generated_code_execution/test_conflicting_module_names.py).The end-to-end test fails without the fix and passes with it:
Full suite, lint, format and type checks were run on Windows. Two
test__run_post_hooks_*tests fail there becausepython3resolves to the Microsoft Store alias on that platform; the same two fail on an unmodified checkout:Fixes #1163
AI was used to write this code.