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

added feature post_init_post_parse #567

Merged
merged 20 commits into from Jun 4, 2019

Conversation

Projects
None yet
2 participants
@sevaho
Copy link
Contributor

commented May 31, 2019

Change Summary

Now you can have __post_init_post_parse__ that runs after pydantic has validated the input.

So __init__() -> __post_init__() -> pydantic object validation -> __post_init_post_parse__()

Related issue number

#566

Checklist

  • Unit tests for the changes exist
  • Tests pass on CI and coverage remains at 100%
  • Documentation reflects the changes where 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>
    • include your github username @<whomever>
@samuelcolvin

This comment has been minimized.

Copy link
Owner

commented May 31, 2019

Looking good, needs tests and docs.

@codecov

This comment has been minimized.

Copy link

commented Jun 1, 2019

Codecov Report

Merging #567 into master will decrease coverage by 0.08%.
The diff coverage is 66.66%.

@@            Coverage Diff             @@
##           master     #567      +/-   ##
==========================================
- Coverage     100%   99.91%   -0.09%     
==========================================
  Files          15       15              
  Lines        2491     2497       +6     
  Branches      497      499       +2     
==========================================
+ Hits         2491     2495       +4     
- Misses          0        1       +1     
- Partials        0        1       +1
@sevaho

This comment has been minimized.

Copy link
Contributor Author

commented Jun 1, 2019

I don't understand why tests fail tho, if I install it locally it works :/

    def test_post_init_post_parse():
        post_init_called = False
        post_init_post_parse_called = False
    
        @pydantic.dataclasses.dataclass
        class MyDataclass:
            a: int
    
            def __post_init__(self):
                nonlocal post_init_called
                post_init_called = True
    
            def __post_init_post_parse__(self):
                nonlocal post_init_post_parse_called
                post_init_post_parse_called = True
    
        d = MyDataclass("1")
        assert d.a == 1
        assert post_init_called
>       assert post_init_post_parse_called
E       assert False
@samuelcolvin

This comment has been minimized.

Copy link
Owner

commented Jun 1, 2019

please revert to single quotes, then I'll review.

@sevaho

This comment has been minimized.

Copy link
Contributor Author

commented Jun 1, 2019

Sorry, autoformatter on save ;)

Show resolved Hide resolved pydantic/dataclasses.py Outdated
@sevaho

This comment has been minimized.

Copy link
Contributor Author

commented Jun 1, 2019

Yeah you were right hasattr is not the way to go ;)

Also added a test which shows the difference between post_init and post_init_post_parse:

def test_post_init_post_parse_types():
    @pydantic.dataclasses.dataclass
    class CustomType(object):
        b: int

    @pydantic.dataclasses.dataclass
    class MyDataclass:
        a: CustomType

        def __post_init__(self):
            assert type(self.a) == dict

        def __post_init_post_parse__(self):
            assert type(self.a) == CustomType

    d = MyDataclass(**{"a": {"b": 1}})
    assert d.a.b == 1

Will add some docs now

@samuelcolvin
Copy link
Owner

left a comment

Looking good, please update history and docs.

Show resolved Hide resolved tests/test_dataclasses.py
a.py Outdated
@@ -0,0 +1,20 @@
import pydantic

This comment has been minimized.

Copy link
@samuelcolvin

samuelcolvin Jun 1, 2019

Owner

remove this file please.

sevaho added some commits Jun 1, 2019

@sevaho

This comment has been minimized.

Copy link
Contributor Author

commented Jun 1, 2019

Added docs and update history.

I think this should be in a new version because it is a new feature? If not I update the version back to 0.27 and it my update to history to 0.27 aswell.

@samuelcolvin
Copy link
Owner

left a comment

just a few small things, otherwise looking good.

Show resolved Hide resolved HISTORY.rst Outdated
Show resolved Hide resolved docs/index.rst Outdated
Show resolved Hide resolved HISTORY.rst Outdated
Show resolved Hide resolved pydantic/dataclasses.py Outdated
@codecov

This comment has been minimized.

Copy link

commented Jun 1, 2019

Codecov Report

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

@@          Coverage Diff          @@
##           master   #567   +/-   ##
=====================================
  Coverage     100%   100%           
=====================================
  Files          15     15           
  Lines        2491   2495    +4     
  Branches      497    498    +1     
=====================================
+ Hits         2491   2495    +4

sevaho and others added some commits Jun 1, 2019

Update HISTORY.rst
Co-Authored-By: Samuel Colvin <samcolvin@gmail.com>
Update HISTORY.rst
Co-Authored-By: Samuel Colvin <samcolvin@gmail.com>
Update pydantic/dataclasses.py
Co-Authored-By: Samuel Colvin <samcolvin@gmail.com>
@samuelcolvin
Copy link
Owner

left a comment

Best to run tests before you commit to avoid simple problems like this.

HISTORY.rst Outdated
@@ -3,9 +3,13 @@
History
-------

v0.28 (unreleased)
..................
* support ``__post_init_post_parse__`` support to dataclasses, #567 by @sevaho

This comment has been minimized.

Copy link
@samuelcolvin
if post_init_original and post_init_original.__name__ == '_pydantic_post_init':
post_init_original = None
if post_init_post_parse:

This comment has been minimized.

Copy link
@samuelcolvin

samuelcolvin Jun 2, 2019

Owner

this is wrong, it needs to be removed completely, I guess this is why tests are failing.

sevaho added some commits Jun 2, 2019

@sevaho

This comment has been minimized.

Copy link
Contributor Author

commented Jun 2, 2019

Idd it had no use

samuelcolvin and others added some commits Jun 3, 2019

@samuelcolvin samuelcolvin merged commit 2a04aa7 into samuelcolvin:master Jun 4, 2019

5 of 8 checks passed

Header rules No header rules processed
Details
Pages changed All files already uploaded
Details
Redirect rules No redirect rules processed
Details
Mixed content No mixed content detected
Details
codecov/project 100% (+0%) compared to d69ca21
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details
deploy/netlify Deploy preview ready!
Details
samuelcolvin.pydantic Build #20190603.3 succeeded
Details
@samuelcolvin

This comment has been minimized.

Copy link
Owner

commented Jun 4, 2019

great, thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.