Skip to content

Commit

Permalink
fix flake8 warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
cp-bagerard committed Dec 12, 2020
1 parent eabb8f6 commit 8a1a68e
Show file tree
Hide file tree
Showing 26 changed files with 110 additions and 135 deletions.
30 changes: 15 additions & 15 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
fail_fast: false
repos:
- repo: https://github.com/ambv/black
rev: 20.8b1
hooks:
- id: black
- repo: https://gitlab.com/pycqa/flake8
rev: 3.8.0a2
hooks:
- id: flake8
additional_dependencies:
- flake8-import-order
- repo: https://github.com/asottile/pyupgrade
rev: v2.7.4
hooks:
- id: pyupgrade
args: [--py36-plus]
- repo: https://github.com/ambv/black
rev: 20.8b1
hooks:
- id: black
- repo: https://gitlab.com/pycqa/flake8
rev: 3.8.0a2
hooks:
- id: flake8
additional_dependencies:
- flake8-import-order
- repo: https://github.com/asottile/pyupgrade
rev: v2.7.4
hooks:
- id: pyupgrade
args: [--py36-plus]
2 changes: 1 addition & 1 deletion benchmarks/test_basic_doc_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def init_book():

print(
"Doc setattr: %.3fus"
% (timeit(lambda: setattr(b, "name", "New name"), 10000) * 10 ** 6)
% (timeit(lambda: setattr(b, "name", "New name"), 10000) * 10 ** 6) # noqa B010
)

print("Doc to mongo: %.3fus" % (timeit(b.to_mongo, 1000) * 10 ** 6))
Expand Down
2 changes: 1 addition & 1 deletion docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@

# General information about the project.
project = "MongoEngine"
copyright = "2009, MongoEngine Authors"
copyright = "2009, MongoEngine Authors" # noqa: A001

# The version info for the project you're documenting, acts as replacement for
# |version| and |release|, also used in various other places throughout the
Expand Down
12 changes: 6 additions & 6 deletions mongoengine/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@
# mongoengine, e.g. instead of `from mongoengine.connection import connect`,
# users can simply use `from mongoengine import connect`, or even
# `from mongoengine import *` and then `connect('testdb')`.
from mongoengine.connection import *
from mongoengine.document import *
from mongoengine.errors import *
from mongoengine.fields import *
from mongoengine.queryset import *
from mongoengine.signals import *
from mongoengine.connection import * # noqa: F401
from mongoengine.document import * # noqa: F401
from mongoengine.errors import * # noqa: F401
from mongoengine.fields import * # noqa: F401
from mongoengine.queryset import * # noqa: F401
from mongoengine.signals import * # noqa: F401


__all__ = (
Expand Down
4 changes: 1 addition & 3 deletions mongoengine/base/datastructures.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,9 +180,7 @@ def __setitem__(self, key, value):
def _mark_as_changed(self, key=None):
if hasattr(self._instance, "_mark_as_changed"):
if key is not None:
self._instance._mark_as_changed(
"{}.{}".format(self._name, key % len(self))
)
self._instance._mark_as_changed(f"{self._name}.{key % len(self)}")
else:
self._instance._mark_as_changed(self._name)

Expand Down
14 changes: 5 additions & 9 deletions mongoengine/base/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,7 @@ def __init__(self, *args, **values):
list(self._fields.keys()) + ["id", "pk", "_cls", "_text_score"]
)
if _undefined_fields:
msg = ('The fields "{}" do not exist on the document "{}"').format(
_undefined_fields, self._class_name
)
msg = f'The fields "{_undefined_fields}" do not exist on the document "{self._class_name}"'
raise FieldDoesNotExist(msg)

if self.STRICT and not self._dynamic:
Expand Down Expand Up @@ -231,10 +229,10 @@ def __setstate__(self, data):
setattr(self, k, data[k])
if "_fields_ordered" in data:
if self._dynamic:
setattr(self, "_fields_ordered", data["_fields_ordered"])
self._fields_ordered = data["_fields_ordered"]
else:
_super_fields_ordered = type(self)._fields_ordered
setattr(self, "_fields_ordered", _super_fields_ordered)
self._fields_ordered = _super_fields_ordered

dynamic_fields = data.get("_dynamic_fields") or SON()
for k in dynamic_fields.keys():
Expand Down Expand Up @@ -576,7 +574,7 @@ def _nestable_types_clear_changed_fields(data):
else:
iterator = data.items()

for index_or_key, value in iterator:
for _index_or_key, value in iterator:
if hasattr(value, "_get_changed_fields") and not isinstance(
value, Document
): # don't follow references
Expand Down Expand Up @@ -999,9 +997,7 @@ def _geo_indices(cls, inspected=None, parent_field=None):
"PolygonField",
)

geo_field_types = tuple(
[_import_class(field) for field in geo_field_type_names]
)
geo_field_types = tuple(_import_class(field) for field in geo_field_type_names)

