Conversation
|
@pawamoy All the test fail on 3.15 but I guess that's known? |
|
@watermarkhu thanks for the PR 🙂 Yes tests on 3.15 are allowed to fail. I see a few style changes related to line length, could you revert those? |
Then shouldn't we avoid re-assigning attribute names, and display the aliased names via the custom template provided by the extension instead? |
|
@pawamoy I've refactored it now to not hijack the key-mapping of the attributes, but to forward the attribute to use an attribute template that comes with Do you agree with this direction? |
In Pydantic there is now the distinction between My argument for only looking at the The main thing is that the documentation should reflect how a client would interact with the model. The aliases are typically a way to represent attributes in a more human way, compared to the typically snake_case attribute names. So it should be presented more front and center when documented via mkdocstrings and griffe-pydantic. Any opinions? |
I see two things here.
I believe it's best to display both at once. Actual attribute names, for regular API use, and both input and output alias names, for potential clients. Let me check the new template 🙂 |
|
OK so I feel like the best thing to do here is:
Have a look at the template for models: it inherits parent blocks from the class template. We should do the same for the field template: it should inherit from the attribute template. This way it's automatically kept in sync with upstream changes (and it will be much lighter). |
|
Thanks, it's starting to be in good shape 🚀! I figured we could by default always show aliases: that's what the extension is for, showing Pydantic stuff with all the relevant info, so I don't think we need a flag here 🙂 I also figured we could always render fields with the field template. I'll push changes that reflect that. |
| # Process model fields that may not have been discovered by griffe | ||
| if hasattr(obj, "model_fields") and isinstance(obj.model_fields, dict): | ||
| for field_name, field_info in obj.model_fields.items(): | ||
| if field_name not in cls.all_members: | ||
| # Create an Attribute object for this field | ||
| attr = Attribute( | ||
| name=field_name, | ||
| lineno=0, | ||
| endlineno=0, | ||
| ) | ||
| cls.members[field_name] = attr # ty: ignore[invalid-assignment] | ||
| _process_attribute(field_info, attr, cls, processed=processed) |
There was a problem hiding this comment.
Can you explain why we need this? This sounds like something we should potentially add in a separate PR before merging this one.
There was a problem hiding this comment.
I think I had some issues with the fields showing up during the dynamic search. But I think I was still struggling with the logic from common. It is indeed not needed.
There was a problem hiding this comment.
If it's not needed, can we remove it?
There was a problem hiding this comment.
So actually, we do need them. During dynamic analysis or search_sys_path=True, the model fields are not showing up in all_members. I've added a test in dc535b1 to check the model fields of a previously existing test class.
|
Indeed, always showing the alias should be the behavior. I was still fixed on having the option because it replaced the shown field initially. I've made a final(?) edit that just shows the |
For reviewers
Description of the change
For documentation purpose, it might be helpful to serialize the documented model on the alias. I've opted for the
serialization_aliasfield property, which should map what the output field name of the model should be.Originally, I was using the model config value
serialize_by_aliasto detect whether a field name should be serialized by the alias in the documentation. But eventually, it seemed more logical to put it as a extension config item instead, separating the model output alias serialization and the documentation field alias serialization.Relevant resources