flake8 tweaks for mongoengine.base
This commit is contained in:
parent
e2097061e9
commit
f2b8dcf292
@ -1,5 +1,5 @@
|
|||||||
import weakref
|
|
||||||
import itertools
|
import itertools
|
||||||
|
import weakref
|
||||||
|
|
||||||
from mongoengine.common import _import_class
|
from mongoengine.common import _import_class
|
||||||
from mongoengine.errors import DoesNotExist, MultipleObjectsReturned
|
from mongoengine.errors import DoesNotExist, MultipleObjectsReturned
|
||||||
@ -199,8 +199,9 @@ class BaseList(list):
|
|||||||
def _mark_as_changed(self, key=None):
|
def _mark_as_changed(self, key=None):
|
||||||
if hasattr(self._instance, '_mark_as_changed'):
|
if hasattr(self._instance, '_mark_as_changed'):
|
||||||
if key:
|
if key:
|
||||||
self._instance._mark_as_changed('%s.%s' % (self._name,
|
self._instance._mark_as_changed(
|
||||||
key % len(self)))
|
'%s.%s' % (self._name, key % len(self))
|
||||||
|
)
|
||||||
else:
|
else:
|
||||||
self._instance._mark_as_changed(self._name)
|
self._instance._mark_as_changed(self._name)
|
||||||
|
|
||||||
|
@ -1,28 +1,28 @@
|
|||||||
import copy
|
import copy
|
||||||
import operator
|
|
||||||
import numbers
|
import numbers
|
||||||
|
import operator
|
||||||
from collections import Hashable
|
from collections import Hashable
|
||||||
from functools import partial
|
from functools import partial
|
||||||
|
|
||||||
import pymongo
|
from bson import ObjectId, json_util
|
||||||
from bson import json_util, ObjectId
|
|
||||||
from bson.dbref import DBRef
|
from bson.dbref import DBRef
|
||||||
from bson.son import SON
|
from bson.son import SON
|
||||||
|
import pymongo
|
||||||
|
|
||||||
from mongoengine import signals
|
from mongoengine import signals
|
||||||
from mongoengine.common import _import_class
|
from mongoengine.base.common import ALLOW_INHERITANCE, get_document
|
||||||
from mongoengine.errors import (ValidationError, InvalidDocumentError,
|
|
||||||
LookUpError, FieldDoesNotExist)
|
|
||||||
from mongoengine.python_support import PY3, txt_type
|
|
||||||
from mongoengine.base.common import get_document, ALLOW_INHERITANCE
|
|
||||||
from mongoengine.base.datastructures import (
|
from mongoengine.base.datastructures import (
|
||||||
BaseDict,
|
BaseDict,
|
||||||
BaseList,
|
BaseList,
|
||||||
EmbeddedDocumentList,
|
EmbeddedDocumentList,
|
||||||
StrictDict,
|
SemiStrictDict,
|
||||||
SemiStrictDict
|
StrictDict
|
||||||
)
|
)
|
||||||
from mongoengine.base.fields import ComplexBaseField
|
from mongoengine.base.fields import ComplexBaseField
|
||||||
|
from mongoengine.common import _import_class
|
||||||
|
from mongoengine.errors import (FieldDoesNotExist, InvalidDocumentError,
|
||||||
|
LookUpError, ValidationError)
|
||||||
|
from mongoengine.python_support import PY3, txt_type
|
||||||
|
|
||||||
__all__ = ('BaseDocument', 'NON_FIELD_ERRORS')
|
__all__ = ('BaseDocument', 'NON_FIELD_ERRORS')
|
||||||
|
|
||||||
@ -73,7 +73,7 @@ class BaseDocument(object):
|
|||||||
# if so raise an Exception.
|
# if so raise an Exception.
|
||||||
if not self._dynamic and (self._meta.get('strict', True) or _created):
|
if not self._dynamic and (self._meta.get('strict', True) or _created):
|
||||||
_undefined_fields = set(values.keys()) - set(
|
_undefined_fields = set(values.keys()) - set(
|
||||||
self._fields.keys() + ['id', 'pk', '_cls', '_text_score'])
|
self._fields.keys() + ['id', 'pk', '_cls', '_text_score'])
|
||||||
if _undefined_fields:
|
if _undefined_fields:
|
||||||
msg = (
|
msg = (
|
||||||
"The fields '{0}' do not exist on the document '{1}'"
|
"The fields '{0}' do not exist on the document '{1}'"
|
||||||
@ -310,7 +310,7 @@ class BaseDocument(object):
|
|||||||
data = SON()
|
data = SON()
|
||||||
data["_id"] = None
|
data["_id"] = None
|
||||||
data['_cls'] = self._class_name
|
data['_cls'] = self._class_name
|
||||||
|
|
||||||
# only root fields ['test1.a', 'test2'] => ['test1', 'test2']
|
# only root fields ['test1.a', 'test2'] => ['test1', 'test2']
|
||||||
root_fields = set([f.split('.')[0] for f in fields])
|
root_fields = set([f.split('.')[0] for f in fields])
|
||||||
|
|
||||||
@ -333,11 +333,11 @@ class BaseDocument(object):
|
|||||||
i.replace(key, '') for i in fields
|
i.replace(key, '') for i in fields
|
||||||
if i.startswith(key)]
|
if i.startswith(key)]
|
||||||
|
|
||||||
ex_vars['fields'] = embedded_fields
|
ex_vars['fields'] = embedded_fields
|
||||||
|
|
||||||
if 'use_db_field' in f_inputs:
|
if 'use_db_field' in f_inputs:
|
||||||
ex_vars['use_db_field'] = use_db_field
|
ex_vars['use_db_field'] = use_db_field
|
||||||
|
|
||||||
value = field.to_mongo(value, **ex_vars)
|
value = field.to_mongo(value, **ex_vars)
|
||||||
|
|
||||||
# Handle self generating fields
|
# Handle self generating fields
|
||||||
@ -566,8 +566,10 @@ class BaseDocument(object):
|
|||||||
continue
|
continue
|
||||||
if isinstance(field, ReferenceField):
|
if isinstance(field, ReferenceField):
|
||||||
continue
|
continue
|
||||||
elif (isinstance(data, (EmbeddedDocument, DynamicEmbeddedDocument))
|
elif (
|
||||||
and db_field_name not in changed_fields):
|
isinstance(data, (EmbeddedDocument, DynamicEmbeddedDocument)) and
|
||||||
|
db_field_name not in changed_fields
|
||||||
|
):
|
||||||
# Find all embedded fields that have been changed
|
# Find all embedded fields that have been changed
|
||||||
changed = data._get_changed_fields(inspected)
|
changed = data._get_changed_fields(inspected)
|
||||||
changed_fields += ["%s%s" % (key, k) for k in changed if k]
|
changed_fields += ["%s%s" % (key, k) for k in changed if k]
|
||||||
@ -608,7 +610,7 @@ class BaseDocument(object):
|
|||||||
break
|
break
|
||||||
elif isinstance(d, list) and p.lstrip('-').isdigit():
|
elif isinstance(d, list) and p.lstrip('-').isdigit():
|
||||||
if p[0] == '-':
|
if p[0] == '-':
|
||||||
p = str(len(d)+int(p))
|
p = str(len(d) + int(p))
|
||||||
try:
|
try:
|
||||||
d = d[int(p)]
|
d = d[int(p)]
|
||||||
except IndexError:
|
except IndexError:
|
||||||
@ -644,7 +646,7 @@ class BaseDocument(object):
|
|||||||
for p in parts:
|
for p in parts:
|
||||||
if isinstance(d, list) and p.lstrip('-').isdigit():
|
if isinstance(d, list) and p.lstrip('-').isdigit():
|
||||||
if p[0] == '-':
|
if p[0] == '-':
|
||||||
p = str(len(d)+int(p))
|
p = str(len(d) + int(p))
|
||||||
d = d[int(p)]
|
d = d[int(p)]
|
||||||
elif (hasattr(d, '__getattribute__') and
|
elif (hasattr(d, '__getattribute__') and
|
||||||
not isinstance(d, dict)):
|
not isinstance(d, dict)):
|
||||||
@ -775,8 +777,12 @@ class BaseDocument(object):
|
|||||||
# Check to see if we need to include _cls
|
# Check to see if we need to include _cls
|
||||||
allow_inheritance = cls._meta.get('allow_inheritance',
|
allow_inheritance = cls._meta.get('allow_inheritance',
|
||||||
ALLOW_INHERITANCE)
|
ALLOW_INHERITANCE)
|
||||||
include_cls = (allow_inheritance and not spec.get('sparse', False) and
|
include_cls = (
|
||||||
spec.get('cls', True) and '_cls' not in spec['fields'])
|
allow_inheritance and
|
||||||
|
not spec.get('sparse', False) and
|
||||||
|
spec.get('cls', True) and
|
||||||
|
'_cls' not in spec['fields']
|
||||||
|
)
|
||||||
|
|
||||||
# 733: don't include cls if index_cls is False unless there is an explicit cls with the index
|
# 733: don't include cls if index_cls is False unless there is an explicit cls with the index
|
||||||
include_cls = include_cls and (spec.get('cls', False) or cls._meta.get('index_cls', True))
|
include_cls = include_cls and (spec.get('cls', False) or cls._meta.get('index_cls', True))
|
||||||
|
@ -5,12 +5,12 @@ import weakref
|
|||||||
from bson import DBRef, ObjectId, SON
|
from bson import DBRef, ObjectId, SON
|
||||||
import pymongo
|
import pymongo
|
||||||
|
|
||||||
from mongoengine.common import _import_class
|
|
||||||
from mongoengine.errors import ValidationError
|
|
||||||
from mongoengine.base.common import ALLOW_INHERITANCE
|
from mongoengine.base.common import ALLOW_INHERITANCE
|
||||||
from mongoengine.base.datastructures import (
|
from mongoengine.base.datastructures import (
|
||||||
BaseDict, BaseList, EmbeddedDocumentList
|
BaseDict, BaseList, EmbeddedDocumentList
|
||||||
)
|
)
|
||||||
|
from mongoengine.common import _import_class
|
||||||
|
from mongoengine.errors import ValidationError
|
||||||
|
|
||||||
__all__ = ("BaseField", "ComplexBaseField",
|
__all__ = ("BaseField", "ComplexBaseField",
|
||||||
"ObjectIdField", "GeoJsonBaseField")
|
"ObjectIdField", "GeoJsonBaseField")
|
||||||
@ -85,13 +85,13 @@ class BaseField(object):
|
|||||||
self.null = null
|
self.null = null
|
||||||
self.sparse = sparse
|
self.sparse = sparse
|
||||||
self._owner_document = None
|
self._owner_document = None
|
||||||
|
|
||||||
# Detect and report conflicts between metadata and base properties.
|
# Detect and report conflicts between metadata and base properties.
|
||||||
conflicts = set(dir(self)) & set(kwargs)
|
conflicts = set(dir(self)) & set(kwargs)
|
||||||
if conflicts:
|
if conflicts:
|
||||||
raise TypeError("%s already has attribute(s): %s" % (
|
raise TypeError("%s already has attribute(s): %s" % (
|
||||||
self.__class__.__name__, ', '.join(conflicts) ))
|
self.__class__.__name__, ', '.join(conflicts)))
|
||||||
|
|
||||||
# Assign metadata to the instance
|
# Assign metadata to the instance
|
||||||
# This efficient method is available because no __slots__ are defined.
|
# This efficient method is available because no __slots__ are defined.
|
||||||
self.__dict__.update(kwargs)
|
self.__dict__.update(kwargs)
|
||||||
@ -169,11 +169,11 @@ class BaseField(object):
|
|||||||
f_inputs = self.to_mongo.__code__.co_varnames
|
f_inputs = self.to_mongo.__code__.co_varnames
|
||||||
ex_vars = {}
|
ex_vars = {}
|
||||||
if 'fields' in f_inputs:
|
if 'fields' in f_inputs:
|
||||||
ex_vars['fields'] = fields
|
ex_vars['fields'] = fields
|
||||||
|
|
||||||
if 'use_db_field' in f_inputs:
|
if 'use_db_field' in f_inputs:
|
||||||
ex_vars['use_db_field'] = use_db_field
|
ex_vars['use_db_field'] = use_db_field
|
||||||
|
|
||||||
return self.to_mongo(value, **ex_vars)
|
return self.to_mongo(value, **ex_vars)
|
||||||
|
|
||||||
def prepare_query_value(self, op, value):
|
def prepare_query_value(self, op, value):
|
||||||
@ -206,7 +206,6 @@ class BaseField(object):
|
|||||||
elif value not in choice_list:
|
elif value not in choice_list:
|
||||||
self.error('Value must be one of %s' % unicode(choice_list))
|
self.error('Value must be one of %s' % unicode(choice_list))
|
||||||
|
|
||||||
|
|
||||||
def _validate(self, value, **kwargs):
|
def _validate(self, value, **kwargs):
|
||||||
# Check the Choices Constraint
|
# Check the Choices Constraint
|
||||||
if self.choices:
|
if self.choices:
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
import warnings
|
import warnings
|
||||||
|
|
||||||
|
from mongoengine.base.common import ALLOW_INHERITANCE, _document_registry
|
||||||
|
from mongoengine.base.fields import BaseField, ComplexBaseField, ObjectIdField
|
||||||
from mongoengine.common import _import_class
|
from mongoengine.common import _import_class
|
||||||
from mongoengine.errors import InvalidDocumentError
|
from mongoengine.errors import InvalidDocumentError
|
||||||
from mongoengine.python_support import PY3
|
from mongoengine.python_support import PY3
|
||||||
@ -7,8 +9,6 @@ from mongoengine.queryset import (DO_NOTHING, DoesNotExist,
|
|||||||
MultipleObjectsReturned,
|
MultipleObjectsReturned,
|
||||||
QuerySetManager)
|
QuerySetManager)
|
||||||
|
|
||||||
from mongoengine.base.common import _document_registry, ALLOW_INHERITANCE
|
|
||||||
from mongoengine.base.fields import BaseField, ComplexBaseField, ObjectIdField
|
|
||||||
|
|
||||||
__all__ = ('DocumentMetaclass', 'TopLevelDocumentMetaclass')
|
__all__ = ('DocumentMetaclass', 'TopLevelDocumentMetaclass')
|
||||||
|
|
||||||
@ -17,6 +17,7 @@ class DocumentMetaclass(type):
|
|||||||
"""Metaclass for all documents.
|
"""Metaclass for all documents.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
# TODO lower complexity of this method
|
||||||
def __new__(cls, name, bases, attrs):
|
def __new__(cls, name, bases, attrs):
|
||||||
flattened_bases = cls._get_bases(bases)
|
flattened_bases = cls._get_bases(bases)
|
||||||
super_new = super(DocumentMetaclass, cls).__new__
|
super_new = super(DocumentMetaclass, cls).__new__
|
||||||
|
@ -29,7 +29,7 @@ except ImportError:
|
|||||||
'because the blinker library is '
|
'because the blinker library is '
|
||||||
'not installed.')
|
'not installed.')
|
||||||
|
|
||||||
send = lambda *a, **kw: None
|
send = lambda *a, **kw: None # noqa
|
||||||
connect = disconnect = has_receivers_for = receivers_for = \
|
connect = disconnect = has_receivers_for = receivers_for = \
|
||||||
temporarily_connected_to = _fail
|
temporarily_connected_to = _fail
|
||||||
del _fail
|
del _fail
|
||||||
|
@ -9,5 +9,5 @@ tests = tests
|
|||||||
[flake8]
|
[flake8]
|
||||||
ignore=E501,F401,F403,F405,I201
|
ignore=E501,F401,F403,F405,I201
|
||||||
exclude=build,dist,docs,venv,.tox,.eggs,tests
|
exclude=build,dist,docs,venv,.tox,.eggs,tests
|
||||||
max-complexity=25
|
max-complexity=42
|
||||||
application-import-names=mongoengine,tests
|
application-import-names=mongoengine,tests
|
||||||
|
Loading…
x
Reference in New Issue
Block a user