From 10aa361600e5ca7b16f9a993be9f9a5733e126a8 Mon Sep 17 00:00:00 2001 From: Serkan Hosca Date: Wed, 25 Mar 2020 08:49:48 -0400 Subject: [PATCH] Pre-commit imporanize pyupgrade and docformat --- .importanizerc | 12 ------- .pre-commit-config.yaml | 64 +++++++++++++++++++++++---------- .travis.yml | 1 + MANIFEST.in | 20 +++++++++++ Makefile | 46 ++++++++++++------------ pyproject.toml | 2 +- rest_dataclasses/__init__.py | 16 +++++++-- rest_dataclasses/__version__.py | 6 ---- rest_dataclasses/serializers.py | 6 +--- rest_dataclasses/utils.py | 9 +++-- setup.cfg | 15 ++++++++ setup.py | 4 +-- tests/__init__.py | 3 -- tests/test_serializers.py | 15 ++++---- tests/test_utils.py | 3 -- 15 files changed, 133 insertions(+), 89 deletions(-) delete mode 100644 .importanizerc create mode 100644 MANIFEST.in diff --git a/.importanizerc b/.importanizerc deleted file mode 100644 index 76c7a80..0000000 --- a/.importanizerc +++ /dev/null @@ -1,12 +0,0 @@ -{ - "length": 120, - "groups": [ - { "type": "stdlib" }, - { "type": "remainder" }, - { "type": "packages", "packages": ["django"] }, - { "type": "packages", "packages": ["rest_framework"] }, - { "type": "packages", "packages": ["rest_enumfield"] }, - { "type": "packages", "packages": ["rest_dataclasses"] }, - { "type": "local" } - ] -} diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index ef140e1..37564e7 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,7 +1,47 @@ --- repos: + +- repo: https://github.com/miki725/importanize + rev: '0.7' + hooks: + - id: importanize + language_version: python3 + +- repo: https://github.com/psf/black + rev: 19.10b0 + hooks: + - id: black + language_version: python3 + +- repo: https://github.com/asottile/pyupgrade + rev: v2.1.0 + hooks: + - id: pyupgrade + args: [--py3-plus] + +- repo: https://github.com/myint/docformatter + rev: v1.3.1 + hooks: + - id: docformatter + +- repo: https://gitlab.com/pycqa/flake8 + rev: 3.7.9 + hooks: + - id: flake8 + exclude: deployment/roles + additional_dependencies: + - flake8-blind-except + - flake8-bugbear + - flake8-comprehensions + - flake8-debugger + +- repo: https://github.com/mgedmin/check-manifest + rev: '0.41' + hooks: + - id: check-manifest + - repo: https://github.com/pre-commit/pre-commit-hooks - rev: v2.4.0 + rev: v2.5.0 hooks: - id: check-added-large-files - id: check-builtin-literals @@ -14,22 +54,8 @@ repos: - id: check-xml - id: check-yaml - id: debug-statements - - id: fix-encoding-pragma - - id: flake8 - additional_dependencies: - - flake8-bugbear - - flake8-comprehensions - id: trailing-whitespace - -- repo: https://github.com/miki725/importanize - rev: '0.7' - hooks: - - id: importanize - language_version: python3 - -- repo: https://github.com/python/black - rev: 19.10b0 - hooks: - - id: black - args: [--line-length=120, --safe] - language_version: python3 + - id: mixed-line-ending + args: [--fix=lf] + - id: pretty-format-json + args: [--autofix] diff --git a/.travis.yml b/.travis.yml index 68bc71d..02a90d4 100644 --- a/.travis.yml +++ b/.travis.yml @@ -10,6 +10,7 @@ cache: python: - "3.7" + - "3.8" install: - pip install tox-travis diff --git a/MANIFEST.in b/MANIFEST.in new file mode 100644 index 0000000..152d1ef --- /dev/null +++ b/MANIFEST.in @@ -0,0 +1,20 @@ +include *.rc +include *.editorconfig +include *.rst +include *.toml +include *.txt +include *.yaml *.yml +include LICENSE +include Makefile +include pytest.ini +include tox.ini +recursive-exclude * __pycache__ +recursive-exclude * *.py[co] + +recursive-include rest_dataclasses *.py + +recursive-include docs * +recursive-exclude docs/_build * + +recursive-include tests * +recursive-include test_site * diff --git a/Makefile b/Makefile index 5e38954..394e989 100644 --- a/Makefile +++ b/Makefile @@ -1,29 +1,29 @@ PACKAGE=rest_dataclasses -FILES=$(shell find $(PACKAGE) -iname '*.py') +FILES=$(shell find $(PACKAGE) -iname '*.py' ! -iname '__*') VERSION=$(shell python setup.py --version) NEXT=$(shell semver -i $(BUMP) $(VERSION)) -COVERAGE_FLAGS?=--cov-fail-under=100 +DBS=\ + test +RESETDBS=$(addsuffix -resetdb,$(DBS)) +COVERAGE_FLAGS?=--cov-report term-missing --cov-fail-under=100 + +.PHONY: help list docs $(FILES) -.PHONY: docs $(FILES) help: @for f in $(MAKEFILE_LIST) ; do \ echo "$$f:" ; \ - grep -E '^[a-zA-Z_-%]+:.*?## .*$$' $$f | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-15s\033[0m %s\n", $$1, $$2}' ; \ + grep -E '^[^[:space:]].*:.*?## .*$$' $$f | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-15s\033[0m %s\n", $$1, $$2}' ; \ done ; \ -resetdb: ## reset test db - -psql -c "drop database test;" -h localhost -U postgres - -psql -c "create database test;" -h localhost -U postgres - +list: ## list all possible targets + @$(MAKE) -pRrq -f $(lastword $(MAKEFILE_LIST)) : 2>/dev/null | awk -v RS= -F: '/^# File/,/^# Finished Make data base/ {if ($$1 !~ "^[#.]") {print $$1}}' | sort | egrep -v -e '^[^[:alnum:]]' -e '^$@$$' clean: clean-build clean-pyc clean-test ## remove all build, test, coverage and Python artifacts clean-build: ## remove build artifacts find -name '*.sqlite3' -delete - rm -rf build/ dist/ .eggs/ - rm -rf '*.egg-info' - rm -rf '*.egg' + rm -rf build dist .eggs .mypy_cache .pytest_cache docs/_build *.egg* clean-pyc: ## remove Python file artifacts find -name '*.pyc' -delete @@ -32,7 +32,13 @@ clean-pyc: ## remove Python file artifacts find -name '__pycache__' -delete clean-test: ## remove test and coverage artifacts - rm -rf .tox/ .coverage htmlcov/ + rm -rf .tox .coverage htmlcov + +%-resetdb: + -psql postgresql://postgres:postgres@localhost -c "drop database $*;" + -psql postgresql://postgres:postgres@localhost -c "create database $*;" + +resetdb: $(RESETDBS) lint: ## run pre-commit hooks on all files if python -c "import sys; exit(1) if sys.version_info.major < 3 else exit(0)"; then \ @@ -41,26 +47,21 @@ lint: ## run pre-commit hooks on all files coverage: ## check code coverage quickly with the default Python py.test $(PYTEST_OPTS) \ - --cov-report html \ - --cov-report term-missing \ - --cov=$(PACKAGE) tests \ + --cov=$(PACKAGE) \ $(COVERAGE_FLAGS) \ - --doctest-modules \ - tests $(PACKAGE) + tests $(FILES): ## helper target to run coverage tests on a module - py.test $(PYTEST_OPTS) \ - --cov-report term-missing \ - --cov-fail-under 100 \ + py.test $(PYTEST_OPTS) $(COVERAGE_FLAGS) \ --cov=$(subst /,.,$(firstword $(subst ., ,$@))) $(subst $(PACKAGE),tests,$(dir $@))test_$(notdir $@) test: ## run tests - py.test $(PYTEST_OPTS) --doctest-modules tests $(PACKAGE) + py.test $(PYTEST_OPTS) tests $(PACKAGE) check: ## run all tests tox -history: ## generate HISTORY.rst +history: docs ## generate HISTORY.rst gitchangelog > HISTORY.rst docs: ## generate docs @@ -78,6 +79,7 @@ next: # print next version bump: history @sed -i 's/$(VERSION)/$(NEXT)/g' $(PACKAGE)/__version__.py @sed -i 's/Next version (unreleased yet)/$(NEXT) ($(shell date +"%Y-%m-%d"))/g' HISTORY.rst + @git add . @git commit -am "Bump version: $(VERSION) → $(NEXT)" tag: ## tags branch diff --git a/pyproject.toml b/pyproject.toml index ed7eee6..fd1ca0a 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.black] line-length = 120 -target-version = ['py34', 'py35', 'py36', 'py37'] +target-version = ['py36', 'py37'] include = '\.pyi?$' exclude = ''' /( diff --git a/rest_dataclasses/__init__.py b/rest_dataclasses/__init__.py index b6e5eb2..52bf790 100644 --- a/rest_dataclasses/__init__.py +++ b/rest_dataclasses/__init__.py @@ -1,4 +1,14 @@ -# -*- coding: utf-8 -*- -from __future__ import absolute_import, print_function, unicode_literals +from .__version__ import ( + __author__, + __author_email__, + __description__, + __version__, +) -from .__version__ import __author__, __author_email__, __description__, __version__ # noqa + +__all__ = [ + "__author__", + "__author_email__", + "__description__", + "__version__", +] diff --git a/rest_dataclasses/__version__.py b/rest_dataclasses/__version__.py index b357687..cdeecbc 100644 --- a/rest_dataclasses/__version__.py +++ b/rest_dataclasses/__version__.py @@ -1,9 +1,3 @@ -# -*- coding: utf-8 -*- -from __future__ import absolute_import, print_function, unicode_literals - - -# -*- coding: utf-8 -*- - __author__ = "Serkan Hosca" __author_email__ = "serkan@hosca.com" __version__ = "0.0.3" diff --git a/rest_dataclasses/serializers.py b/rest_dataclasses/serializers.py index ed8a573..bbc07fa 100644 --- a/rest_dataclasses/serializers.py +++ b/rest_dataclasses/serializers.py @@ -1,5 +1,3 @@ -# -*- coding: utf-8 -*- -from __future__ import absolute_import, print_function, unicode_literals import copy import dataclasses as da import enum @@ -9,12 +7,10 @@ from decimal import Decimal from django.core.exceptions import ValidationError as DjangoValidationError - +from rest_enumfield import EnumField from rest_framework import fields, serializers from rest_framework.exceptions import ValidationError -from rest_enumfield import EnumField - from .utils import django_to_drf_validation_error diff --git a/rest_dataclasses/utils.py b/rest_dataclasses/utils.py index d5fda7c..66af27e 100644 --- a/rest_dataclasses/utils.py +++ b/rest_dataclasses/utils.py @@ -1,8 +1,7 @@ -# -*- coding: utf-8 -*- -from __future__ import absolute_import, print_function, unicode_literals - -from django.core.exceptions import NON_FIELD_ERRORS, ValidationError as DjangoValidationError - +from django.core.exceptions import ( + NON_FIELD_ERRORS, + ValidationError as DjangoValidationError, +) from rest_framework.serializers import ValidationError from rest_framework.settings import api_settings diff --git a/setup.cfg b/setup.cfg index 17aea56..7819d0d 100644 --- a/setup.cfg +++ b/setup.cfg @@ -7,7 +7,22 @@ addopts= -r sfxX [flake8] +show-source = true +enable-extensions = B,G max-line-length = 120 exclude = .eggs,.tox,.git,*/migrations/*,*/static/CACHE/*,docs,node_modules select = C,E,F,W,B,B950 ignore = E501,W503 + +[importanize] +allow_plugins=True +plugins= + unused_imports +length=120 +groups= + stdlib + sitepackages + remainder + packages:django + packages:tests + local diff --git a/setup.py b/setup.py index e9dbe3a..adbb5e2 100644 --- a/setup.py +++ b/setup.py @@ -1,6 +1,4 @@ #!/usr/bin/env python -# -*- coding: utf-8 -*- -from __future__ import absolute_import, print_function, unicode_literals import os import shutil import sys @@ -27,7 +25,7 @@ def read(fname): os.system("python setup.py sdist bdist_wheel") os.system("twine upload dist/*") print("You probably want to also tag the version now:") - print(" git tag -a %s -m %s" % (about["__version__"], about["__version__"])) + print(" git tag -a {} -m {}".format(about["__version__"], about["__version__"])) print(" git push --tags") os.system("make clean") sys.exit() diff --git a/tests/__init__.py b/tests/__init__.py index a59afda..4eff4f4 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -1,6 +1,3 @@ -# -*- coding: utf-8 -*- -from __future__ import absolute_import, print_function, unicode_literals - import django import django.test.utils from django.conf import settings diff --git a/tests/test_serializers.py b/tests/test_serializers.py index 7c1d62b..2df99b0 100644 --- a/tests/test_serializers.py +++ b/tests/test_serializers.py @@ -1,17 +1,13 @@ -# -*- coding: utf-8 -*- -from __future__ import absolute_import, print_function, unicode_literals import dataclasses as da import enum from typing import Dict, List from django.core.exceptions import ValidationError as DjangoValidationError from django.test import SimpleTestCase - +from rest_dataclasses.serializers import DataclassSerializer from rest_framework import fields from rest_framework.exceptions import ValidationError -from rest_dataclasses.serializers import DataclassSerializer - class Color(enum.Enum): RED = "red" @@ -82,7 +78,9 @@ class Meta: fields = "id,name" serializer = Serializer(data={"id": 1, "name": "shosca", "email": "some@email.com"}) - with self.assertRaisesMessage(TypeError, 'The `fields` option must be a list or tuple or "__all__". Got str.'): + with self.assertRaisesMessage( + TypeError, 'The `fields` option must be a list or tuple or "__all__". Got str.', + ): serializer.is_valid(raise_exception=True) def test_bad_exclude(self): @@ -256,7 +254,10 @@ class Serializer(DataclassSerializer): class Meta: model = Line fields = "__all__" - extra_kwargs = {"a": {"allow_null": False, "allow_create": False}, "b": {"allow_null": False}} + extra_kwargs = { + "a": {"allow_null": False, "allow_create": False}, + "b": {"allow_null": False}, + } instance = Line() diff --git a/tests/test_utils.py b/tests/test_utils.py index f3805b9..4725aed 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -1,9 +1,6 @@ -# -*- coding: utf-8 -*- -from __future__ import absolute_import, print_function, unicode_literals import unittest from django.core.exceptions import ValidationError - from rest_dataclasses.utils import _django_to_drf