for field in cls._fields.values():
if not isinstance(field, geo_field_types):
Expand Down
2 changes: 1 addition & 1 deletion mongoengine/base/metaclasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ def __new__(mcs, name, bases, attrs):
# allow_inheritance to False. If the base Document allows inheritance,
# none of its subclasses can override allow_inheritance to False.
simple_class = all(
[b._meta.get("abstract") for b in flattened_bases if hasattr(b, "_meta")]
b._meta.get("abstract") for b in flattened_bases if hasattr(b, "_meta")
)
if (
not simple_class
Expand Down
4 changes: 2 additions & 2 deletions mongoengine/dereference.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,10 @@ def __call__(self, items, max_depth=1, instance=None, name=None):
doc_type = doc_type.document_type
is_list = not hasattr(items, "items")

if is_list and all([i.__class__ == doc_type for i in items]):
if is_list and all(i.__class__ == doc_type for i in items):
return items
elif not is_list and all(
[i.__class__ == doc_type for i in items.values()]
i.__class__ == doc_type for i in items.values()
):
return items
elif not field.dbref:
Expand Down
2 changes: 1 addition & 1 deletion mongoengine/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -1743,7 +1743,7 @@ def __repr__(self):

def __str__(self):
gridout = self.get()
filename = getattr(gridout, "filename") if gridout else "<no file>"
filename = gridout.filename if gridout else "<no file>"
return f"<{self.__class__.__name__}: {filename} ({self.grid_id})>"

def __eq__(self, other):
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[flake8]
ignore=E501,F401,F403,F405,I201,I202,W504, W605, W503
ignore=E501,F403,F405,I201,I202,W504,W605,W503,B007
exclude=build,dist,docs,venv,venv3,.tox,.eggs,tests
max-complexity=47
application-import-names=mongoengine,tests
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

# Hack to silence atexit traceback in newer python versions
try:
import multiprocessing
import multiprocessing # noqa: F401
except ImportError:
pass

Expand Down
2 changes: 1 addition & 1 deletion tests/document/test_class_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def test_definition(self):
"""Ensure that document may be defined using fields."""
assert ["_cls", "age", "id", "name"] == sorted(self.Person._fields.keys())
assert ["IntField", "ObjectIdField", "StringField", "StringField"] == sorted(
[x.__class__.__name__ for x in self.Person._fields.values()]
x.__class__.__name__ for x in self.Person._fields.values()
)

def test_get_db(self):
Expand Down
13 changes: 4 additions & 9 deletions tests/document/test_inheritance.py
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ class C(A, B):
C.ensure_indexes()

assert sorted(
[idx["key"] for idx in C._get_collection().index_information().values()]
idx["key"] for idx in C._get_collection().index_information().values()
) == sorted([[("_cls", 1), ("b", 1)], [("_id", 1)], [("_cls", 1), ("a", 1)]])

def test_polymorphic_queries(self):
Expand Down Expand Up @@ -467,7 +467,7 @@ class City(Document):
assert city.pk is None
# TODO: expected error? Shouldn't we create a new error type?
with pytest.raises(KeyError):
setattr(city, "pk", 1)
city.pk = 1

def test_allow_inheritance_embedded_document(self):
"""Ensure embedded documents respect inheritance."""
Expand Down Expand Up @@ -499,13 +499,8 @@ class DateCreatedDocument(Document):
class DateUpdatedDocument(Document):
meta = {"allow_inheritance": True, "abstract": True}

try:

class MyDocument(DateCreatedDocument, DateUpdatedDocument):
pass

except Exception:
assert False, "Couldn't create MyDocument class"
class MyDocument(DateCreatedDocument, DateUpdatedDocument):
pass

def test_abstract_documents(self):
"""Ensure that a document superclass can be marked as abstract
Expand Down
5 changes: 1 addition & 4 deletions tests/document/test_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,10 +210,7 @@ class Child(Parent):
child.reference = parent

# Saving the child should not raise a ValidationError
try:
child.save()
except ValidationError as e:
self.fail("ValidationError raised: %s" % e.message)
child.save()


if __name__ == "__main__":
Expand Down
2 changes: 1 addition & 1 deletion tests/fields/test_complex_datetime_field.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class LogEntry(Document):
assert log == log1

# Test string padding
microsecond = map(int, [math.pow(10, x) for x in range(6)])
microsecond = map(int, (math.pow(10, x) for x in range(6)))
mm = dd = hh = ii = ss = [1, 10]

for values in itertools.product([2014], mm, dd, hh, ii, ss, microsecond):
Expand Down
4 changes: 1 addition & 3 deletions tests/fields/test_email_field.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import sys

import pytest

from mongoengine import *
from mongoengine import Document, EmailField, ValidationError
from tests.utils import MongoDBTestCase


Expand Down
4 changes: 2 additions & 2 deletions tests/fields/test_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -1357,9 +1357,9 @@ class Bar(Document):
foo.delete()
bar = Bar.objects.get()
with pytest.raises(DoesNotExist):
getattr(bar, "ref")
bar.ref
with pytest.raises(DoesNotExist):
getattr(bar, "generic_ref")
bar.generic_ref

# When auto_dereference is disabled, there is no trouble returning DBRef
bar = Bar.objects.get()
Expand Down
2 changes: 1 addition & 1 deletion tests/fields/test_file_field.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from mongoengine.connection import get_db

try:
from PIL import Image
from PIL import Image # noqa: F401

HAS_PIL = True
except ImportError:
Expand Down
4 changes: 2 additions & 2 deletions tests/fields/test_sequence_field.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,8 @@ class Person(Document):
ids = [i.id for i in Person.objects]
assert ids == list(range(1, 11))

id = [i.id for i in Animal.objects]
assert id == list(range(1, 11))
_id = [i.id for i in Animal.objects]
assert _id == list(range(1, 11))

c = self.db["mongoengine.counters"].find_one({"_id": "person.id"})
assert c["next"] == 10
Expand Down
5 changes: 0 additions & 5 deletions tests/queryset/test_pickable.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import pickle
import unittest

from mongoengine import Document, IntField, StringField
from mongoengine.connection import connect
from tests.utils import MongoDBTestCase


Expand All @@ -22,14 +20,11 @@ def setUp(self):
self.john = Person.objects.create(name="John", age=21)

def test_picke_simple_qs(self):

qs = Person.objects.all()

pickle.dumps(qs)

def _get_loaded(self, qs):
s = pickle.dumps(qs)

return pickle.loads(s)

def test_unpickle(self):
Expand Down
17 changes: 8 additions & 9 deletions tests/queryset/test_queryset.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

from bson import DBRef, ObjectId
import pymongo
from pymongo.read_concern import ReadConcern
from pymongo.read_preferences import ReadPreference
from pymongo.results import UpdateResult
import pytest
Expand Down Expand Up @@ -449,15 +448,15 @@ class A(Document):

# test iterating over the result set
cnt = 0
for a in A.objects.batch_size(10):
for _ in A.objects.batch_size(10):
cnt += 1
assert cnt == 100

# test chaining
qs = A.objects.all()
qs = qs.limit(10).batch_size(20).skip(91)
cnt = 0
for a in qs:
for _ in qs:
cnt += 1
assert cnt == 9

Expand Down Expand Up @@ -1146,7 +1145,7 @@ def test_repeated_iteration(self):
people2 = [person for person in queryset]

# Check that it still works even if iteration is interrupted.
for person in queryset:
for _person in queryset:
break
people3 = [person for person in queryset]

Expand Down Expand Up @@ -1182,7 +1181,7 @@ def __repr__(self):
assert "[<Doc: 1>, <Doc: 2>, <Doc: 3>]" == "%s" % docs

assert docs.count(with_limit_and_skip=True) == 3
for doc in docs:
for _ in docs:
assert ".. queryset mid-iteration .." == repr(docs)

def test_regex_query_shortcuts(self):
Expand Down Expand Up @@ -3171,10 +3170,10 @@ class Test(Document):

Test.drop_collection()

for i in range(50):
for _ in range(50):
Test(val=1).save()

for i in range(20):
for _ in range(20):
Test(val=2).save()

freqs = Test.objects.item_frequencies("val", map_reduce=False, normalize=True)
Expand Down Expand Up @@ -3207,7 +3206,7 @@ def test_average(self):

for i, weight in enumerate(ages):
self.Person(
name="test meta%i", person_meta=self.PersonMeta(weight=weight)
name=f"test meta{i}", person_meta=self.PersonMeta(weight=weight)
).save()

assert (
Expand Down Expand Up @@ -3246,7 +3245,7 @@ def test_sum(self):

# test summing over a filtered queryset
assert self.Person.objects.filter(age__gte=50).sum("age") == sum(
[a for a in ages if a >= 50]
a for a in ages if a >= 50
)

def test_sum_over_db_field(self):
Expand Down
2 changes: 0 additions & 2 deletions tests/test_common.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import unittest

import pytest

from mongoengine import Document
Expand Down
4 changes: 2 additions & 2 deletions tests/test_context_managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,11 +180,11 @@ class Group(Document):

with no_dereference(Group) as Group:
group = Group.objects.first()
assert all([not isinstance(m, User) for m in group.members])
assert all(not isinstance(m, User) for m in group.members)
assert not isinstance(group.ref, User)
assert not isinstance(group.generic, User)

assert all([isinstance(m, User) for m in group.members])
assert all(isinstance(m, User) for m in group.members)
assert isinstance(group.ref, User)
assert isinstance(group.generic, User)

Expand Down
Loading

0 comments on commit 8a1a68e

Please sign in to comment.