Skip to content

Commit

Permalink
Add pre-commit hooks
Browse files Browse the repository at this point in the history
- Add and apply pre-commit hooks for black and pyupgrade.
- Remove the lint step from the CI workflow as it now fully duplicates
  what the pre-commit step does.
- Update pre-commit hooks with `pre-commit autoupdate`.
- Pin the rstcheck hook to a commit hash.
  • Loading branch information
antonagestam authored and Stranger6667 committed Apr 19, 2021
1 parent 0e91d9e commit 78e7aed
Show file tree
Hide file tree
Showing 13 changed files with 45 additions and 47 deletions.
17 changes: 1 addition & 16 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ jobs:

- run: pip install pre-commit
- run: pre-commit run --all-files

docs:
name: docs
runs-on: ubuntu-latest
Expand All @@ -33,22 +34,6 @@ jobs:

- name: Run docs tox job
run: tox -e docs
lint:
name: lint
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
with:
fetch-depth: 1

- uses: actions/setup-python@v2
with:
python-version: 3.7

- run: pip install tox

- name: Run lint tox job
run: tox -e lint

tests:
strategy:
Expand Down
21 changes: 17 additions & 4 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
repos:
- repo: git://github.com/pre-commit/pre-commit-hooks
rev: v3.3.0
rev: v3.4.0
hooks:
- id: trailing-whitespace
- id: check-yaml
Expand All @@ -11,26 +11,39 @@ repos:
args:
- --remove

- repo: https://github.com/asottile/pyupgrade
rev: v2.12.0
hooks:
- id: pyupgrade
args:
- --py3-plus

- repo: https://github.com/asottile/seed-isort-config
rev: v2.2.0
hooks:
- id: seed-isort-config

- repo: https://github.com/pre-commit/mirrors-isort
rev: v5.6.4
rev: v5.8.0
hooks:
- id: isort

- repo: https://gitlab.com/pycqa/flake8
rev: 3.8.4
rev: 3.9.1
hooks:
- id: flake8
exclude: ^docs

- repo: https://github.com/myint/rstcheck
rev: master
rev: 3f92957
hooks:
- id: rstcheck
additional_dependencies:
- sphinx==3.1.2
args: [--ignore-directives=code]

