-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
Add support for mapping types as custom root #958
Conversation
Codecov Report
@@ Coverage Diff @@
## master #958 +/- ##
=====================================
Coverage 100% 100%
=====================================
Files 19 19
Lines 3299 3293 -6
Branches 651 651
=====================================
- Hits 3299 3293 -6
Continue to review full report at Codecov.
|
704a83e
to
c5f52ad
Compare
c5f52ad
to
4e0daa4
Compare
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.
otherwise LGTM.
On second thoughts I think we should have both tests and documentation describing the peculiarities of root validation if you provide |
I thought about this more while attempting to address the feedback, and I think there is a problem here that we need to address: if the Because of this, I think when the root type is a mapping, we should not have different behavior depending on the presence of the Proposal:
Discussion / Justification:If we had no backwards compatibility requirements, I currently think it would be best to change the behavior so that we always assumed there was no However, given the backwards compatibility requirement, we obviously need to continue supporting both approaches for non-mapping root models. But I think since mapping root-models aren't currently supported, we can be more opinionated here (and just document the choice).
I personally would prefer "Option 2" above, since the handling a dump-reparse cycle seems like a substantially rarer requirement than parsing in the first place, so I'd rather prioritize having a clean API for The behavior would also be easy to change if desired by users by just overriding the relevant methods (i.e., We could also (eventually) address the lack of symmetry by introducing a This could also be addressed by adding an |
I think right now we should do the thing which maximises backwards comparability, that is: if cls.__custom_root_type__:
obj = {ROOT_KEY: obj} This would mean I'm happy to omit the " I think this is the same as your option 2 above, so we're agreeing. |
@samuelcolvin To be clear, is that a v2 change? Otherwise, it could break existing code that relies on this behavior, right? I'm currently planning to use the following check: if cls.__custom_root_type__ and (
not (isinstance(obj, dict) and obj.keys() == {'__root__'})
or cls.__fields__["__root__"].shape == SHAPE_MAPPING
):
obj = {ROOT_KEY: obj} I could add a deprecation warning for the case where |
de2d243
to
781b4b4
Compare
Sorry, I was being dumb. You check looks good. We can change/reconsider before v2. |
@samuelcolvin I added some tests and docs discussing this. Let me know if you think anything is wrong / missing. Otherwise, I think this is ready to merge. (Assuming the cython builds go through...) |
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.
otherwise lgtm.
I need to go to bed now, feel free to finish this up (and #934 if desired) if you notice any remaining minor issues, otherwise I'll finish it some time tomorrow. |
great, thank you so much. |
* Add support for mapping types as custom root * Incorporate feedback * Add changes * Incorporate feedback * Add docs and tests * Fix linting issue * Incorporate more feedback * Add more specific match
* Add support for mapping types as custom root * Incorporate feedback * Add changes * Incorporate feedback * Add docs and tests * Fix linting issue * Incorporate more feedback * Add more specific match
* Refactor ._iter() method: Moved all keys-related stuff (include, exclude, etc.) to ._iter() Removed redundant iteration through default values Almost all arguments checks moved out of loops, so checks happen once Fast yield from .__dict__ on plain .iter() (x10 boost) Removed redundant set(dict.keys()) in ._calculate_keys() * Moved back from nested generator checks to checks, optimized copy a bit * Bump pytest-mock from 1.12.0 to 1.12.1 (#1018) Bumps [pytest-mock](https://github.com/pytest-dev/pytest-mock) from 1.12.0 to 1.12.1. - [Release notes](https://github.com/pytest-dev/pytest-mock/releases) - [Changelog](https://github.com/pytest-dev/pytest-mock/blob/master/CHANGELOG.rst) - [Commits](pytest-dev/pytest-mock@v1.12.0...v1.12.1) Signed-off-by: dependabot-preview[bot] <support@dependabot.com> * __str__ and __repr__ inheritance for models, fix #1022 (#1023) * add testimonials section to docs with reference to python bytes podcast episode (#1025) * add testimonials section with reference to python bytes podcast episode * added description to changes directory * Bump twine from 3.0.0 to 3.1.0 (#1029) Bumps [twine](https://github.com/pypa/twine) from 3.0.0 to 3.1.0. - [Release notes](https://github.com/pypa/twine/releases) - [Changelog](https://github.com/pypa/twine/blob/master/docs/changelog.rst) - [Commits](pypa/twine@3.0.0...3.1.0) Signed-off-by: dependabot-preview[bot] <support@dependabot.com> * Support typing.Literal in python 3.8 (#1027) * Support typing.Literal in python 3.8 * Improve import pattern for Literal * Update references to in docs * Try to get build to pass * Add support for mapping types as custom root (#958) * Add support for mapping types as custom root * Incorporate feedback * Add changes * Incorporate feedback * Add docs and tests * Fix linting issue * Incorporate more feedback * Add more specific match * Add parse_as_type function (#934) * Add parse_as_type function * Add changes * Incorporate feedback * Add naming tests * Fix double quotes * Fix docs example * Reorder parameters; add dataclass and mapping tests * Rename parse_as_type to parse_obj, and add parse_file * Incorporate feedback * Incorporate feedback * use custom root types * Add better support for validator reuse (#941) * Add better support for validator reuse * Clean up classmethod unpacking * Add changes * Fix coverage check * Make 3.8 compatible * Update changes/940-dmontagu.md Co-Authored-By: Samuel Colvin <s@muelcolvin.com> * Make allow_reuse discoverable by adding to error message * switch _check_validator_name to _prepare_validator * Add changes file * Delete unrelated files * Add check that k in fields before using alias Co-Authored-By: Samuel Colvin <samcolvin@gmail.com> * Remove redundant call to __iter__ Co-Authored-By: Samuel Colvin <samcolvin@gmail.com> * Use typing.AbstractSet * Update pydantic/main.py Co-Authored-By: Samuel Colvin <samcolvin@gmail.com> Co-authored-by: dependabot-preview[bot] <27856297+dependabot-preview[bot]@users.noreply.github.com> Co-authored-by: Samuel Colvin <samcolvin@gmail.com> Co-authored-by: Colin Sullivan <csullivan@brandwatch.com> Co-authored-by: David Montague <35119617+dmontagu@users.noreply.github.com>
Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Change Summary
Modifies
parse_obj
andMetaModel
to allow mapping types with a custom root.I needed this to address some of the feedback on #934
Related issue number
Closes #908
Checklist
changes/<pull request or issue id>-<github username>.md
file added describing change(see changes/README.md for details)