Skip to content

Commit a0843cd

Browse files
authored
Merge pull request #29 from quantmind/ls-mypy
mypy
2 parents 43f9c3b + 739def9 commit a0843cd

File tree

13 files changed

+494
-641
lines changed

13 files changed

+494
-641
lines changed

.github/workflows/build.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ jobs:
1818
PYPI_PASSWORD: ${{ secrets.PYPI_PASSWORD }}
1919
strategy:
2020
matrix:
21-
python-version: ["3.8", "3.9", "3.10", "3.11"]
21+
python-version: ["3.10", "3.11"]
2222

2323
steps:
2424
- uses: actions/checkout@v2

Makefile

+8-6
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,16 @@ install: ## install packages for development
1919
@pip install -U pip poetry
2020
@poetry install
2121

22-
lint: ## run linters
23-
@poetry run dev/lint
22+
lint: ## Run linters
23+
@poetry run ./dev/lint fix
24+
25+
26+
lint-check: ## Run linters in check mode
27+
@poetry run ./dev/lint
28+
2429

2530
test: ## test with python 3.8 with coverage
2631
@poetry run pytest -x -v --cov --cov-report xml
2732

28-
codecov: ## upload code coverage
29-
@poetry run codecov --token $(CODECOV_TOKEN) --file ./build/coverage.xml
30-
31-
publish: ## release to pypi and github tag
33+
publish: ## release to pypi and github tag
3234
@poetry publish --build -u lsbardel -p $(PYPI_PASSWORD)

ccy/__init__.py

+10-11
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""Python currencies"""
22

3-
__version__ = "1.3.0"
3+
__version__ = "1.4.0"
44

55

66
from .core.country import (
@@ -21,7 +21,7 @@
2121
currencydb,
2222
dump_currency_table,
2323
)
24-
from .core.daycounter import ActActYears, alldc, getdc
24+
from .core.daycounter import alldc, getdc
2525
from .dates.converters import (
2626
date2juldate,
2727
date2timestamp,
@@ -44,7 +44,6 @@
4444
"dump_currency_table",
4545
#
4646
"getdc",
47-
"ActActYears",
4847
"alldc",
4948
#
5049
"country",
@@ -81,18 +80,18 @@ def crossover(code):
8180
return currency(code).as_cross("/")
8281

8382

84-
def all():
85-
return currencydb().keys()
83+
def all() -> tuple[str, ...]:
84+
return tuple(currencydb())
8685

8786

88-
def g7():
89-
return ["EUR", "GBP", "USD", "CAD"]
87+
def g7() -> tuple[str, ...]:
88+
return ("EUR", "GBP", "USD", "CAD")
9089

9190

92-
def g10():
93-
return g7() + ["CHF", "SEK", "JPY"]
91+
def g10() -> tuple[str, ...]:
92+
return g7() + ("CHF", "SEK", "JPY")
9493

9594

96-
def g10m():
95+
def g10m() -> tuple[str, ...]:
9796
"""modified g10 = G10 + AUD, NZD, NOK"""
98-
return g10() + ["AUD", "NZD", "NOK"]
97+
return g10() + ("AUD", "NZD", "NOK")

ccy/core/country.py

+24-13
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
#
2-
# Requires pytz 2008i or higher
3-
#
1+
from __future__ import annotations
2+
3+
from typing import Protocol
4+
45
from .currency import currencydb
56

67
# Eurozone countries (officially the euro area)
@@ -18,11 +19,17 @@ def print_eurozone():
1819
print(c)
1920

2021

21-
_countries = None
22+
_countries: dict[str, Country] = {}
2223
_country_ccys = None
2324
_country_maps = {}
2425

2526

27+
class Country(Protocol):
28+
alpha_2: str
29+
name: str
30+
alpha_3: str = ""
31+
32+
2633
class CountryError(Exception):
2734
pass
2835

@@ -39,19 +46,16 @@ def countryccy(code):
3946
return cdb.get(code, None)
4047

4148

42-
def countries():
49+
def countries() -> dict[str, Country]:
4350
"""
44-
get country dictionar from pytz and add some extra.
51+
get country dictionary from pytz and add some extra.
4552
"""
4653
global _countries
4754
if not _countries:
48-
v = {}
49-
_countries = v
5055
try:
51-
from pytz import country_names
56+
import pycountry
5257

53-
for k, n in country_names.items():
54-
v[k.upper()] = n
58+
_countries = {country.alpha_2: country for country in pycountry.countries}
5559
except Exception:
5660
pass
5761
return _countries
@@ -106,7 +110,7 @@ def set_country_map(cfrom, cto, name=None, replace=True):
106110
raise CountryError("Country %s not in database" % c)
107111

108112

109-
def set_new_country(code, ccy, name):
113+
def set_new_country(code: str, ccy: str, name: str) -> None:
110114
"""
111115
Add new country code to database
112116
"""
@@ -118,7 +122,12 @@ def set_new_country(code, ccy, name):
118122
ccy = str(ccy).upper()
119123
if ccy not in ccys:
120124
raise CountryError("Currency %s not in database" % ccy)
121-
cdb[code] = str(name)
125+
# hacky - but best way I could find
126+
cdb[code] = type(cdb["IT"])(
127+
alpha_2=code,
128+
name=name,
129+
official_name=name,
130+
) # type: ignore
122131
cccys = countryccys()
123132
cccys[code] = ccy
124133

@@ -133,4 +142,6 @@ def country_map(code):
133142

134143

135144
# Add eurozone to list of Countries
145+
set_new_country("EZ", "EUR", "Eurozone")
146+
# lagacy - to remove
136147
set_new_country("EU", "EUR", "Eurozone")

0 commit comments

Comments
 (0)