-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Description
Initial Checks
- I have searched Google & GitHub for similar requests and couldn't find anything
- I have read and followed the docs and still think this feature is missing
Description
Thank you for making such an amazing tool.
I have a feature request in regards to aliases. My use case is that I need to use aliases for several of my models to interact with the AWS API. I am representing AWS resources such as:
class Ec2Instance(BaseModel):
arn: Optional[str] = Field(alias="Arn")
"""Resource ID for the Instance"""
instance_type: str = Field(alias="InstanceType")
"""The instance type such as t2.micro, t3.xlarge, etc."""
...more fields...
class Config:
allow_population_by_field_name = TrueI need these aliases because this library is used to fetch data via the AWS API, which returns dictionaries with PascalCase keys and the aliases are much more Pythonic than converting each key or using PascalCase for field names.
In addition to fetching data, these models are also used for creating new resources. With the Ec2Instance example above, a user would define all required fields when creating the resource. This library is used by users without expertise of the domain, and the entire point of the library is to make things easier by adding another layer of abstraction where they can use intellisense and inline documentation instead of needing to open the AWS docs in a browser.
Currently, it is almost there, they can hover over parameters when instantiating an object from the model to see the inline docs, but the issue is that intellisense will suggest the PascalCase aliases instead of the field names. Since the Python convention is to use lower_case_with_underscores, I would like to change this behavior.
The workaround the I currently have implemented is ugly and violates the DRY principal:
def aliasing(alias: str) -> str:
return alias
class Ec2Instance(BaseModel):
arn: Optional[str] = Field(alias=aliasing("Arn"))
"""Resource ID for the Instance"""
instance_type: str = Field(alias=aliasing("InstanceType"))
"""The instance type such as t2.micro, t3.xlarge, etc."""
...more fields...
class Config:
allow_population_by_field_name = TrueWith this workaround, I get the desired behavior. Is this something that could be added to the model Config? I know you are hesitant to increase the parameters in the model Config, but I would argue that this change will help keep code Pythonic in certain circumstances.
If you are willing to accept a PR then I can make some time to implement this over the next several weeks.
Affected Components
- Compatibility between releases
- Data validation/parsing
- Data serialization -
.model_dump()and.model_dump_json() - JSON Schema
- Dataclasses
- Model Config
- Field Types - adding or changing a particular data type
- Function validation decorator
- Generic Models
- Other Model behaviour -
model_construct(), pickling, private attributes, ORM mode - Plugins and integration with other tools - mypy, FastAPI, python-devtools, Hypothesis, VS Code, PyCharm, etc.