Skip to content

Commit

Permalink
🔧 Make linting stricter
Browse files Browse the repository at this point in the history
  • Loading branch information
spapanik committed Dec 12, 2024
1 parent 0f15f25 commit 625280f
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 77 deletions.
26 changes: 22 additions & 4 deletions dev_scripts/update_country.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,19 @@
import json
from argparse import ArgumentParser, Namespace
from pathlib import Path
from typing import Any
from typing import TypedDict

from bs4 import BeautifulSoup, Tag


class CountryDict(TypedDict):
alpha_2_code: str
alpha_3_code: str
english_name: str
french_name: str
numeric_code: int


def clean_nbsp(text: str) -> str:
return text.replace("\u00a0", " ").strip()

Expand All @@ -24,7 +32,16 @@ def parse_args() -> Namespace:
return parser.parse_args()


def parse_country(country: Tag) -> dict[str, Any]:
def get_tag(tag: Tag, name: str, attributes: dict[str, str] | None = None) -> Tag:
attributes = attributes or {}
found = tag.find(name, attrs=attributes)
if not isinstance(found, Tag):
msg = f"Tag `{name}` not found in `{tag}`"
raise TypeError(msg)
return found


def parse_country(country: Tag) -> CountryDict:
attributes = [td.get_text() for td in country.find_all("td")]
return {
"alpha_2_code": attributes[2],
Expand All @@ -39,8 +56,9 @@ def update_country(definitions: Path) -> None:
data_path = Path("__file__").parent.joinpath("src/teritorio/_data/country.json")
soup = BeautifulSoup(definitions.read_text(), "lxml")

countries: dict[str, Any] = {}
for country in soup.find("table", {"role": "grid"}).find("tbody").find_all("tr"):
countries: dict[str, CountryDict] = {}
table = get_tag(soup, "table", {"role": "grid"})
for country in get_tag(table, "tbody").find_all("tr"):
parsed_country = parse_country(country)
countries[parsed_country["alpha_3_code"]] = parsed_country

Expand Down
32 changes: 24 additions & 8 deletions dev_scripts/update_currency.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,19 @@
import json
from argparse import ArgumentParser, Namespace
from pathlib import Path
from typing import Any
from typing import TypedDict

from bs4 import BeautifulSoup, Tag


class CurrencyDict(TypedDict):
code: str
entities: list[str]
minor_units: int | None
name: str
numeric_code: int


def parse_args() -> Namespace:
parser = ArgumentParser()
parser.add_argument(
Expand All @@ -20,32 +28,40 @@ def parse_args() -> Namespace:
return parser.parse_args()


def parse_currency(currency: Tag) -> dict[str, Any] | None:
def get_tag(tag: Tag, name: str) -> Tag:
found = tag.find(name)
if not isinstance(found, Tag):
msg = f"Tag `{name}` not found in `{tag}`"
raise TypeError(msg)
return found


def parse_currency(currency: Tag) -> CurrencyDict | None:
code = currency.find("Ccy")
if not code:
return None

minor_units_text = currency.find("CcyMnrUnts").get_text()
minor_units_text = get_tag(currency, "CcyMnrUnts").get_text()
try:
minor_units = int(minor_units_text)
except ValueError:
minor_units = None

return {
"code": code.get_text().strip(),
"entities": [currency.find("CtryNm").get_text().strip()],
"entities": [get_tag(currency, "CtryNm").get_text().strip()],
"minor_units": minor_units,
"name": currency.find("CcyNm").get_text().strip(),
"numeric_code": int(currency.find("CcyNbr").get_text()),
"name": get_tag(currency, "CcyNm").get_text().strip(),
"numeric_code": int(get_tag(currency, "CcyNbr").get_text()),
}


def update_currency(definitions: Path) -> None:
data_path = Path("__file__").parent.joinpath("src/teritorio/_data/currency.json")
soup = BeautifulSoup(definitions.read_text(), features="xml")

currencies: dict[str, Any] = {}
for currency in soup.find("CcyTbl").find_all("CcyNtry"):
currencies: dict[str, CurrencyDict] = {}
for currency in get_tag(soup, "CcyTbl").find_all("CcyNtry"):
parsed_currency = parse_currency(currency)
if parsed_currency is not None:
if parsed_currency["code"] in currencies:
Expand Down
86 changes: 23 additions & 63 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ lint = [
"black~=24.10",
"mypy~=1.13",
"ruff~=0.8",
"types-beautifulsoup4~=4.12",
]
test = [
"pytest~=8.3",
Expand All @@ -69,7 +70,11 @@ target-version = [

[tool.mypy]
check_untyped_defs = true
disallow_any_decorated = true
disallow_any_explicit = true
disallow_any_expr = false # many builtins are Any
disallow_any_generics = true
disallow_any_unimported = true
disallow_incomplete_defs = true
disallow_subclassing_any = true
disallow_untyped_calls = true
Expand All @@ -78,13 +83,18 @@ disallow_untyped_defs = true
extra_checks = true
ignore_missing_imports = true
no_implicit_reexport = true
show_column_numbers = true
show_error_codes = true
strict_equality = true
warn_return_any = true
warn_redundant_casts = true
warn_return_any = true
warn_unused_configs = true
warn_unused_ignores = true
warn_unreachable = true
warn_unused_configs = true

[[tool.mypy.overrides]]
module = "tests.*"
disallow_any_decorated = false # mock.MagicMock is Any

[tool.ruff]
src = [
Expand All @@ -94,73 +104,23 @@ target-version = "py39"

[tool.ruff.lint]
select = [
"A",
"ANN",
"ARG",
"ASYNC",
"B",
"BLE",
"C4",
"COM",
"DTZ",
"E",
"EM",
"ERA",
"EXE",
"F",
"FA",
"FBT",
"FIX",
"FLY",
"FURB",
"G",
"I",
"ICN",
"INP",
"ISC",
"LOG",
"N",
"PGH",
"PERF",
"PIE",
"PL",
"PT",
"PTH",
"PYI",
"Q",
"RET",
"RSE",
"RUF",
"S",
"SIM",
"SLF",
"SLOT",
"T10",
"TCH",
"TD",
"TID",
"TRY",
"UP",
"W",
"YTT",
"ALL",
]
ignore = [
"ANN401",
"COM812",
"E501",
"FIX002",
"PLR09",
"TD002",
"TD003",
"TRY003",
"C901", # Adding a limit to complexity is too arbitrary
"COM812", # Avoid magic trailing commas
"D10", # Not everything needs a docstring
"D203", # Prefer `no-blank-line-before-class` (D211)
"D213", # Prefer `multi-line-summary-first-line` (D212)
"E501", # Avoid clashes with black
"PLR09", # Adding a limit to complexity is too arbitrary
]

[tool.ruff.lint.per-file-ignores]
"tests/**" = [
"FBT001",
"PLR2004",
"PT011",
"S101",
"FBT001", # Test arguments are handled by pytest
"PLR2004", # Tests should contain magic number comparisons
"S101", # Pytest needs assert statements
]
"dev_scripts/**" = [
"INP001",
Expand Down
4 changes: 2 additions & 2 deletions src/teritorio/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import json
from dataclasses import dataclass
from pathlib import Path
from typing import Any, Generic, TypeVar
from typing import Generic, TypeVar

from pyutilkit.classes import Singleton

Expand Down Expand Up @@ -79,7 +79,7 @@ class Currencies(DataList[Currency], metaclass=Singleton):
_object_class = Currency


def _list_to_tuple(obj: Any) -> Any:
def _list_to_tuple(obj: object) -> object:
if isinstance(obj, list):
return tuple(obj)
if isinstance(obj, dict):
Expand Down

0 comments on commit 625280f

Please sign in to comment.