Skip to content

Commit dba3c5a

Browse files
committed
Black'en code base.
1 parent bc23f8a commit dba3c5a

16 files changed

+555
-394
lines changed

polymodels/__init__.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
VERSION = (1, 8, 0, 'final', 0)
1+
VERSION = (1, 8, 0, "final", 0)
22

3-
__version__ = '1.8.0'
3+
__version__ = "1.8.0"

polymodels/fields.py

+54-38
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
from django.db.models import ForeignKey, Q
55
from django.db.models.fields import NOT_PROVIDED
66
from django.db.models.fields.related import (
7-
RelatedField, lazy_related_operation,
7+
RelatedField,
8+
lazy_related_operation,
89
)
910
from django.utils.deconstruct import deconstructible
1011
from django.utils.functional import LazyObject, empty
@@ -21,15 +22,15 @@ def __init__(self, field, limit_choices_to):
2122

2223
@property
2324
def value(self):
24-
subclasses_lookup = self.field.polymorphic_type.subclasses_lookup('pk')
25+
subclasses_lookup = self.field.polymorphic_type.subclasses_lookup("pk")
2526
limit_choices_to = self.limit_choices_to
2627
if limit_choices_to is None:
2728
limit_choices_to = subclasses_lookup.copy()
2829
elif isinstance(limit_choices_to, dict):
2930
limit_choices_to = dict(limit_choices_to, **subclasses_lookup)
3031
elif isinstance(limit_choices_to, Q):
3132
limit_choices_to = limit_choices_to & Q(**subclasses_lookup)
32-
self.__dict__['value'] = limit_choices_to
33+
self.__dict__["value"] = limit_choices_to
3334
return limit_choices_to
3435

3536
def __call__(self):
@@ -42,8 +43,8 @@ def __init__(self, remote_field, db):
4243
self.__dict__.update(remote_field=remote_field, db=db)
4344

4445
def _setup(self):
45-
remote_field = self.__dict__.get('remote_field')
46-
db = self.__dict__.get('db')
46+
remote_field = self.__dict__.get("remote_field")
47+
db = self.__dict__.get("db")
4748
self._wrapped = remote_field.model._default_manager.using(db).complex_filter(
4849
remote_field.limit_choices_to()
4950
)
@@ -53,7 +54,7 @@ def __getattr__(self, attr):
5354
# Django 2.1+ in order to clear possible cached results.
5455
# Since no results might have been cached before _setup() is called
5556
# it's safe to keep deferring until something else is accessed.
56-
if attr == 'all' and self._wrapped is empty:
57+
if attr == "all" and self._wrapped is empty:
5758
return lambda: self
5859
return super().__getattr__(attr)
5960

@@ -79,32 +80,35 @@ def __repr__(self):
7980

8081
class PolymorphicTypeField(ForeignKey):
8182
default_error_messages = {
82-
'invalid': _('Specified model is not a subclass of %(model)s.')
83+
"invalid": _("Specified model is not a subclass of %(model)s.")
8384
}
84-
description = _(
85-
'Content type of a subclass of %(type)s'
86-
)
85+
description = _("Content type of a subclass of %(type)s")
8786
default_kwargs = {
88-
'to': 'contenttypes.contenttype',
89-
'related_name': '+',
87+
"to": "contenttypes.contenttype",
88+
"related_name": "+",
9089
}
9190

9291
def __init__(self, polymorphic_type, *args, **kwargs):
9392
self.polymorphic_type = polymorphic_type
9493
self.overriden_default = False
9594
for kwarg, value in self.default_kwargs.items():
9695
kwargs.setdefault(kwarg, value)
97-
kwargs['limit_choices_to'] = LimitChoicesToSubclasses(self, kwargs.pop('limit_choices_to', None))
96+
kwargs["limit_choices_to"] = LimitChoicesToSubclasses(
97+
self, kwargs.pop("limit_choices_to", None)
98+
)
9899
super().__init__(*args, **kwargs)
99100