- repo: https://github.com/psf/black
rev: 20.8b1
hooks:
- id: black
exclude: ^docs
6 changes: 3 additions & 3 deletions djmoney/contrib/exchange/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def get_rate(source, target, backend=None):
"""
if backend is None:
backend = get_default_backend_name()
key = "djmoney:get_rate:%s:%s:%s" % (source, target, backend)
key = "djmoney:get_rate:{}:{}:{}".format(source, target, backend)
result = cache.get(key)
if result is not None:
return result
Expand All @@ -57,7 +57,7 @@ def _get_rate(source, target, backend):
return 1
rates = Rate.objects.filter(currency__in=(source, target), backend=backend).select_related("backend")
if not rates:
raise MissingRate("Rate %s -> %s does not exist" % (source, target))
raise MissingRate("Rate {} -> {} does not exist".format(source, target))
if len(rates) == 1:
return _try_to_get_rate_directly(source, target, rates[0])
return _get_rate_via_base(rates, target)
Expand All @@ -74,7 +74,7 @@ def _try_to_get_rate_directly(source, target, rate):
elif rate.backend.base_currency == target and rate.currency == source:
return 1 / rate.value
# Case when target or source is not a base currency
raise MissingRate("Rate %s -> %s does not exist" % (source, target))
raise MissingRate("Rate {} -> {} does not exist".format(source, target))


def _get_rate_via_base(rates, target):
Expand Down
2 changes: 1 addition & 1 deletion djmoney/forms/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def __init__(
min_value=min_value,
max_digits=max_digits,
decimal_places=decimal_places,
**kwargs
**kwargs,
)
currency_field = ChoiceField(choices=currency_choices)

Expand Down
2 changes: 1 addition & 1 deletion djmoney/money.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ def get_current_locale():
if locale.upper() in _FORMATTER.formatting_definitions:
return locale

locale = ("%s_%s" % (locale, locale)).upper()
locale = ("{}_{}".format(locale, locale)).upper()
if locale in _FORMATTER.formatting_definitions:
return locale

Expand Down
14 changes: 7 additions & 7 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@
master_doc = 'index'

# General information about the project.
project = u'Django-money'
copyright = u'2016, Jacob Hansson'
author = u'Jacob Hansson'
project = 'Django-money'
copyright = '2016, Jacob Hansson'
author = 'Jacob Hansson'

# 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 Expand Up @@ -227,8 +227,8 @@
# (source start file, target name, title,
# author, documentclass [howto, manual, or own class]).
latex_documents = [
(master_doc, 'Django-money.tex', u'Django-money Documentation',
u'Jacob Hansson', 'manual'),
(master_doc, 'Django-money.tex', 'Django-money Documentation',
'Jacob Hansson', 'manual'),
]

# The name of an image file (relative to this directory) to place at the top of
Expand Down Expand Up @@ -257,7 +257,7 @@
# One entry per manual page. List of tuples
# (source start file, name, description, authors, manual section).
man_pages = [
(master_doc, 'django-money', u'Django-money Documentation',
(master_doc, 'django-money', 'Django-money Documentation',
[author], 1)
]

Expand All @@ -271,7 +271,7 @@
# (source start file, target name, title, author,
# dir menu entry, description, category)
texinfo_documents = [
(master_doc, 'Django-money', u'Django-money Documentation',
(master_doc, 'Django-money', 'Django-money Documentation',
author, 'Django-money', 'One line description of project.',
'Miscellaneous'),
]
Expand Down
4 changes: 4 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
[build-system]
requires = ["setuptools >= 40.6.0", "wheel"]
build-backend = "setuptools.build_meta"

[tool.black]
line-length = 120
target-version = ["py35"]
4 changes: 2 additions & 2 deletions tests/contrib/exchange/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,13 @@ def test_rates_via_base(source, target, expected, django_assert_num_queries):
@pytest.mark.parametrize("source, target", (("NOK", "ZAR"), ("ZAR", "NOK"), ("USD", "ZAR"), ("ZAR", "USD")))
@pytest.mark.usefixtures("default_openexchange_rates")
def test_unknown_currency_with_partially_exiting_currencies(source, target):
with pytest.raises(MissingRate, match="Rate %s \\-\\> %s does not exist" % (source, target)):
with pytest.raises(MissingRate, match="Rate {} \\-\\> {} does not exist".format(source, target)):
get_rate(source, target)


@pytest.mark.parametrize("source, target", (("USD", "EUR"), ("SEK", "ZWL")))
def test_unknown_currency(source, target):
with pytest.raises(MissingRate, match="Rate %s \\-\\> %s does not exist" % (source, target)):
with pytest.raises(MissingRate, match="Rate {} \\-\\> {} does not exist".format(source, target)):
get_rate(source, target)


Expand Down
4 changes: 2 additions & 2 deletions tests/contrib/test_django_rest_framework.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,8 @@ def test_serializer_with_fields(self):
@pytest.mark.parametrize(
"value, error",
(
(Money(50, "EUR"), u"Ensure this value is greater than or equal to 100.00 €."),
(Money(1500, "EUR"), u"Ensure this value is less than or equal to 1,000.00 €."),
(Money(50, "EUR"), "Ensure this value is greater than or equal to 100.00 €."),
(Money(1500, "EUR"), "Ensure this value is less than or equal to 1,000.00 €."),
(Money(40, "USD"), "Ensure this value is greater than or equal to $50.00."),
(Money(600, "USD"), "Ensure this value is less than or equal to $500.00."),
(Money(400, "NOK"), "Ensure this value is greater than or equal to 500.00 Nkr."),
Expand Down
2 changes: 1 addition & 1 deletion tests/migrations/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def makemigrations():


def get_migration(name):
return __import__("money_app.migrations.%s_%s" % (name, MIGRATION_NAME), fromlist=["Migration"]).Migration
return __import__("money_app.migrations.{}_{}".format(name, MIGRATION_NAME), fromlist=["Migration"]).Migration


def get_operations(migration_name):
Expand Down
6 changes: 3 additions & 3 deletions tests/test_form.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,8 @@ class TestValidation:
@pytest.mark.parametrize(
"value, error",
(
(Money(50, "EUR"), u"Ensure this value is greater than or equal to 100.00 €."),
(Money(1500, "EUR"), u"Ensure this value is less than or equal to 1,000.00 €."),
(Money(50, "EUR"), "Ensure this value is greater than or equal to 100.00 €."),
(Money(1500, "EUR"), "Ensure this value is less than or equal to 1,000.00 €."),
(Money(40, "USD"), "Ensure this value is greater than or equal to $50.00."),
(Money(600, "USD"), "Ensure this value is less than or equal to $500.00."),
(Money(400, "NOK"), "Ensure this value is greater than or equal to 500.00 Nkr."),
Expand Down Expand Up @@ -171,7 +171,7 @@ def test_positive_validator(self, value):
def test_default_django_validator(self):
form = MoneyModelFormWithValidation(data={"balance_0": 0, "balance_1": "GBP"})
assert not form.is_valid()
assert form.errors == {"balance": [u"Ensure this value is greater than or equal to GB£100.00."]}
assert form.errors == {"balance": ["Ensure this value is greater than or equal to GB£100.00."]}


class TestDisabledField:
Expand Down
2 changes: 1 addition & 1 deletion tests/test_money.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def test_repr():


def test_html_safe():
assert Money("10.5", "EUR").__html__() == u"10.50\xa0€"
assert Money("10.5", "EUR").__html__() == "10.50\xa0€"


def test_html_unsafe():
Expand Down
8 changes: 2 additions & 6 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,9 @@ commands = py.test --ds=tests.settings --cov=./djmoney {posargs} -W error::Depre

[testenv:lint]
deps =
flake8
isort
black
pre-commit
commands =
flake8 {toxinidir}/djmoney {toxinidir}/tests
isort -c {toxinidir}/djmoney {toxinidir}/tests
black -l 120 --check --diff {toxinidir}/djmoney {toxinidir}/tests setup.py
pre-commit run --all-files

[django]
1.11.x =
Expand Down

0 comments on commit 78e7aed

Please sign in to comment.