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 parse_as_type function #934
Conversation
Codecov Report
@@ Coverage Diff @@
## master #934 +/- ##
=====================================
Coverage 100% 100%
=====================================
Files 19 20 +1
Lines 3293 3317 +24
Branches 651 653 +2
=====================================
+ Hits 3293 3317 +24
Continue to review full report at Codecov.
|
This doesn't seem to work with dataclasses, is it supposed to? |
It should work if and only if you could put dataclasses as fields on a pydantic model. I don't know whether @samuelcolvin would want to support that or not. |
pydantic/tools.py
Outdated
from pydantic.main import create_model | ||
|
||
type_name = getattr(type_, '__name__', str(type_)) | ||
return create_model(f'ParsingModel[{type_name}] (for {source})', obj=(type_, ...)) |
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 not sure about obj
I think it could be confusing, I think we should use custom root types, e.g. __root__
.
That might involve fixing #908 but I think that's a good idea anyway.
Is this a partial solution for #481 and what is missing? |
This is orthogonal to the points raised in that issue, but I think it would be nice if it could support pydantic dataclasses too. I'm not sure how I'd make that happen though. I think if you added the functionality discussed in that issue, it would probably make it easier to have the Edit: Actually, it's not quite orthogonal, but it goes beyond what is discussed in that issue -- it supports parsing any type parseable by pydantic, not just models / dataclasses. |
i like the tests, especially edit: clarified input to mean types |
Yeah, I ordered arguments based on what seemed intuitive to me from the function name |
Just waiting on #958 to replace |
@ahirner Actually, to my surprise, it looks like this works out of the box with dataclasses. (I added a test.) This will also handle the I might hold off on implementing the |
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 looks good.
50b7fd0
to
76a41ae
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.
I'm still not sure about
I guess I'd prefer |
I'm fine with that; it seemed similar to me since it is basically like the unbound version of the I'd be in favor of |
Sorry to be indecisive. Let's go with |
@samuelcolvin I did the renames and added some docs; assuming you have no issues with the docs, I think this is just waiting now on changing (To be clear, I'm fine with this whether we use |
* 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 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
* 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>
Change Summary
Adds just the
parse_as_type
function from #812, along with some documentation.The documentation may need some work; @samuelcolvin if you give me some direction on what you'd like I'd be happy to modify it. I also think it may make sense to flesh it out a little more
and/or place it on a separate "tools" page once the
dump_as_type
functionality is implemented.Related issue number
Addresses minor parts of #811, but to close that issue the
dump_as_type
functionality will be more important.Given the lengthy back and forth on #812 I figured it might make more sense to just start fresh and break the pull request into smaller chunks.
Checklist
changes/<pull request or issue id>-<github username>.md
file added describing change(see changes/README.md for details)