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

Various pylint fixes #246

Merged
merged 1 commit into from Apr 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 4 additions & 2 deletions umongo/abstract.py
Expand Up @@ -118,10 +118,12 @@ class BaseField(ma_fields.Field):

def __init__(self, *args, io_validate=None, unique=False, instance=None, **kwargs):
if 'missing' in kwargs:
raise RuntimeError("uMongo doesn't use `missing` argument, use `default` "
raise RuntimeError(
"uMongo doesn't use `missing` argument, use `default` "
"instead and `marshmallow_missing`/`marshmallow_default` "
"to tell `as_marshmallow_field` to use a custom value when "
"generating pure Marshmallow field.")
"generating pure Marshmallow field."
)
if 'default' in kwargs:
kwargs['missing'] = kwargs['default']

Expand Down
10 changes: 6 additions & 4 deletions umongo/builder.py
Expand Up @@ -16,8 +16,8 @@


def camel_to_snake(name):
s1 = re.sub('(.)([A-Z][a-z]+)', r'\1_\2', name)
return re.sub('([a-z0-9])([A-Z])', r'\1_\2', s1).lower()
tmp_str = re.sub('(.)([A-Z][a-z]+)', r'\1_\2', name)
return re.sub('([a-z0-9])([A-Z])', r'\1_\2', tmp_str).lower()


def _is_child(bases):
Expand Down Expand Up @@ -72,8 +72,10 @@ def _collect_indexes(meta, schema_nmspc, bases):

# Then get our own custom indexes
if is_child:
custom_indexes = [parse_index(x, base_compound_field='_cls')
for x in getattr(meta, 'indexes', ())]
custom_indexes = [
parse_index(x, base_compound_field='_cls')
for x in getattr(meta, 'indexes', ())
]
else:
custom_indexes = [parse_index(x) for x in getattr(meta, 'indexes', ())]
indexes += custom_indexes
Expand Down
6 changes: 3 additions & 3 deletions umongo/data_objects.py
Expand Up @@ -78,7 +78,7 @@ def set_modified(self):

def clear_modified(self):
self._modified = False
if len(self) and isinstance(self[0], BaseDataObject):
if self and isinstance(self[0], BaseDataObject):
for obj in self:
obj.clear_modified()

Expand Down Expand Up @@ -179,8 +179,8 @@ def __repr__(self):
def __eq__(self, other):
if isinstance(other, self.document_cls):
return other.pk == self.pk
elif isinstance(other, Reference):
if isinstance(other, Reference):
return self.pk == other.pk and self.document_cls == other.document_cls
elif isinstance(other, DBRef):
if isinstance(other, DBRef):
return self.pk == other.id and self.document_cls.collection.name == other.collection
return NotImplemented
60 changes: 32 additions & 28 deletions umongo/data_proxy.py
Expand Up @@ -19,6 +19,7 @@ def __init__(self, data=None):
self.not_loaded_fields = set()
# Inside data proxy, data are stored in mongo world representation
self._modified_data = set()
self._data = {}
self.load(data or {})

@property
Expand All @@ -29,16 +30,15 @@ def partial(self):
def to_mongo(self, update=False):
if update:
return self._to_mongo_update()
else:
return self._to_mongo()
return self._to_mongo()

def _to_mongo(self):
mongo_data = {}
for k, v in self._data.items():
field = self._fields_from_mongo_key[k]
v = field.serialize_to_mongo(v)
if v is not missing:
mongo_data[k] = v
for key, val in self._data.items():
field = self._fields_from_mongo_key[key]
val = field.serialize_to_mongo(val)
if val is not missing:
mongo_data[key] = val
return mongo_data

def _to_mongo_update(self):
Expand All @@ -48,11 +48,11 @@ def _to_mongo_update(self):
for name in self.get_modified_fields():
field = self._fields[name]
name = field.attribute or name
v = field.serialize_to_mongo(self._data[name])
if v is missing:
val = field.serialize_to_mongo(self._data[name])
if val is missing:
unset_data.append(name)
else:
set_data[name] = v
set_data[name] = val
if set_data:
mongo_data['$set'] = set_data
if unset_data:
Expand All @@ -61,14 +61,15 @@ def _to_mongo_update(self):

def from_mongo(self, data, partial=False):
self._data = {}
for k, v in data.items():
for key, val in data.items():
try:
field = self._fields_from_mongo_key[k]
field = self._fields_from_mongo_key[key]
except KeyError:
raise UnknownFieldInDBError(
_('{cls}: unknown "{key}" field found in DB.'
.format(key=k, cls=self.__class__.__name__)))
self._data[k] = field.deserialize_from_mongo(v)
raise UnknownFieldInDBError(_(
'{cls}: unknown "{key}" field found in DB.'
.format(key=key, cls=self.__class__.__name__)
))
self._data[key] = field.deserialize_from_mongo(val)
if partial:
self._collect_partial_fields(data.keys(), as_mongo_fields=True)
else:
Expand Down Expand Up @@ -158,7 +159,7 @@ def __repr__(self):
def __eq__(self, other):
if isinstance(other, dict):
return self._data == other
elif hasattr(other, '_data'):
if hasattr(other, '_data'):
return self._data == other._data
return NotImplemented

