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

sevaho
Copy link
Contributor

@sevaho sevaho 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
Copy link
Member

Looking good, needs tests and docs.

@codecov
Copy link

codecov bot 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
Copy link
Contributor Author

sevaho 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
Copy link
Member

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

@sevaho
Copy link
Contributor Author

sevaho commented Jun 1, 2019

Sorry, autoformatter on save ;)

@sevaho
Copy link
Contributor Author

sevaho 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

Copy link
Member

@samuelcolvin samuelcolvin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking good, please update history and docs.

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove this file please.

@sevaho
Copy link
Contributor Author

sevaho 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.

Copy link
Member

@samuelcolvin samuelcolvin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just a few small things, otherwise looking good.

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 Show resolved Hide resolved
@codecov
Copy link

codecov bot 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 5 commits June 1, 2019 16:45
Co-Authored-By: Samuel Colvin <samcolvin@gmail.com>
Co-Authored-By: Samuel Colvin <samcolvin@gmail.com>
Co-Authored-By: Samuel Colvin <samcolvin@gmail.com>
Copy link
Member

@samuelcolvin samuelcolvin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@sevaho
Copy link
Contributor Author

sevaho commented Jun 2, 2019

Idd it had no use

@samuelcolvin samuelcolvin merged commit 2a04aa7 into pydantic:master Jun 4, 2019
@samuelcolvin
Copy link
Member

great, thanks.

alexdrydew pushed a commit to alexdrydew/pydantic that referenced this pull request Dec 23, 2023
* fix: copy with tzinfo

* fix: apply feedback
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants