Skip to content

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

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
Member

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

Choose a reason for hiding this comment

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

Done :)

Bobronium and others added 3 commits July 24, 2019 12:52
Co-Authored-By: Samuel Colvin <samcolvin@gmail.com>
Co-Authored-By: Samuel Colvin <samcolvin@gmail.com>
@samuelcolvin samuelcolvin merged commit fae3588 into pydantic:master Jul 24, 2019
PrettyWood added a commit to ToucanToco/toucan-connectors that referenced this pull request 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 pull request 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 pull request 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
alexdrydew pushed a commit to alexdrydew/pydantic that referenced this pull request Dec 23, 2023
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.

2 participants