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

Merged
merged 10 commits into from Jul 24, 2019

Conversation

Bobronium
Copy link
Contributor

@Bobronium Bobronium 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
Copy link

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

Copy link
Collaborator

@samuelcolvin samuelcolvin left a comment

otherwise LGTM.

pydantic/main.py Outdated
@@ -214,10 +215,11 @@ def __new__(mcs, name, bases, namespace):
config=config,
)

type_blacklist = TYPE_BLACKLIST + config.keep_untouched
Copy link
Collaborator

@samuelcolvin samuelcolvin Jul 24, 2019

Choose a reason for hiding this comment

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

please rename TYPE_BLACKLIST to UNTOUCHED_TYPES to be consistent with config.

Copy link
Contributor Author

@Bobronium Bobronium Jul 24, 2019

Choose a reason for hiding this comment

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

Done :)

pydantic/validators.py Outdated Show resolved Hide resolved
HISTORY.rst Outdated Show resolved Hide resolved
Bobronium and others added 3 commits Jul 24, 2019
Co-Authored-By: Samuel Colvin <samcolvin@gmail.com>
Co-Authored-By: Samuel Colvin <samcolvin@gmail.com>
docs/index.rst Outdated Show resolved Hide resolved
Bobronium and others added 2 commits Jul 24, 2019
@samuelcolvin samuelcolvin merged commit fae3588 into pydantic:master Jul 24, 2019
10 checks passed
PrettyWood added a commit to ToucanToco/toucan-connectors that referenced this issue Aug 4, 2019
It was a hack to bypass the validation
But thanks to pydantic/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 issue Aug 4, 2019
It was a hack to bypass the validation
But thanks to pydantic/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 issue Aug 4, 2019
It was a hack to bypass the validation
But thanks to pydantic/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
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants