Skip to content
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

Properly set Config in create_model #320

Merged
merged 12 commits into from Dec 7, 2018

Conversation

@hugoduncan
Copy link
Contributor

@hugoduncan hugoduncan commented Dec 6, 2018

Change Summary

Set the Config attribute in create_model, so it is found by the
MetaModel. Pushes the field and validator configuration into the
metaclass.

Related issue number

None

Performance Changes

Not run.

Checklist

  • Unit tests for the changes exist
  • Tests pass on CI and coverage remains at 100%
  • Documentation reflects the changes
  • No performance deterioration (if applicable)
  • HISTORY.rst has been updated
    • if this is the first change since a release, please add a new section
    • include the issue number or this pull request number #<number>
    • if you're not a regular contributer please include your github username @whatever
hugoduncan added 2 commits Dec 6, 2018
Set the Config attribute in create_model, so it is found by the
MetaModel.
@codecov
Copy link

@codecov codecov bot commented Dec 6, 2018

Codecov Report

Merging #320 into master will not change coverage.
The diff coverage is 100%.

@@          Coverage Diff          @@
##           master   #320   +/-   ##
=====================================
  Coverage     100%   100%           
=====================================
  Files          13     13           
  Lines        1761   1771   +10     
  Branches      340    344    +4     
=====================================
+ Hits         1761   1771   +10

Copy link
Owner

@samuelcolvin samuelcolvin left a comment

Thanks for the changes, overall this looks like a good idea. Thanks.

There are a few changes here that require tests:

  • validator inheritance in the standard model inheritance case, with and without further subsequent validators on the same field in inheriting models
  • validators in create_model

@@ -100,17 +100,25 @@ def _extract_validators(namespace):
return validators


def inherit_validators(base_validators, validators):
Copy link
Owner

@samuelcolvin samuelcolvin Dec 6, 2018

better if this method returns validators so it's clear what's going on.

pydantic/main.py Outdated
@@ -100,17 +100,25 @@ def _extract_validators(namespace):
return validators


def inherit_validators(base_validators, validators):
for field, field_validators in base_validators.items():
validators.setdefault(field, []).extend(field_validators)
Copy link
Owner

@samuelcolvin samuelcolvin Dec 6, 2018

better to be explicit

if field in validators:
    validators[field] += field_validators
else:
    validators[field] = field_validators

pydantic/main.py Outdated
annotations[f_name] = f_annotation
fields[f_name] = f_value

namespace = {"__annotations__": annotations, "__validators__": vg.validators}
Copy link
Owner

@samuelcolvin samuelcolvin Dec 6, 2018

single quotes please.

pydantic/main.py Outdated
namespace = {'config': config, '__fields__': fields}
# f_validators = vg.get_validators(f_name)
# if f_validators:
# setattr(f_value, "__validator_config", f_validators)
Copy link
Owner

@samuelcolvin samuelcolvin Dec 6, 2018

Is this definitely not required? if so please remove.

Copy link
Contributor Author

@hugoduncan hugoduncan Dec 6, 2018

I'll strip out some more incorrect validator logic.

ignore_extra = False
allow_extra = False

model = create_model("FooModel", foo=(int, ...), __config__=Config)
Copy link
Owner

@samuelcolvin samuelcolvin Dec 6, 2018

I haven't got around to finding a way to lint this, but pydantic should use single quotes always unless single quotes are used in the string.

Copy link
Contributor Author

@hugoduncan hugoduncan Dec 6, 2018

We struggled with this with black as well. Ended up just using the Black convention.

Copy link
Owner

@samuelcolvin samuelcolvin left a comment

Still needs the extra tests mentioned above, otherwise looking good.

pydantic/main.py Outdated
@@ -100,17 +100,30 @@ def _extract_validators(namespace):
return validators


def inherit_validators(base_validators, validators):
for field, field_validators in base_validators.items():
if field in validators:
Copy link
Owner

@samuelcolvin samuelcolvin Dec 7, 2018

if you want to do it this way

if field not in validators:
    validators[field] = []
validators[field] += field_validators

Copy link
Contributor Author

@hugoduncan hugoduncan Dec 7, 2018

This is to avoid mutating the validators in the base classes.

a = []
b = a
b += [1]
assert a == [1]

Copy link
Owner

@samuelcolvin samuelcolvin Dec 7, 2018

what I've suggested has exactly the same effect as what you have now, just avoids duplicated lines.

pydantic/main.py Outdated

config = inherit_config(namespace.get('Config'), config)
vg = ValidatorGroup(_extract_validators(namespace))
inherit_validators(_extract_validators(namespace), validators)
Copy link
Owner

@samuelcolvin samuelcolvin Dec 7, 2018

validators =

@hugoduncan
Copy link
Contributor Author

@hugoduncan hugoduncan commented Dec 7, 2018

@samuelcolvin I've extended the two existing validation test using inheritance. Is that sufficient or did you have something further in mind?

@samuelcolvin
Copy link
Owner

@samuelcolvin samuelcolvin commented Dec 7, 2018

Definitely requires more tests, from above:

There are a few changes here that require tests:

  • validator inheritance in the standard model inheritance case, with and without further subsequent validators on the same field in inheriting models
  • validators in create_model

@hugoduncan
Copy link
Contributor Author

@hugoduncan hugoduncan commented Dec 7, 2018

I've added some tests for child classes using validators from parent classes, to complement the existing tests for adding validation in child classes.

  • validators in create_model

Do you have specific test cases in mind that aren't covered by test_inheritance_validators and test_inheritance_validators_all?

@@ -353,6 +353,65 @@ def check_a_two(cls, v):
Child(a='snap')


def test_validate_parent_all():
Copy link
Owner

@samuelcolvin samuelcolvin Dec 7, 2018

looks like this test and the one at the bottom should swap name, so this becomes test_validate_child_all.

@samuelcolvin
Copy link
Owner

@samuelcolvin samuelcolvin commented Dec 7, 2018

Do you have specific test cases in mind

yes, a test where we have a model with validators, then us that as a base in create_model and check the validators are called correctly on the created model.

I'm not saying it'll add any specific coverage (it can't since we have 100% coverage), but since we think that should be possible we should have a test of doing it.

@hugoduncan
Copy link
Contributor Author

@hugoduncan hugoduncan commented Dec 7, 2018

yes, a test where we have a model with validators, then us that as a base in create_model and check the validators are called correctly on the created model.

I've added some detail around the test_inheritance_validators test, adding a case for always=True. Otherwise I'm struggling to see how this doesn't cover what you describe.

@samuelcolvin
Copy link
Owner

@samuelcolvin samuelcolvin commented Dec 7, 2018

my mistake, sorry.

LGTM, I'll look full and merge early next week.

@samuelcolvin samuelcolvin merged commit 3249330 into samuelcolvin:master Dec 7, 2018
7 checks passed
@samuelcolvin
Copy link
Owner

@samuelcolvin samuelcolvin commented Dec 7, 2018

great, thank you very much.

Will deploy in a week or two, unless you need it urgently in which case let me know and I'll deploy sooner.

@hugoduncan
Copy link
Contributor Author

@hugoduncan hugoduncan commented Dec 8, 2018

Thank you - the review process was definitely useful! If you could manage to release sooner it would save me the work of deploying an internal fork.

@samuelcolvin
Copy link
Owner

@samuelcolvin samuelcolvin commented Dec 8, 2018

Will do.

@samuelcolvin
Copy link
Owner

@samuelcolvin samuelcolvin commented Dec 10, 2018

done.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

None yet

2 participants