Skip to content

Commit

Permalink
Merge pull request #2497 from bagerard/switch_to_isort
Browse files Browse the repository at this point in the history
Switch to isort + add few pre-commit hooks
  • Loading branch information
bagerard committed Mar 31, 2021
2 parents f0fad6d + 49a4d23 commit 9680259
Show file tree
Hide file tree
Showing 48 changed files with 136 additions and 87 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/github-actions.yml
Original file line number Diff line number Diff line change
Expand Up @@ -140,4 +140,4 @@ jobs:
- name: publish pypi
uses: pypa/gh-action-pypi-publish@master
with:
password: ${{ secrets.pypi_token }}
password: ${{ secrets.pypi_token }}
17 changes: 13 additions & 4 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,17 +1,26 @@
fail_fast: false
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v3.4.0
hooks:
- id: check-merge-conflict
- id: debug-statements
- id: trailing-whitespace
- id: end-of-file-fixer
- repo: https://github.com/ambv/black
rev: 20.8b1
hooks:
- id: black
- repo: https://gitlab.com/pycqa/flake8
rev: 3.8.4
rev: 3.9.0
hooks:
- id: flake8
additional_dependencies:
- flake8-import-order
- repo: https://github.com/asottile/pyupgrade
rev: v2.7.4
rev: v2.11.0
hooks:
- id: pyupgrade
args: [--py36-plus]
- repo: https://github.com/pycqa/isort
rev: 5.8.0
hooks:
- id: isort
8 changes: 6 additions & 2 deletions CONTRIBUTING.rst
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ Travis runs the tests against the main Python 3.x versions.
Style Guide
-----------

MongoEngine's codebase is formatted with `black <https://github.com/python/black>`_, other tools like
flake8 are also used. Those tools will run as part of the CI and will fail in case the code is not formatted properly.
MongoEngine's codebase is auto-formatted with `black <https://github.com/python/black>`_, imports are ordered with `isort <https://pycqa.github.io/isort/>`_
and other tools like flake8 are also used. Those tools will run as part of the CI and will fail in case the code is not formatted properly.

To install all development tools, simply run the following commands:

Expand All @@ -58,6 +58,10 @@ To enable ``pre-commit`` simply run:
See the ``.pre-commit-config.yaml`` configuration file for more information
on how it works.

pre-commit will now run upon every commit and will reject anything that doesn't comply.

You can also run all the checks with ``pre-commit run -a``, this is what is used in the CI.

Testing
-------

