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
Refactored extra types to use a single enum #352
Conversation
Still need to add new unit tests, docs and deprecation warning but I wanted to get early feedback. |
Codecov Report
@@ Coverage Diff @@
## master #352 +/- ##
=====================================
Coverage 100% 100%
=====================================
Files 14 14
Lines 1910 1934 +24
Branches 370 375 +5
=====================================
+ Hits 1910 1934 +24 |
@samuelcolvin about the failing test: def test_allow_extra_deprecated():
"""Will be removed in a future release"""
class Model(BaseModel):
a: float = ...
class Config:
allow_extra = True
assert Model(a='10.2', b=12).dict() == {'a': 10.2, 'b': 12} I'm not sure how this passed before since the default of |
|
If the default of ignore_extra is True and allow_extra is True, doesn't that mean that extra attributes will be ignored on init but allowed on mutation? |
No, see #345 which is merged but not yet deployed. They both apply on init. |
Ah, I see. So to better reflect this in my logic:
Ignore on init, fail on mutate
Always fail on extra
Always allow
Also always allow. Correct? If so, that's kinda weird. Are you sure you want to keep this behaviour like this? |
pydantic/main.py
Outdated
@@ -19,14 +20,26 @@ | |||
from .validators import dict_validator | |||
|
|||
|
|||
class ExtraAttributes(Enum): | |||
DISALLOW_MUTATION = auto() |
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 don't think we should have different behaviour for mutation and init, Just ignore
and allow
should suffice.
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.
also, please use lower case.
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.
sorry, ignore
, allow
and disallow
.
But since we're making a change, maybe ignored
, allowed
and forbidden
would be clearer.
pydantic/main.py
Outdated
@@ -19,14 +20,26 @@ | |||
from .validators import dict_validator | |||
|
|||
|
|||
class ExtraAttributes(Enum): |
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.
IntEnum
please and no need for auto. 1
and 2
should be fine.
That way people aren't forced to import to use this.
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.
On reflection, maybe better to use (str, Enum)
and have values of allowed
, ignored
and forbidden
- should be clearer for people to use.
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.
you mean like this?
class Extra(str, Enum):
allowed = 'allowed'
ignored = 'ignored'
forbidden = 'forbidden'
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.
yes
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.
Hmm, how is that exactly used?
pydantic/main.py
Outdated
@@ -402,28 +415,54 @@ def validate_model(model, input_data: dict, raise_exc=True): # noqa: C901 (igno | |||
""" | |||
validate data against a model. | |||
""" | |||
def _deprecated_values(config) -> bool: | |||
""" |
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.
No need for a separate method, also move the depeciation logic to the __new__
method.
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.
Not sure where you mean
tests/test_main.py
Outdated
@@ -179,7 +179,9 @@ class Model(BaseModel): | |||
assert Model().c == 0 | |||
|
|||
|
|||
def test_allow_extra(): | |||
def test_allow_extra_deprecated(): | |||
"""Will be removed in a future release""" |
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.
please capture warnings and check check they're right.
Mutation and init should have the same (or equivalent) behaviour:
|
I like that, but this changes the default behaviour. You want it to be |
Default should be If there are changes to current behaviour, just include "breaking change" in history. |
If we break behaviour then let's get rid of I vote for this option BTW, the old behaviour was kinda confusing, which is the motivation of this. This new behaviour is way more clearer and explicit. |
I don't think we do break current behaviour with the new setting as |
This condition which would ignore on init but fail on mutate would not be supported anymore. |
Ok, but that's a more subtle change than just removing Please keep those settings but with a deprecation warning, and add |
So what's the expected behaviour in that scenario? Should it correlate to ignore or forbidden? |
You don't need to change the current behaviour for If extra is |
Probably easier at this stage to commit to your PR than try to explain what I mean in comments. |
sorry about the formatting |
guess you took it over completely... |
docs/index.rst
Outdated
:allow_extra: whether or not to allow (and include on the model) any extra values from input data; | ||
when ``allow_extra`` option is set to ``True`` it overrides ``ignore_extra`` (default: ``False``) | ||
:extra: whether to ignore, allow or forbid extra attributes in model. Can use either string values of ``ignore``, | ||
``allow`` or ``forbidden``, or use ``Extra`` enum (default is ``Extra.ignore``) |
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.
Currently, the values are ignored
, allowed
or forbidden
.
However looking at it again ignore
, allow
and forbid
might make more sense and would be closer to the previous usage. Choice is yours.
Thanks a lot, much appreciated.
I quite understand that and I'm very sorry. That wasn't my intention and I agree with you that welcoming first-time contributors is key to the success of open source projects, I hope you can see that from #308, #341, #320 or #179. This is not a criticism but rather an explanation for why I rather took over: very open-ended questions like "Not sure where you mean" and "Hmm, how is that exactly used?" frustrated me slightly and made me think that explaining what I meant was going to take a very long time, I assumed (perhaps wrongly) that you either didn't have very much experience or hadn't taken long to look into it yourself. I then decided to move the core logic into I also think that contributing to pydantic could be made easier with some documentation, for example, you might not have been aware of Again, sorry not to be more welcoming and thank you very much for your contribution. |
You've missed renaming in a few places, also linting is failing. If it's a pain to fix, I'll do it in a couple of days. |
I plan to fix it, not feeling too hot these days. If it can wait a few days, I'll do it then. Also, I plan to respond to your last comment, but like I said, I'm too sick now... |
great. |
About your reply: I can understand now that my questions were too open ended like you said, and perhaps were better suited in an ongoing discussion in chat room instead of comments to a PR. I am by no means a new python developer (you can browse my Github profile) but I admit that I was relying more on you answering my question than really trying to dig in the code, and I also take responsibility about that. I can also suggest that other than the suggestion you already made to improve first time contributions, that you also add docstrings, at the very least in crucial parts of the logic, like the metaclass and other non trivial sections of the code. Anyway, I accept your apology and take responsibility in my part of the "problem". |
Hi @liiight, any change you could fix this? It'd be great to get it merged. If it's a pain, I'll happily do it in a day or two. |
I'm sorry, I had a lot of stuff to catch up on due to my sickness. I'll get
this done ASAP
On Jan 26, 2019 20:09, "Samuel Colvin" <notifications@github.com> wrote:
Hi @liiight <https://github.com/liiight>, any change you could fix this?
It'd be great to get it merged.
If it's a pain, I'll happily do it in a day or two.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#352 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AELANdK1ZInoFGwIfrprzeLS2fDIU7SGks5vHJnkgaJpZM4Z0ftB>
.
|
To avoid this PR having loads of conflicts, I'm waiting to merge it before merging type hints #373. Unfortunately, that means this is delaying a big improvement to pydantic. Normally in this situation, I'd take over and get the PR finished. However given ⬆️, I'm a little 😨 😨 😨 👻 about doing that. @liiight therefore please finish this or let me know if you'd like me to finished it. Again thank you for your contribution and appologies for nagging. 🙏 |
I apologize for the delays and I oblige to finish it within the next 24 hours |
thanks. |
Looks done to me @samuelcolvin |
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.
Tiny change you can just accept, then I'll merge.
Co-Authored-By: liiight <4374581+liiight@users.noreply.github.com>
awesome. Thank you very much. |
Sure. Let's hope the next PR wont be this difficult 😂 |
Change Summary
Use a single value to describe allowing extra attributes or ignoring them, by using an enum:
Related issue number
#351
Checklist
HISTORY.rst
has been updated#<number>
@<whomever>