Add warning suppression for openpyxl default style in portfolio scripts and tests

This commit is contained in:
2026-06-21 13:39:56 +03:00
parent eaa9726a5f
commit 7298a992f4
4 changed files with 54 additions and 2 deletions
+5 -2
View File
@@ -22,14 +22,17 @@ _IMPL_PATH = (
def _load_impl(): def _load_impl():
module_name = __name__ if __name__ != "__main__" else "_xtb_wealthfolio_exporter_impl"
script_dir = _IMPL_PATH.parent script_dir = _IMPL_PATH.parent
if str(script_dir) not in sys.path: if str(script_dir) not in sys.path:
sys.path.insert(0, str(script_dir)) sys.path.insert(0, str(script_dir))
spec = importlib.util.spec_from_file_location(__name__, _IMPL_PATH) spec = importlib.util.spec_from_file_location(module_name, _IMPL_PATH)
if spec is None or spec.loader is None: if spec is None or spec.loader is None:
raise ImportError(f"Could not load XTB Wealthfolio implementation at {_IMPL_PATH}") raise ImportError(f"Could not load XTB Wealthfolio implementation at {_IMPL_PATH}")
module = importlib.util.module_from_spec(spec) module = importlib.util.module_from_spec(spec)
sys.modules[__name__] = module sys.modules[module_name] = module
if __name__ != "__main__":
sys.modules[__name__] = module
spec.loader.exec_module(module) spec.loader.exec_module(module)
return module return module
@@ -2,6 +2,7 @@ import argparse
import contextlib import contextlib
import io import io
import re import re
import warnings
from dataclasses import dataclass, field from dataclasses import dataclass, field
from datetime import datetime, date, timedelta from datetime import datetime, date, timedelta
from html import escape from html import escape
@@ -37,6 +38,18 @@ COST_FALLBACK_NOTES = {
), ),
} }
def suppress_openpyxl_default_style_warning() -> None:
warnings.filterwarnings(
"ignore",
message=r"Workbook contains no default style, apply openpyxl's default",
category=UserWarning,
module=r"openpyxl\.styles\.stylesheet",
)
suppress_openpyxl_default_style_warning()
# XTB "Type" values that represent trading activity (not cash transfers). # XTB "Type" values that represent trading activity (not cash transfers).
TRADE_TYPE_RE = re.compile( TRADE_TYPE_RE = re.compile(
r"stock\s*(purchase|sale|buy|sell)|\bopen\b|\bclose\b", r"stock\s*(purchase|sale|buy|sell)|\bopen\b|\bclose\b",
@@ -2,6 +2,7 @@ import argparse
import contextlib import contextlib
import io import io
import re import re
import warnings
from dataclasses import dataclass, field from dataclasses import dataclass, field
from datetime import datetime, date, timedelta from datetime import datetime, date, timedelta
from html import escape from html import escape
@@ -37,6 +38,18 @@ COST_FALLBACK_NOTES = {
), ),
} }
def suppress_openpyxl_default_style_warning() -> None:
warnings.filterwarnings(
"ignore",
message=r"Workbook contains no default style, apply openpyxl's default",
category=UserWarning,
module=r"openpyxl\.styles\.stylesheet",
)
suppress_openpyxl_default_style_warning()
# XTB "Type" values that represent trading activity (not cash transfers). # XTB "Type" values that represent trading activity (not cash transfers).
TRADE_TYPE_RE = re.compile( TRADE_TYPE_RE = re.compile(
r"stock\s*(purchase|sale|buy|sell)|\bopen\b|\bclose\b", r"stock\s*(purchase|sale|buy|sell)|\bopen\b|\bclose\b",
+23
View File
@@ -1,3 +1,5 @@
import warnings
import pandas as pd import pandas as pd
import pytest import pytest
@@ -44,6 +46,27 @@ def cash_row(type_, instrument, amount, comment="", time="2026-01-15 10:00:00"):
# Generic helpers # Generic helpers
# --------------------------------------------------------------------------- # ---------------------------------------------------------------------------
class TestHelpers: class TestHelpers:
def test_suppresses_openpyxl_default_style_warning_only(self):
with warnings.catch_warnings(record=True) as caught:
warnings.simplefilter("default")
main.suppress_openpyxl_default_style_warning()
warnings.warn_explicit(
"Workbook contains no default style, apply openpyxl's default",
UserWarning,
"stylesheet.py",
237,
module="openpyxl.styles.stylesheet",
)
warnings.warn_explicit(
"Another workbook warning",
UserWarning,
"stylesheet.py",
237,
module="openpyxl.styles.stylesheet",
)
assert [str(w.message) for w in caught] == ["Another workbook warning"]
def test_clean_columns_normalizes(self): def test_clean_columns_normalizes(self):
df = pd.DataFrame(columns=["Open Price", "Profit/Loss", " Ticker "]) df = pd.DataFrame(columns=["Open Price", "Profit/Loss", " Ticker "])
out = clean_columns(df) out = clean_columns(df)