Remove Django-isms for embedded exceptions classes

This commit is contained in:
Andrew Brookins 2021-09-29 20:42:02 -07:00
parent b80addbb13
commit 54f00b5774
2 changed files with 4 additions and 39 deletions

View file

@ -29,7 +29,7 @@ import uuid
import redis import redis
from pydantic import BaseModel, validator from pydantic import BaseModel, validator
from pydantic.fields import FieldInfo as PydanticFieldInfo, PrivateAttr, Field from pydantic.fields import FieldInfo as PydanticFieldInfo
from pydantic.fields import ModelField, Undefined, UndefinedType from pydantic.fields import ModelField, Undefined, UndefinedType
from pydantic.main import ModelMetaclass from pydantic.main import ModelMetaclass
from pydantic.typing import NoArgAnyCallable from pydantic.typing import NoArgAnyCallable
@ -42,25 +42,6 @@ model_registry = {}
_T = TypeVar("_T") _T = TypeVar("_T")
def subclass_exception(name, bases, module, attached_to):
"""
Create exception subclass. Used by RedisModel below.
The exception is created in a way that allows it to be pickled, assuming
that the returned exception class will be added as an attribute to the
'attached_to' class.
"""
return type(name, bases, {
'__module__': module,
'__qualname__': '%s.%s' % (attached_to.__qualname__, name),
})
def _has_contribute_to_class(value):
# Only call contribute_to_class() if it's bound.
return not inspect.isclass(value) and hasattr(value, 'contribute_to_class')
class TokenEscaper: class TokenEscaper:
""" """
Escape punctuation within an input string. Escape punctuation within an input string.
@ -646,7 +627,6 @@ class ModelMeta(ModelMetaclass):
meta = meta or getattr(new_class, 'Meta', None) meta = meta or getattr(new_class, 'Meta', None)
base_meta = getattr(new_class, '_meta', None) base_meta = getattr(new_class, '_meta', None)
parents = [b for b in bases if isinstance(b, ModelMeta)]
if meta and meta != DefaultMeta and meta != base_meta: if meta and meta != DefaultMeta and meta != base_meta:
new_class.Meta = meta new_class.Meta = meta
@ -662,21 +642,6 @@ class ModelMeta(ModelMetaclass):
new_class._meta = deepcopy(DefaultMeta) new_class._meta = deepcopy(DefaultMeta)
new_class.Meta = new_class._meta new_class.Meta = new_class._meta
# Not an abstract model class
if abc.ABC not in bases:
key = f"{new_class.__module__}.{new_class.__name__}"
model_registry[key] = new_class
new_class.add_to_class(
'NotFoundError',
subclass_exception(
'NotFoundError',
tuple(
x.NotFoundError for x in bases if hasattr(x, '_meta') and not issubclass(x, abc.ABC)
) or (NotFoundError,),
attrs['__module__'],
attached_to=new_class))
# Create proxies for each model field so that we can use the field # Create proxies for each model field so that we can use the field
# in queries, like Model.get(Model.field_name == 1) # in queries, like Model.get(Model.field_name == 1)
for field_name, field in new_class.__fields__.items(): for field_name, field in new_class.__fields__.items():
@ -857,7 +822,7 @@ class HashModel(RedisModel, abc.ABC):
def get(cls, pk: Any) -> 'HashModel': def get(cls, pk: Any) -> 'HashModel':
document = cls.db().hgetall(cls.make_primary_key(pk)) document = cls.db().hgetall(cls.make_primary_key(pk))
if not document: if not document:
raise cls.NotFoundError raise NotFoundError
return cls.parse_obj(document) return cls.parse_obj(document)
@classmethod @classmethod

View file

@ -12,7 +12,7 @@ from redis_developer.orm import (
HashModel, HashModel,
Field, Field,
) )
from redis_developer.orm.model import RedisModelError, QueryNotSupportedError from redis_developer.orm.model import RedisModelError, QueryNotSupportedError, NotFoundError
r = redis.Redis() r = redis.Redis()
today = datetime.date.today() today = datetime.date.today()
@ -311,7 +311,7 @@ def test_sorting(members):
def test_not_found(): def test_not_found():
with pytest.raises(Member.NotFoundError): with pytest.raises(NotFoundError):
# This ID does not exist. # This ID does not exist.
Member.get(1000) Member.get(1000)