100101
def contribute_to_class(self, cls, name):
101102
super().contribute_to_class(cls, name)
102103
polymorphic_type = self.polymorphic_type
103-
if (isinstance(polymorphic_type, str) or
104-
polymorphic_type._meta.pk is None):
104+
if isinstance(polymorphic_type, str) or polymorphic_type._meta.pk is None:
105+
105106
def resolve_polymorphic_type(model, related_model, field):
106107
field.do_polymorphic_type(related_model)
107-
lazy_related_operation(resolve_polymorphic_type, cls, polymorphic_type, field=self)
108+
109+
lazy_related_operation(
110+
resolve_polymorphic_type, cls, polymorphic_type, field=self
111+
)
108112
else:
109113
self.do_polymorphic_type(polymorphic_type)
110114

@@ -115,49 +119,61 @@ def do_polymorphic_type(self, polymorphic_type):
115119
self.overriden_default = True
116120
self.polymorphic_type = polymorphic_type
117121
self.type = polymorphic_type.__name__
118-
self.error_messages['invalid'] = (
119-
'Specified content type is not of a subclass of %s.' % polymorphic_type._meta.object_name
122+
self.error_messages["invalid"] = (
123+
"Specified content type is not of a subclass of %s."
124+
% polymorphic_type._meta.object_name
120125
)
121126

122127
def check(self, **kwargs):
123128
errors = super().check(**kwargs)
124129
if isinstance(self.polymorphic_type, str):
125-
errors.append(checks.Error(
126-
("Field defines a relation with model '%s', which "
127-
"is either not installed, or is abstract.") % self.polymorphic_type,
128-
id='fields.E300',
129-
))
130+
errors.append(
131+
checks.Error(
132+
(
133+
"Field defines a relation with model '%s', which "
134+
"is either not installed, or is abstract."
135+
)
136+
% self.polymorphic_type,
137+
id="fields.E300",
138+
)
139+
)
130140
elif not issubclass(self.polymorphic_type, BasePolymorphicModel):
131-
errors.append(checks.Error(
132-
"The %s type is not a subclass of BasePolymorphicModel." % self.polymorphic_type.__name__,
133-
id='polymodels.E004',
134-
))
141+
errors.append(
142+
checks.Error(
143+
"The %s type is not a subclass of BasePolymorphicModel."
144+
% self.polymorphic_type.__name__,
145+
id="polymodels.E004",
146+
)
147+
)
135148
return errors
136149

137150
def formfield(self, **kwargs):
138-
db = kwargs.pop('using', None)
151+
db = kwargs.pop("using", None)
139152
if isinstance(self.polymorphic_type, str):
140153
raise ValueError(
141-
"Cannot create form field for %r yet, because its related model %r has not been loaded yet" % (
142-
self.name, self.polymorphic_type
143-
)
154+
"Cannot create form field for %r yet, because its related model %r has not been loaded yet"
155+
% (self.name, self.polymorphic_type)
144156
)
145157
defaults = {
146-
'form_class': forms.ModelChoiceField,
147-
'queryset': LazyPolymorphicTypeQueryset(self.remote_field, db),
148-
'to_field_name': self.remote_field.field_name,
158+
"form_class": forms.ModelChoiceField,
159+
"queryset": LazyPolymorphicTypeQueryset(self.remote_field, db),
160+
"to_field_name": self.remote_field.field_name,
149161
}
150162
defaults.update(kwargs)
151163
return super(RelatedField, self).formfield(**defaults)
152164

153165
def deconstruct(self):
154166
name, path, args, kwargs = super().deconstruct()
155-
opts = getattr(self.polymorphic_type, '_meta', None)
156-
kwargs['polymorphic_type'] = "%s.%s" % (opts.app_label, opts.object_name) if opts else self.polymorphic_type
167+
opts = getattr(self.polymorphic_type, "_meta", None)
168+
kwargs["polymorphic_type"] = (
169+
"%s.%s" % (opts.app_label, opts.object_name)
170+
if opts
171+
else self.polymorphic_type
172+
)
157173
for kwarg, value in list(kwargs.items()):
158174
if self.default_kwargs.get(kwarg) == value:
159175
kwargs.pop(kwarg)
160176
if self.overriden_default:
161-
kwargs.pop('default')
162-
kwargs.pop('limit_choices_to', None)
177+
kwargs.pop("default")
178+
kwargs.pop("limit_choices_to", None)
163179
return name, path, args, kwargs

polymodels/forms.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def __getitem__(self, model):
2222

2323
class PolymorphicModelForm(models.ModelForm, metaclass=PolymorphicModelFormMetaclass):
2424
def __new__(cls, *args, **kwargs):
25-
instance = kwargs.get('instance', None)
25+
instance = kwargs.get("instance", None)
2626
if instance:
2727
cls = cls[instance.__class__]
2828
return super().__new__(cls)

polymodels/managers.py

+15-12
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
from django.db import models
66
from django.db.models.query import ModelIterable
77

8-
type_cast_iterator = partial(map, methodcaller('type_cast'))
9-
type_cast_prefetch_iterator = partial(map, methodcaller('type_cast', with_prefetched_objects=True))
8+
type_cast_iterator = partial(map, methodcaller("type_cast"))
9+
type_cast_prefetch_iterator = partial(
10+
map, methodcaller("type_cast", with_prefetched_objects=True)
11+
)
1012

1113

1214
class PolymorphicModelIterable(ModelIterable):
@@ -31,19 +33,15 @@ def select_subclasses(self, *models):
3133
subclasses = set()
3234
for model in models:
3335
if not issubclass(model, self.model):
34-
raise TypeError(
35-
"%r is not a subclass of %r" % (model, self.model)
36-
)
36+
raise TypeError("%r is not a subclass of %r" % (model, self.model))
3737
subclasses.update(model.subclass_accessors)
3838
# Collect all `select_related` required lookups
3939
for subclass in subclasses:
4040
# Avoid collecting ourself and proxy subclasses
4141
related_lookup = accessors[subclass].related_lookup
4242
if related_lookup:
4343
related_lookups.add(related_lookup)
44-
queryset = self.filter(
45-
**self.model.content_type_lookup(*tuple(subclasses))
46-
)
44+
queryset = self.filter(**self.model.content_type_lookup(*tuple(subclasses)))
4745
else:
4846
# Collect all `select_related` required relateds
4947
for accessor in accessors.values():
@@ -63,7 +61,9 @@ def _fetch_all(self):
6361
# Override _fetch_all in order to disable PolymorphicModelIterable's
6462
# type casting when prefetch_related is used because the latter might
6563
# crash or disfunction when dealing with a mixed set of objects.
66-
prefetch_related_objects = self._prefetch_related_lookups and not self._prefetch_done
64+
prefetch_related_objects = (
65+
self._prefetch_related_lookups and not self._prefetch_done
66+
)
6767
type_cast = False
6868
if self._result_cache is None:
6969
iterable_class = self._iterable_class
@@ -74,17 +74,20 @@ def _fetch_all(self):
7474
if prefetch_related_objects:
7575
self._prefetch_related_objects()
7676
if type_cast:
77-
self._result_cache = list(type_cast_prefetch_iterator(self._result_cache))
77+
self._result_cache = list(
78+
type_cast_prefetch_iterator(self._result_cache)
79+
)
7880

7981

8082
class PolymorphicManager(models.Manager.from_queryset(PolymorphicQuerySet)):
8183
def contribute_to_class(self, model, name):
8284
# Avoid circular reference
8385
from .models import BasePolymorphicModel
86+
8487
if not issubclass(model, BasePolymorphicModel):
8588
raise ImproperlyConfigured(
86-
'`%s` can only be used on '
87-
'`BasePolymorphicModel` subclasses.' % self.__class__.__name__
89+
"`%s` can only be used on "
90+
"`BasePolymorphicModel` subclasses." % self.__class__.__name__
8891
)
8992
return super().contribute_to_class(model, name)
9093

0 commit comments

Comments
 (0)