Skip to content

Commit

Permalink
Merge pull request #668 from benjaoming/fix-django50
Browse files Browse the repository at this point in the history
Make pre-commit config work for others than Pyhon 3.9, fix Django 5.0 warning
  • Loading branch information
benjaoming committed May 17, 2022
2 parents a75fbb7 + b746b3d commit 6e8c959
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 7 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
default_language_version:
python: python3.9
python: python3
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.2.0
Expand Down
8 changes: 7 additions & 1 deletion djmoney/money.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,13 @@ def __rtruediv__(self, other):
@property
def is_localized(self):
if self.use_l10n is None:
return settings.USE_L10N
# This definitely raises a warning in Django 4 - we want to ignore RemovedInDjango50Warning
# However, we cannot ignore this specific warning class as it doesn't exist in older
# Django versions
with warnings.catch_warnings():
warnings.filterwarnings("ignore")
setting = getattr(settings, "USE_L10N", True)
return setting
return self.use_l10n

def __str__(self):
Expand Down
7 changes: 6 additions & 1 deletion tests/settings.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import warnings
from decimal import ROUND_HALF_EVEN

import django

import moneyed
from moneyed.localization import _FORMATTER, DEFAULT

Expand Down Expand Up @@ -33,7 +35,10 @@

SECRET_KEY = "foobar"

USE_L10N = True

# This now defaults to True and raises RemovedInDjango50Warning
if django.VERSION < (4, 0):
USE_L10N = True


_FORMATTER.add_sign_definition("pl_PL", moneyed.PLN, suffix=" zł")
Expand Down
9 changes: 7 additions & 2 deletions tests/test_admin.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import django
import django.contrib.admin.utils as admin_utils

import pytest
Expand All @@ -22,7 +23,9 @@
),
)
def test_display_for_field_with_legacy_formatting(legacy_formatting, settings, value, expected):
settings.USE_L10N = True
# This now defaults to True and raises RemovedInDjango50Warning
if django.VERSION < (4, 0):
settings.USE_L10N = True
# This locale has no definitions in py-moneyed, so it will work for localized money representation.
settings.LANGUAGE_CODE = "cs"
settings.DECIMAL_PLACES_DISPLAY = {}
Expand All @@ -40,7 +43,9 @@ def test_display_for_field_with_legacy_formatting(legacy_formatting, settings, v
),
)
def test_display_for_field(settings, value, expected):
settings.USE_L10N = True
# This now defaults to True and raises RemovedInDjango50Warning
if django.VERSION < (4, 0):
settings.USE_L10N = True
# This locale has no definitions in py-moneyed, so it will work for localized money representation.
settings.LANGUAGE_CODE = "cs"
assert admin_utils.display_for_field(value, MONEY_FIELD, "") == expected
Expand Down
9 changes: 7 additions & 2 deletions tests/test_tags.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import django
from django.template import Context, Template, TemplateSyntaxError
from django.utils.translation import override

Expand Down Expand Up @@ -150,8 +151,12 @@ def test_tag(string, result, context):
),
)
def test_l10n_off(settings, string, result, context):
settings.USE_L10N = False
assert_template(string, result, context)
# This test is only nice to run in older version of Django that do not either
# complain that the setting is deprecated or as of Django 5.0 entirely ignores
# this setting
if django.VERSION < (4, 0):
settings.USE_L10N = False
assert_template(string, result, context)


def test_forced_l10n():
Expand Down

0 comments on commit 6e8c959

Please sign in to comment.