Open jhammarstedt opened 1 year ago
I need default factories for generating "@dataclass" classes like this:
Current output:
@dataclass
class CustomOriginConfig:
OriginProtocolPolicy: str
HTTPPort: Optional[int] = 80
HTTPSPort: Optional[int] = 443
OriginKeepaliveTimeout: Optional[int] = 5
OriginReadTimeout: Optional[int] = 30
OriginSSLProtocols: Optional[List[str]] = ['TLSv1', 'SSLv3']
but this doesn't pass typechecking (running uvicorn)
What is required:
@dataclass
class CustomOriginConfig:
OriginProtocolPolicy: str
HTTPPort: Optional[int] = 80
HTTPSPort: Optional[int] = 443
OriginKeepaliveTimeout: Optional[int] = 5
OriginReadTimeout: Optional[int] = 30
OriginSSLProtocols: Optional[List[str]] = field(default_factory=lambda: ['TLSv1', 'SSLv3'])
Is your feature request related to a problem? Please describe. I'm using pydantic to parse raw data in a dataflow pipeline. This comes with a lot of nested data models where the inner field sometimes will be empty. These will cause error when calling a nested model that does not exist. The current solution is just to use:
x = A.B.c if A.B else None
similar to thex = A.get("B",{}).get("c")
when using dictionaries.Describe the solution you'd like I want a flag
--default-factory
that sets the default value to an empty version of the datamodel:Hence: Currently a normal generation gives:
This works with input json
{"A":{"B": {"c":[]} }}
but I could also get{"A":None}
.By then complementing this solution with a model_validator:
I'm able to safely call anything which will just return a None if the field does not exist.
Describe alternatives you've considered The current solution is just to use:
x = A.B.c if A.B else None
similar to thex = A.get("B",{}).get("c")
when using dictionaries. One could also overwrite the pydantic class methods to do something similar or use Field validators.