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

Add Config.keep_untouched for custom descriptors support #679

Merged
merged 10 commits into from Jul 24, 2019

Conversation

@MrMrRobat
Copy link
Contributor

commented Jul 19, 2019

Change Summary

This will allow to extend default TYPE_BLACKLIST during class creation.

It's very helpful if we want use custom descriptors for our class (like cached_property, etc) and don't want pydantic to break them.

from pydantic import BaseModel

class _ClassPropertyDescriptor:
    __slots__ = ('getter', )

    def __init__(self, getter):
        self.getter = getter

    def __get__(self, instance, owner):
        return self.getter(owner)

classproperty = _ClassPropertyDescriptor

class Model(BaseModel):
    class Config:
        keep_untouched = (classproperty, )

    @classproperty
    def class_name(cls) -> str:
        return cls.__name__

print(Model.class_name)  # Model
print(Model().class_name)  # Model

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>
@codecov

This comment has been minimized.

Copy link

commented Jul 19, 2019

Codecov Report

❗️ No coverage uploaded for pull request base (master@b09e697). Click here to learn what that means.
The diff coverage is 100%.

@@           Coverage Diff           @@
##             master   #679   +/-   ##
=======================================
  Coverage          ?   100%           
=======================================
  Files             ?     15           
  Lines             ?   2719           
  Branches          ?    538           
=======================================
  Hits              ?   2719           
  Misses            ?      0           
  Partials          ?      0

MrMrRobat added some commits Jul 19, 2019

@samuelcolvin
Copy link
Owner

left a comment

otherwise LGTM.

@@ -214,10 +215,11 @@ def __new__(mcs, name, bases, namespace):
config=config,
)

type_blacklist = TYPE_BLACKLIST + config.keep_untouched

This comment has been minimized.

Copy link
@samuelcolvin

samuelcolvin Jul 24, 2019

Owner

please rename TYPE_BLACKLIST to UNTOUCHED_TYPES to be consistent with config.

This comment has been minimized.

Copy link
@MrMrRobat

MrMrRobat Jul 24, 2019

Author Contributor

Done :)

Show resolved Hide resolved pydantic/validators.py Outdated
Show resolved Hide resolved HISTORY.rst Outdated

MrMrRobat and others added some commits Jul 24, 2019

Update pydantic/validators.py
Co-Authored-By: Samuel Colvin <samcolvin@gmail.com>
Update HISTORY.rst
Co-Authored-By: Samuel Colvin <samcolvin@gmail.com>
Show resolved Hide resolved docs/index.rst Outdated

MrMrRobat and others added some commits Jul 24, 2019

Update docs/index.rst
Co-Authored-By: Samuel Colvin <samcolvin@gmail.com>

@samuelcolvin samuelcolvin merged commit fae3588 into samuelcolvin:master Jul 24, 2019

7 of 10 checks passed

Header rules No header rules processed
Details
Pages changed 2 new files uploaded
Details
Redirect rules No redirect rules processed
Details
Mixed content No mixed content detected
Details
codecov/project No report found to compare against
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details
deploy/netlify Deploy preview ready!
Details
samuelcolvin.pydantic Build #20190724.15 succeeded
Details
samuelcolvin.pydantic (Job Python36) Job Python36 succeeded
Details
samuelcolvin.pydantic (Job Python37) Job Python37 succeeded
Details

PrettyWood added a commit to ToucanToco/toucan-connectors that referenced this pull request Aug 4, 2019

Remove TemplateMixin
It was a hack to bypass the validation
But thanks to samuelcolvin/pydantic#679,
it is now possible to use custom descriptors like classproperty
without raising errors

PrettyWood added a commit to ToucanToco/toucan-connectors that referenced this pull request Aug 4, 2019

Remove TemplateMixin
It was a hack to bypass the validation
But thanks to samuelcolvin/pydantic#679,
it is now possible to use custom descriptors like classproperty
without raising errors

PrettyWood added a commit to ToucanToco/toucan-connectors that referenced this pull request Aug 4, 2019

Remove TemplateMixin
It was a hack to bypass the validation
But thanks to samuelcolvin/pydantic#679,
it is now possible to use custom descriptors like classproperty
without raising errors
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.