[wiz-cli] the wiz
tool shouldn't use any duplicate dataclass names
#58
Labels
bug
Something isn't working
good first issue
Good for newcomers
help wanted
Extra attention is needed
self-created
Opened by me!
Description
I found an interesting case where the wrong dataclass schema is generated with the
wiz
command-line tool. The schema is wrong insofar as that the same dataclass name -Ability
in this case - is reused between generated dataclasses in a nested dataclass structure. This obviously won't allow us to actually load JSON data to the main dataclassData
, because the nested schema has a bug (duplicate name for dataclass) as indicated.See output below for more details.
What I Did
I ran
wiz gs
from my terminal, with the following JSON data as input:The result generated the following nested dataclass schema:
As you can already see, the
Ability
dataclass name is duplicated. This results in the second class definition overwriting the previous one, which is what we would like to avoid in this case.I've then tried the following Python code with the generated class schema above:
Which resulted in an error, as expected, since the second dataclass definition overwrites the first one:
Renaming the second class to something else, for example
Ability2
, and then updating any references to it, then works as expected to load the JSON input data to a dataclass instance.Resolution
One possible approach that comes to mind, is to keep a hashset of all generated class names - or better yet a
dict
mapping of class name to a count of its occurrences, or how many times we've seen it - maybe at the module or global scope, or perhaps recursively pass it in through each function in the generation process.Then check if the class name we want to add is already in the mapping, and if so we add some random suffix (for example increment by the count of how many times we've already seen the class name) to the generated class name; if not, we use the generated class name as-is. In any case, we then increment the count of the class name in the mapping, to indicate that we've seen it previously.
The text was updated successfully, but these errors were encountered: