[wiz-cli] duplicate dataclass schemas should be replaced with just one
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 250
- Forks
- 36
- PR merge metrics
- No merged PRs in 30d
Description
- Dataclass Wizard version: 0.22.1
- Python version: 3.10
- Operating System: Mac OS
Description
In certain cases - and especially in certain API responses, most notably for AWS Rekognition - the input JSON object can contain multiple definitions for the same field - for ex. "element", all of which contain an identical schema.
I'd like to eliminate those duplicate dataclass definitions in the output, so that the generated schema is a bit less verbose and we only have the data we care about.
For example, note the below sample input and output.
What I Did
I ran the following command from my mac terminal:
echo '{
"element": {
"my_str": "string",
"my_int": 3
},
"Elements": [
{
"my_str": "hello",
"my_int": 5
},
{
"myStr": "world",
"MyInt": 7
}
],
"other_field": {
"element": {
"my_str": "other string",
"my_int": 42
}
}
}' | wiz gs
The generated output is a bit noisy in this scenario, as it contains duplicate definitions of the dataclass Element:
from dataclasses import dataclass
from typing import List
from dataclass_wizard import JSONWizard
@dataclass
class Data(JSONWizard):
"""
Data dataclass
"""
element: 'Element'
elements: List['Element']
other_field: 'OtherField'
@dataclass
class Element:
"""
Element dataclass
"""
my_str: str
my_int: int
@dataclass
class Element:
"""
Element dataclass
"""
my_str: str
my_int: int
@dataclass
class OtherField:
"""
OtherField dataclass
"""
element: 'Element'
@dataclass
class Element:
"""
Element dataclass
"""
my_str: str
my_int: int
I'd like to eliminate all the duplicate definitions - preferably trim any duplicates after the first dataclass schema for Element.
Resolution
There are multiple ways to achieve this, but I think the easiest might be to store the generated string or __repr__ for the schema in a dict with the class name as the key, and then lookup and compare if those string defintions are the same. If so, we just continue and return an empty __repr__ after the first time. If not, we generate all the field names and types for the dataclass as normal.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by running the provided wiz gs command with the sample JSON and inspect the generated dataclass output. Trace the schema-generation path from that CLI entry point, then verify that identical Element schemas appear only once while distinct schemas remain represented.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- cli
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 45/100