samuelcolvin / pydantic Public
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
change alias priority logic #1178
Conversation
Codecov Report
@@ Coverage Diff @@
## master #1178 +/- ##
======================================
Coverage 100% 100%
======================================
Files 20 20
Lines 3490 3523 +33
Branches 679 687 +8
======================================
+ Hits 3490 3523 +33
Continue to review full report at Codecov.
|
I personally view this as a bugfix -- I think manually labeled fields should always retain their manually labeled alias unless specifically overridden. It doesn't really surprise me that this hasn't caused issues because I think it is rare that the two are used together. I am sensitive to the idea that it is a breaking change, but in my opinion this seems pretty clearly like incorrect behavior instead of a change from valid implementation to another. It would be different if a lot of people were now dependent on the incorrect logic. That would very much surprise me, but I'm not sure how to confirm it's not the case. Do you have a sense of how hard it would be to raise a model-creation-time warning in the case where this PR would actually result in a different (i.e., incompatible) alias being used? The warning message could say something like |
@dmontagu very interesting. I think in theory the warning approach would be possible, we would change this PR to raise the warning on the new behaviour but not change the alias. I guess we'd then either need to tell people "wait for v1.5 when the new behaviour will come in" or "add the This all sounds like a lot of work and complexity for something which:
I think if we give an appropriate warning in the release notes and are explicit about what's changed, people can't be that upset by the change. |
That seems ok. I could see a pretty reasonable argument it should work that way. I could also see a pretty reasonable argument that children should always override the parent. But following specificity, as you are, seems reasonable to me. So long as it is clearly documented. My 2 cents: I'm not sure how I feel about adding an Thanks for working on this, and for your speed! |
29e0593
to
d016fff
Compare
Done I hope, feedback welcome on the docs.
Me neither, I haven't documented it and I don't think people should ever use
again I completely agree, but that will have to wait for v2, see #1186 For me this is done, any feedback welcome. Otherwise I'll merge tomorrowish. I'm intending to deploy v1.4 fairly soon. |
docs/usage/model_config.md
Outdated
!!! warning | ||
Alias priority logic changed in **v1.4** to resolve buggy and unexpected behaviour in previous versions. | ||
In some circumstances this may represent a **breaking change**, | ||
see [#1178](https://github.com/samuelcolvin/pydantic/issues/1177) and the precedence order below for details. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did you intend to indicate issue 1178
but link to 1177
? Pretty minor since they are related though.
docs/usage/model_config.md
Outdated
In some circumstances this may represent a **breaking change**, | ||
see [#1178](https://github.com/samuelcolvin/pydantic/issues/1177) and the precedence order below for details. | ||
|
||
A field alias's is chosen based on the following |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you meant A field's alias
(or maybe this line is unnecessary/duplicated below)
Sounds great. Documentation looks good. Thanks! |
Hi everyone! I have a base model with aliases that match legacy API. I also have extended models that inherit from that base models and these will be served for client application. Now the problem is that I would like to use alias_generator in that extended models to replace snake case field names with camel case. Obviously, just as explained in the docs, this works only for the fields defined in child model.
Now, the use case for that is as following: |
no way I know of to force use of a different alias. You have the options of:
|
@mat02, if you're still looking for a way to do this, I figured out a reasonable solution using from itertools import chain
from pydantic import BaseModel, create_model
class Bread(BaseModel):
class Config:
alias_generator = lambda s: f"bread_{s}"
name: str
id: int
class Butter(BaseModel):
class Config:
alias_generator = lambda s: f"butter_{s}"
name: str
id: int
print(Butter.__fields__)
print(Bread.__fields__)
class BreadAndButterDoesntWork(Bread, Butter):
"""This won't work :("""
print(BreadAndButterDoesntWork.__fields__)
def BreadAndButterWorks():
return create_model(
"BreadAndButter",
**{
f.alias: (f.type_, ...)
for f in chain.from_iterable(
getattr(m, "__fields__").values() for m in [Bread, Butter]
)
},
)
print(BreadAndButterWorks().__fields__) Hopefully this helps someone! Fwiw, I was dealing with a use case where we are using Pydantic models to get bits of data from various APIs and databases in order to ultimately stitch them together into one big data warehouse table. Imagine in the above, that My experience is limited with this library, and my problem is somewhat unusual, but while I'm here I'll toss 2 cents in the bucket for raising the priority of |
Change Summary
This changes the priority of aliases to fix #1177, however it introduces a potential breaking change.
For example:
pydantic/tests/test_aliases.py
Lines 189 to 204 in 80152f3
before
alias_generator
on the child would take taken priority. While it's arguable whether this is correct and we could allowalias_generator
to return a tuple(<alias>, <alias priority>)
to allow customisation, I'm aware this is a breaking change.However I'm not sure how else to fix the issue in a cleaner way. @tiangolo, @dmontagu, anyone else involved - do you think we should:
?
Checklist
changes/<pull request or issue id>-<github username>.md
file added