Expand Down
1 change: 0 additions & 1 deletion docs/guide/validation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -120,4 +120,3 @@ the validation and cleaning of a document when you call :meth:`~mongoengine.docu
Person(age=1000).save(validate=False)
person = Person.objects.first()
assert person.age == 1000
15 changes: 8 additions & 7 deletions mongoengine/__init__.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
# Import submodules so that we can expose their __all__
from mongoengine import connection
from mongoengine import document
from mongoengine import errors
from mongoengine import fields
from mongoengine import queryset
from mongoengine import signals
from mongoengine import (
connection,
document,
errors,
fields,
queryset,
signals,
)

# Import everything from each submodule so that it can be accessed via
# mongoengine, e.g. instead of `from mongoengine.connection import connect`,
Expand All @@ -17,7 +19,6 @@
from mongoengine.queryset import * # noqa: F401
from mongoengine.signals import * # noqa: F401


__all__ = (
list(document.__all__)
+ list(fields.__all__)
Expand Down
3 changes: 1 addition & 2 deletions mongoengine/base/document.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import copy

import numbers
from functools import partial

from bson import DBRef, ObjectId, SON, json_util
import pymongo
from bson import SON, DBRef, ObjectId, json_util

from mongoengine import signals
from mongoengine.base.common import get_document
Expand Down
8 changes: 6 additions & 2 deletions mongoengine/base/fields.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import operator
import weakref

from bson import DBRef, ObjectId, SON
import pymongo
from bson import SON, DBRef, ObjectId

from mongoengine.base.common import UPDATE_OPERATORS
from mongoengine.base.datastructures import BaseDict, BaseList, EmbeddedDocumentList
from mongoengine.base.datastructures import (
BaseDict,
BaseList,
EmbeddedDocumentList,
)
from mongoengine.common import _import_class
from mongoengine.errors import DeprecatedError, ValidationError

Expand Down
7 changes: 5 additions & 2 deletions mongoengine/base/metaclasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@
import warnings

from mongoengine.base.common import _document_registry
from mongoengine.base.fields import BaseField, ComplexBaseField, ObjectIdField
from mongoengine.base.fields import (
BaseField,
ComplexBaseField,
ObjectIdField,
)
from mongoengine.common import _import_class
from mongoengine.errors import InvalidDocumentError
from mongoengine.queryset import (
Expand All @@ -12,7 +16,6 @@
QuerySetManager,
)


__all__ = ("DocumentMetaclass", "TopLevelDocumentMetaclass")


Expand Down
2 changes: 1 addition & 1 deletion mongoengine/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,8 +217,8 @@ def register_connection(

def disconnect(alias=DEFAULT_CONNECTION_NAME):
"""Close the connection with a given alias."""
from mongoengine.base.common import _get_documents_by_db
from mongoengine import Document
from mongoengine.base.common import _get_documents_by_db

if alias in _connections:
get_connection(alias=alias).close()
Expand Down
9 changes: 7 additions & 2 deletions mongoengine/dereference.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from bson import DBRef, SON
from bson import SON, DBRef

from mongoengine.base import (
BaseDict,
Expand All @@ -10,7 +10,12 @@
from mongoengine.base.datastructures import LazyReference
from mongoengine.connection import get_db
from mongoengine.document import Document, EmbeddedDocument
from mongoengine.fields import DictField, ListField, MapField, ReferenceField
from mongoengine.fields import (
DictField,
ListField,
MapField,
ReferenceField,
)
from mongoengine.queryset import QuerySet


Expand Down
15 changes: 12 additions & 3 deletions mongoengine/document.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import re

from bson.dbref import DBRef
import pymongo
from bson.dbref import DBRef
from pymongo.read_preferences import ReadPreference

from mongoengine import signals
Expand All @@ -16,14 +16,23 @@
)
from mongoengine.common import _import_class
from mongoengine.connection import DEFAULT_CONNECTION_NAME, get_db
from mongoengine.context_managers import set_write_concern, switch_collection, switch_db
from mongoengine.context_managers import (
set_write_concern,
switch_collection,
switch_db,
)
from mongoengine.errors import (
InvalidDocumentError,
InvalidQueryError,
SaveConditionError,
)
from mongoengine.pymongo_support import list_collection_names
from mongoengine.queryset import NotUniqueError, OperationError, QuerySet, transform
from mongoengine.queryset import (
NotUniqueError,
OperationError,
QuerySet,
transform,
)

__all__ = (
"Document",
Expand Down
1 change: 0 additions & 1 deletion mongoengine/errors.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from collections import defaultdict


__all__ = (
"NotRegistered",
"InvalidDocumentError",
Expand Down
11 changes: 7 additions & 4 deletions mongoengine/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
from io import BytesIO
from operator import itemgetter

from bson import Binary, DBRef, ObjectId, SON
from bson.int64 import Int64
import gridfs
import pymongo
from bson import SON, Binary, DBRef, ObjectId
from bson.int64 import Int64
from pymongo import ReturnDocument

try:
Expand All @@ -22,7 +22,6 @@
else:
import dateutil.parser


from mongoengine.base import (
BaseDocument,
BaseField,
Expand All @@ -36,7 +35,11 @@
from mongoengine.common import _import_class
from mongoengine.connection import DEFAULT_CONNECTION_NAME, get_db
from mongoengine.document import Document, EmbeddedDocument
from mongoengine.errors import DoesNotExist, InvalidQueryError, ValidationError
from mongoengine.errors import (
DoesNotExist,
InvalidQueryError,
ValidationError,
)
from mongoengine.queryset import DO_NOTHING
from mongoengine.queryset.base import BaseQuerySet
from mongoengine.queryset.transform import STRING_OPERATORS
Expand Down
1 change: 0 additions & 1 deletion mongoengine/mongodb_support.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
"""
from mongoengine.connection import get_connection


# Constant that can be used to compare the version retrieved with
# get_mongodb_version()
MONGODB_34 = (3, 4)
Expand Down
6 changes: 2 additions & 4 deletions mongoengine/queryset/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@
import itertools
import re
import warnings

from collections.abc import Mapping

from bson import SON, json_util
from bson.code import Code
import pymongo
import pymongo.errors
from bson import SON, json_util
from bson.code import Code
from pymongo.collection import ReturnDocument
from pymongo.common import validate_read_preference
from pymongo.read_concern import ReadConcern
Expand All @@ -34,7 +33,6 @@
from mongoengine.queryset.field_list import QueryFieldList
from mongoengine.queryset.visitor import Q, QNode


__all__ = ("BaseQuerySet", "DO_NOTHING", "NULLIFY", "CASCADE", "DENY", "PULL")

# Delete rules
Expand Down
1 change: 1 addition & 0 deletions mongoengine/queryset/manager.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from functools import partial

from mongoengine.queryset.queryset import QuerySet

__all__ = ("queryset_manager", "QuerySetManager")
Expand Down
2 changes: 1 addition & 1 deletion mongoengine/queryset/queryset.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
from mongoengine.errors import OperationError
from mongoengine.queryset.base import (
BaseQuerySet,
CASCADE,
DENY,
DO_NOTHING,
NULLIFY,
PULL,
BaseQuerySet,
)

__all__ = (
Expand Down
4 changes: 2 additions & 2 deletions mongoengine/queryset/transform.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from collections import defaultdict

from bson import ObjectId, SON
from bson.dbref import DBRef
import pymongo
from bson import SON, ObjectId
from bson.dbref import DBRef

from mongoengine.base import UPDATE_OPERATORS
from mongoengine.common import _import_class
Expand Down
1 change: 0 additions & 1 deletion requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
black
flake8
flake8-import-order
pre-commit
pytest
ipdb
Expand Down
10 changes: 9 additions & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,17 @@
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

[tool:pytest]
# Limits the discovery to tests directory
# avoids that it runs for instance the benchmark
testpaths = tests

[isort]
known_first_party = mongoengine,tests
default_section = THIRDPARTY
multi_line_output = 3
include_trailing_comma = True
combine_as_imports = True
line_length = 70
ensure_newline_before_comments = 1
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ def finalize_options(self):

def run_tests(self):
# import here, cause outside the eggs aren't loaded
from pkg_resources import _namespace_packages
import pytest
from pkg_resources import _namespace_packages

# Purge modules under test from sys.modules. The test loader will
# re-import them from the build location. Required when 2to3 is used
Expand Down
1 change: 1 addition & 0 deletions tests/document/test_delta.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import unittest

from bson import SON

from mongoengine import *
from mongoengine.pymongo_support import list_collection_names
from tests.utils import MongoDBTestCase
Expand Down
7 changes: 5 additions & 2 deletions tests/document/test_indexes.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import unittest
from datetime import datetime

import pytest
from pymongo.collation import Collation
from pymongo.errors import OperationFailure
import pytest

from mongoengine import *
from mongoengine.connection import get_db
from mongoengine.mongodb_support import MONGODB_42, get_mongodb_version
from mongoengine.mongodb_support import (
MONGODB_42,
get_mongodb_version,
)


class TestIndexes(unittest.TestCase):
Expand Down
8 changes: 6 additions & 2 deletions tests/document/test_instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
from datetime import datetime

import bson
import pytest
from bson import DBRef, ObjectId
from pymongo.errors import DuplicateKeyError
import pytest

from mongoengine import *
from mongoengine import signals
Expand All @@ -23,7 +23,11 @@
NotUniqueError,
SaveConditionError,
)
from mongoengine.mongodb_support import MONGODB_34, MONGODB_36, get_mongodb_version
from mongoengine.mongodb_support import (
MONGODB_34,
MONGODB_36,
get_mongodb_version,
)
from mongoengine.pymongo_support import list_collection_names
from mongoengine.queryset import NULLIFY, Q
from tests import fixtures
Expand Down

0 comments on commit 9680259

Please sign in to comment.