Expand All @@ -177,14 +178,16 @@ def get_modified_fields(self):

def clear_modified(self):
self._modified_data.clear()
for v in self._data.values():
if isinstance(v, BaseDataObject):
v.clear_modified()
for val in self._data.values():
if isinstance(val, BaseDataObject):
val.clear_modified()

def is_modified(self):
return (bool(self._modified_data) or
return (
bool(self._modified_data) or
any(isinstance(v, BaseDataObject) and v.is_modified()
for v in self._data.values()))
for v in self._data.values())
)

def _collect_partial_fields(self, loaded_fields, as_mongo_fields=False):
if as_mongo_fields:
Expand Down Expand Up @@ -222,8 +225,9 @@ def required_validate(self):
# Standards iterators providing oo and mongo worlds views

def items(self):
return ((key, self._data[field.attribute or key])
for key, field in self._fields.items())
return (
(key, self._data[field.attribute or key]) for key, field in self._fields.items()
)

def items_by_mongo_name(self):
return self._data.items()
Expand Down Expand Up @@ -257,13 +261,13 @@ def _to_mongo(self):

def from_mongo(self, data, partial=False):
self._data = {}
for k, v in data.items():
for key, val in data.items():
try:
field = self._fields_from_mongo_key[k]
field = self._fields_from_mongo_key[key]
except KeyError:
self._additional_data[k] = v
self._additional_data[key] = val
else:
self._data[k] = field.deserialize_from_mongo(v)
self._data[key] = field.deserialize_from_mongo(val)
if partial:
self._collect_partial_fields(data.keys(), as_mongo_fields=True)
else:
Expand Down
17 changes: 4 additions & 13 deletions umongo/document.py
Expand Up @@ -8,6 +8,7 @@
from .exceptions import (NotCreatedError, NoDBDefinedError,
AbstractDocumentError, DocumentDefinitionError)
from .template import Implementation, Template, MetaImplementation
from .data_objects import Reference


__all__ = (
Expand Down Expand Up @@ -37,7 +38,6 @@ class DocumentTemplate(Template):
or `marshmallow.post_dump`) to this class that will be passed
to the marshmallow schema internally used for this document.
"""
pass


Document = DocumentTemplate
Expand Down Expand Up @@ -76,9 +76,7 @@ class Meta:
indexes yes List of custom indexes
offspring no List of Documents inheriting this one
==================== ====================== ===========

"""

def __repr__(self):
return ('<{ClassName}('
'instance={self.instance}, '
Expand Down Expand Up @@ -148,14 +146,13 @@ def __repr__(self):
self.__module__, self.__class__.__name__, dict(self._data.items()))

def __eq__(self, other):
from .data_objects import Reference
if self.pk is None:
return self is other
elif isinstance(other, self.__class__) and other.pk is not None:
if isinstance(other, self.__class__) and other.pk is not None:
return self.pk == other.pk
elif isinstance(other, DBRef):
if isinstance(other, DBRef):
return other.collection == self.collection.name and other.id == self.pk
elif isinstance(other, Reference):
if isinstance(other, Reference):
return isinstance(self, other.document_cls) and self.pk == other.pk
return NotImplemented

Expand Down Expand Up @@ -316,7 +313,6 @@ def pre_insert(self):

.. note:: If you use an async driver, this callback can be asynchronous.
"""
pass

def pre_update(self):
"""
Expand All @@ -326,7 +322,6 @@ def pre_update(self):

.. note:: If you use an async driver, this callback can be asynchronous.
"""
pass

def pre_delete(self):
"""
Expand All @@ -336,7 +331,6 @@ def pre_delete(self):

.. note:: If you use an async driver, this callback can be asynchronous.
"""
pass

def post_insert(self, ret):
"""
Expand All @@ -345,7 +339,6 @@ def post_insert(self, ret):

.. note:: If you use an async driver, this callback can be asynchronous.
"""
pass

def post_update(self, ret):
"""
Expand All @@ -354,7 +347,6 @@ def post_update(self, ret):

.. note:: If you use an async driver, this callback can be asynchronous.
"""
pass

def post_delete(self, ret):
"""
Expand All @@ -363,4 +355,3 @@ def post_delete(self, ret):

.. note:: If you use an async driver, this callback can be asynchronous.
"""
pass
4 changes: 1 addition & 3 deletions umongo/embedded_document.py
Expand Up @@ -21,7 +21,6 @@ class EmbeddedDocumentTemplate(Template):
:class:`umongo.instance.BaseInstance` to obtain it corresponding
:class:`umongo.embedded_document.EmbeddedDocumentImplementation`.
"""
pass


EmbeddedDocument = EmbeddedDocumentTemplate
Expand Down Expand Up @@ -58,7 +57,6 @@ class Meta:
offspring no List of EmbeddedDocuments inheriting this one
==================== ====================== ===========
"""

def __repr__(self):
return ('<{ClassName}('
'instance={self.instance}, '
Expand Down Expand Up @@ -106,7 +104,7 @@ def __repr__(self):
def __eq__(self, other):
if isinstance(other, dict):
return self._data == other
elif hasattr(other, '_data'):
if hasattr(other, '_data'):
return self._data == other._data
return NotImplemented

Expand Down
27 changes: 12 additions & 15 deletions umongo/fields.py
Expand Up @@ -6,6 +6,7 @@
from marshmallow import fields as ma_fields

# from .registerer import retrieve_document
from .document import DocumentImplementation
from .exceptions import NotRegisteredDocumentError
from .template import get_template
from .data_objects import Reference, List, Dict
Expand Down Expand Up @@ -367,7 +368,7 @@ def _deserialize(self, value, attr, data, **kwargs):
if value.document_cls != self.document_cls:
raise ValidationError(_("`{document}` reference expected.").format(
document=self.document_cls.__name__))
if type(value) is not self.reference_cls:
if not isinstance(value, self.reference_cls):
value = self.reference_cls(value.document_cls, value.pk)
return value
elif isinstance(value, self.document_cls):
Expand Down Expand Up @@ -401,8 +402,6 @@ class GenericReferenceField(BaseField, ma_bonus_fields.GenericReference):
def __init__(self, *args, reference_cls=Reference, **kwargs):
super().__init__(*args, **kwargs)
self.reference_cls = reference_cls
# Avoid importing multiple times
from .document import DocumentImplementation
self._document_implementation_cls = DocumentImplementation

def _document_cls(self, class_name):
Expand All @@ -421,15 +420,15 @@ def _deserialize(self, value, attr, data, **kwargs):
if value is None:
return None
if isinstance(value, Reference):
if type(value) is not self.reference_cls:
if not isinstance(value, self.reference_cls):
value = self.reference_cls(value.document_cls, value.pk)
return value
elif isinstance(value, self._document_implementation_cls):
if isinstance(value, self._document_implementation_cls):
if not value.is_created:
raise ValidationError(
_("Cannot reference a document that has not been created yet."))
return self.reference_cls(value.__class__, value.pk)
elif isinstance(value, dict):
if isinstance(value, dict):
if value.keys() != {'cls', 'id'}:
raise ValidationError(_("Generic reference must have `id` and `cls` fields."))
try:
Expand All @@ -438,8 +437,7 @@ def _deserialize(self, value, attr, data, **kwargs):
raise ValidationError(_("Invalid `id` field."))
document_cls = self._document_cls(value['cls'])
return self.reference_cls(document_cls, _id)
else:
raise ValidationError(_("Invalid value for generic reference field."))
raise ValidationError(_("Invalid value for generic reference field."))

def _serialize_to_mongo(self, obj):
return {'_id': obj.pk, '_cls': obj.document_cls.__name__}
Expand Down Expand Up @@ -516,11 +514,10 @@ def _deserialize(self, value, attr, data, **kwargs):
try:
to_use_cls = embedded_document_cls.opts.instance.retrieve_embedded_document(
to_use_cls_name)
except NotRegisteredDocumentError as e:
raise ValidationError(str(e))
except NotRegisteredDocumentError as exc:
raise ValidationError(str(exc))
return to_use_cls(**value)
else:
return embedded_document_cls(**value)
return embedded_document_cls(**value)

def _serialize_to_mongo(self, obj):
return obj.to_mongo()
Expand All @@ -533,7 +530,7 @@ def _validate_missing(self, value):
super()._validate_missing(value)
errors = {}
if value is missing:
def get_sub_value(key):
def get_sub_value(_):
return missing
elif isinstance(value, dict):
# value is a dict for deserialization
Expand All @@ -555,8 +552,8 @@ def get_sub_value(key):
continue
try:
field._validate_missing(sub_value)
except ValidationError as ve:
errors[name] = ve.messages
except ValidationError as exc:
errors[name] = exc.messages
if errors:
raise ValidationError(errors)

Expand Down
1 change: 0 additions & 1 deletion umongo/frameworks/__init__.py
@@ -1,7 +1,6 @@
"""
Frameworks
==========

"""

from importlib import import_module
Expand Down