From 202b9201cf3bf9d3971a10736524c12ae3ab9e5f Mon Sep 17 00:00:00 2001 From: "Carl F. Corneil" Date: Wed, 25 Feb 2026 14:39:31 +0100 Subject: [PATCH 1/2] rounding infinites --- .pre-commit-config.yaml | 6 +++--- docs/conf.py | 1 - noxfile.py | 1 - src/statbank/auth.py | 2 +- src/statbank/statbank_logger.py | 2 +- src/statbank/uttrekk.py | 8 ++++++++ tests/test_statbank.py | 12 ++++++++++++ 7 files changed, 25 insertions(+), 7 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 35908ad..0211bc5 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -3,7 +3,7 @@ default_language_version: repos: # Pre-commit maintained utility hooks - repo: https://github.com/pre-commit/pre-commit-hooks - rev: v4.6.0 + rev: v6.0.0 hooks: - id: check-added-large-files - id: check-merge-conflict @@ -27,7 +27,7 @@ repos: # Ruff linter - repo: https://github.com/astral-sh/ruff-pre-commit - rev: v0.6.9 + rev: v0.15.2 hooks: - id: ruff args: ["--fix", "--exit-non-zero-on-fix"] @@ -35,7 +35,7 @@ repos: # Black formatter - repo: https://github.com/psf/black - rev: 24.10.0 + rev: 26.1.0 hooks: - id: black require_serial: true diff --git a/docs/conf.py b/docs/conf.py index a0443f5..a732add 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -15,7 +15,6 @@ import os import sys - sys.path.insert(0, os.path.abspath("../src")) # -- Project information ----------------------------------------------------- diff --git a/noxfile.py b/noxfile.py index 29066f2..67109dd 100644 --- a/noxfile.py +++ b/noxfile.py @@ -10,7 +10,6 @@ import nox - try: from nox_poetry import Session from nox_poetry import session diff --git a/src/statbank/auth.py b/src/statbank/auth.py index f322a23..709f497 100644 --- a/src/statbank/auth.py +++ b/src/statbank/auth.py @@ -176,7 +176,7 @@ def _build_headers(self: Self) -> dict[str, str]: } def _get_auth(self) -> requests.auth.AuthBase: - host = cast(str, self._config.endpoint_base.host) + host = cast("str", self._config.endpoint_base.host) with Netrc(self._config.netrc_path) as authfile: auth_record = authfile[host] diff --git a/src/statbank/statbank_logger.py b/src/statbank/statbank_logger.py index 4fa7278..932d679 100644 --- a/src/statbank/statbank_logger.py +++ b/src/statbank/statbank_logger.py @@ -21,7 +21,7 @@ def __init__( """Initialize the formatter with specified format strings.""" super().__init__(*args, **kwargs) - self.colors = colors if colors else {} + self.colors = colors or {} def format(self, record: logging.LogRecord) -> str: """Format the specified record as text.""" diff --git a/src/statbank/uttrekk.py b/src/statbank/uttrekk.py index 4c3ccde..c2df503 100644 --- a/src/statbank/uttrekk.py +++ b/src/statbank/uttrekk.py @@ -6,6 +6,7 @@ import json import math import sys +import warnings from decimal import ROUND_HALF_UP from decimal import Decimal from decimal import localcontext @@ -15,6 +16,7 @@ from typing import TypedDict from typing import overload +import numpy as np import pandas as pd import requests as r import requests.auth @@ -554,6 +556,12 @@ def _round( ) -> str: if pd.isna(n): result: str = "" + elif n in [np.inf, -np.inf]: + warnings.warn( + "Found Infinite values in your data, why? Remove them?", + stacklevel=1, + ) + result = "" elif round_up and decimals and (n or n == 0): with localcontext() as ctx: ctx.rounding = ROUND_HALF_UP diff --git a/tests/test_statbank.py b/tests/test_statbank.py index a5fc878..cd005d5 100644 --- a/tests/test_statbank.py +++ b/tests/test_statbank.py @@ -12,6 +12,7 @@ from unittest import mock import ipywidgets as widgets +import numpy as np import pandas as pd import pytest import requests @@ -36,6 +37,17 @@ def test_round_up_zero(): assert StatbankUttrekksBeskrivelse._round(0.0, 0) == "0" # noqa: SLF001 +def test_round_handles_inf_values(): + assert StatbankUttrekksBeskrivelse._round(np.inf, 0) == "" # noqa: SLF001 + assert StatbankUttrekksBeskrivelse._round(-np.inf, 0) == "" # noqa: SLF001 + + +@pytest.mark.parametrize("value", [np.inf, -np.inf]) +def test_round_warns_on_inf_values(value: float): + with pytest.warns(UserWarning, match="Found Infinite values in your data"): + assert StatbankUttrekksBeskrivelse._round(value, 0) == "" # noqa: SLF001 + + def fake_user(): return "tbf" From 7cc261d016c4e1f1f10519fc6eb7564bbf1f2740 Mon Sep 17 00:00:00 2001 From: "Carl F. Corneil" Date: Wed, 25 Feb 2026 14:42:02 +0100 Subject: [PATCH 2/2] bump patch version --- pyproject.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index 2045e88..1bd9ae0 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -2,7 +2,7 @@ name = "dapla-statbank-client" description = "Handles data transfer Statbank <-> Dapla for Statistics Norway" license = "MIT" -version = "1.4.0" +version = "1.4.1" readme = "README.md" authors = [{name = "Carl F. Corneil", email = "cfc@ssb.no"}, {name = "Statistics Norway"}] requires-python = ">=3.10"