Skip to content

Commit

Permalink
chore: Add py.typed to start exporting types
Browse files Browse the repository at this point in the history
* chore: Add py.typed to start exporting types

* chore: Add mypy

* chore: Add mypy to pre-commit

* chore: Name variables in `add_currency` call

Resolves a mypy error.

If you look back at pymoneyed in 2019 when this was
added, the signature of the function was
`def add_currency(code, numeric, name, countries):` (source: https://github.com/py-moneyed/py-moneyed/blob/b3ed12f9aa9c9dec83f9f09b9e1daad7949fe577/moneyed/classes.py#L250)

Now the function signature is a little different:
`def add_currency(code: str, numeric: Optional[str], sub_unit: int = 1, name: Optional[str] = None, countries: Optional[List[str]] = None,)`.

* chore: Fix remaining mypy errors
  • Loading branch information
sondrelg committed Jan 6, 2023
1 parent ecdabca commit d6122a6
Show file tree
Hide file tree
Showing 9 changed files with 31 additions and 6 deletions.
9 changes: 9 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,12 @@ repos:
hooks:
- id: black
exclude: ^docs

- repo: https://github.com/pre-commit/mirrors-mypy
rev: v0.981
hooks:
- id: mypy
additional_dependencies:
- django-stubs
- py-moneyed
files: 'djmoney\/.*\.py|tests\/.*\.py'
5 changes: 3 additions & 2 deletions djmoney/contrib/exchange/backends/base.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import json
import ssl
from decimal import Decimal
from typing import Optional
from urllib.parse import parse_qsl, urlparse, urlunparse
from urllib.request import Request, urlopen

Expand All @@ -19,8 +20,8 @@


class BaseExchangeBackend:
name = None
url = None
name: Optional[str] = None
url: Optional[str] = None

def get_rates(self, **kwargs):
"""
Expand Down
3 changes: 2 additions & 1 deletion djmoney/contrib/exchange/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Generated by Django 2.0.4 on 2018-04-03 08:42
from typing import List, Tuple

import django.db.models.deletion
from django.db import migrations, models
Expand All @@ -10,7 +11,7 @@ class Migration(migrations.Migration):

initial = True

dependencies = []
dependencies: List[Tuple[str, str]] = []

operations = [
migrations.CreateModel(
Expand Down
Empty file added djmoney/py.typed
Empty file.
3 changes: 2 additions & 1 deletion djmoney/settings.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import operator
from types import MappingProxyType
from typing import Optional

from django.conf import settings

Expand All @@ -8,7 +9,7 @@

# The default currency, you can define this in your project's settings module
# This has to be a currency object imported from moneyed
DEFAULT_CURRENCY: Currency = getattr(settings, "DEFAULT_CURRENCY", None)
DEFAULT_CURRENCY: Optional[Currency] = getattr(settings, "DEFAULT_CURRENCY", None)


# The default currency choices, you can define this in your project's
Expand Down
11 changes: 11 additions & 0 deletions mypy.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[mypy]
python_version = 3.7
ignore_missing_imports = True
plugins = mypy_django_plugin.main
exclude = (?x)(
docs/.+
| .github/.+
)

[mypy.plugins.django-stubs]
django_settings_module = "djmoney.settings"
2 changes: 2 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ def run_tests(self):
"pytest-pythonpath",
"pytest-cov",
"mixer",
"mypy",
"django-stubs",
]

extras_requirements = {
Expand Down
2 changes: 1 addition & 1 deletion tests/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
USE_L10N = True


moneyed.add_currency("USDT", "000", "Tether", None)
moneyed.add_currency(code="USDT", numeric="000", name="Tether")

OPEN_EXCHANGE_RATES_APP_ID = "test"
FIXER_ACCESS_KEY = "test"
2 changes: 1 addition & 1 deletion tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -512,7 +512,7 @@ class TestFExpressions:
(Money(50, "USD") * F("integer"), Money(100, "USD")),
(F("integer") * Money(50, "USD"), Money(100, "USD")),
(Money(50, "USD") / F("integer"), Money(25, "USD")),
(Money(51, "USD") % F("integer"), Money(1, "USD")),
(Money(51, "USD") % F("integer"), Money(1, "USD")), # type: ignore
(F("money") / 2, Money(50, "USD")),
(F("money") % 98, Money(2, "USD")),
(F("money") / F("integer"), Money(50, "USD")),
Expand Down

0 comments on commit d6122a6

Please sign in to comment.