From b4dcd50d6e48a5afd1191fc7b1c8a0444fc7f126 Mon Sep 17 00:00:00 2001 From: mtauha <111292199+mtauha@users.noreply.github.com> Date: Fri, 13 Nov 2026 20:20:07 +0500 Subject: [PATCH] feat: expose badge and ex_flag; upgrade to psxdata 1.5.0; release 0.8.0 psxdata 1.5.0 returns PSX's ticker badge beside the ticker. Add badge and ex_flag to ScreenerRow, QuoteData and IndexConstituentRow (the index router now maps nullable strings to null). The proxy cache test seeds a 1.5.0-shaped snapshot, since older ones are refetched. --- CHANGELOG.md | 12 ++++++++++ api/__init__.py | 2 +- api/requirements.txt | 2 +- api/routers/indices.py | 5 ++++- api/schemas.py | 45 ++++++++++++++++++++++++++++++++++++++ pyproject.toml | 2 +- tests/api/test_indices.py | 18 +++++++++++++++ tests/api/test_proxy.py | 3 ++- tests/api/test_screener.py | 13 +++++++++++ tests/api/test_stocks.py | 10 +++++++++ 10 files changed, 107 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 07a5d6c..6b8bfb9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,18 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm --- +## [0.8.0] — 2026-11-13 + +### Added + +- `badge` and `ex_flag` on `/screener` rows, `/stocks/{symbol}/quote` and `/indices/{name}` constituents (from psxdata 1.5.0). `ex_flag` is the corporate-action badge PSX shows next to a ticker (`XD` ex-dividend, `XB` ex-bonus, `XR` ex-rights), or `null`; `badge` is any badge (e.g. `NC`). Tickers stay plain. + +### Changed + +- Upgrade to psxdata 1.5.0 (GitHub release archive). + +--- + ## [0.7.1] — 2026-11-13 ### Fixed diff --git a/api/__init__.py b/api/__init__.py index a5f830a..777f190 100644 --- a/api/__init__.py +++ b/api/__init__.py @@ -1 +1 @@ -__version__ = "0.7.1" +__version__ = "0.8.0" diff --git a/api/requirements.txt b/api/requirements.txt index d513e02..371427b 100644 --- a/api/requirements.txt +++ b/api/requirements.txt @@ -1,5 +1,5 @@ # API layer dependencies — installs psxdata from PyPI + FastAPI stack -psxdata @ https://github.com/psxdata/psxdata/archive/refs/tags/v1.4.0.zip # TODO: back to psxdata==1.4.0 once on PyPI +psxdata @ https://github.com/psxdata/psxdata/archive/refs/tags/v1.5.0.zip # TODO: back to psxdata==1.5.0 once on PyPI fastapi[standard]>=0.142.0,<0.143 opentelemetry-sdk>=1.45,<1.46 uvicorn[standard]~=0.23 diff --git a/api/routers/indices.py b/api/routers/indices.py index dc5596c..1791271 100644 --- a/api/routers/indices.py +++ b/api/routers/indices.py @@ -46,7 +46,8 @@ def get_index( rows: list[IndexConstituentRow] = [] if not df.empty: - df = df.where(pd.notna(df), other=None) + # object dtype first so nullable string columns (badge, ex_flag) give None + df = df.astype(object).where(pd.notna(df), other=None) for record in df.to_dict("records"): rows.append( IndexConstituentRow( @@ -57,6 +58,8 @@ def get_index( market_cap_m=record.get("market_cap_m", 0.0), freefloat_m=record.get("freefloat_m"), shares_m=record.get("shares_m"), + badge=record.get("badge"), + ex_flag=record.get("ex_flag"), ) ) return IndexConstituentResponse( diff --git a/api/schemas.py b/api/schemas.py index 558bfec..a0c5389 100644 --- a/api/schemas.py +++ b/api/schemas.py @@ -100,6 +100,21 @@ class QuoteData(BaseModel): default=None, description="Sector name for sector_code, e.g. \"FERTILIZER\"; null if unknown.", ) + badge: str | None = Field( + default=None, + description=( + "Badge PSX shows next to the ticker (XD, XB, XR, NC, ...; several joined with +); " + "null if none." + ), + ) + ex_flag: str | None = Field( + default=None, + description=( + "Corporate-action badge only: XD ex-dividend, XB ex-bonus, XR ex-rights; " + "null otherwise. " + "A stock that goes ex drops by about the dividend or entitlement without a real loss." + ), + ) class QuoteResponse(BaseModel): @@ -133,6 +148,21 @@ class IndexConstituentRow(BaseModel): market_cap_m: float freefloat_m: float | None = None shares_m: float | None = None + badge: str | None = Field( + default=None, + description=( + "Badge PSX shows next to the ticker (XD, XB, XR, NC, ...; several joined with +); " + "null if none." + ), + ) + ex_flag: str | None = Field( + default=None, + description=( + "Corporate-action badge only: XD ex-dividend, XB ex-bonus, XR ex-rights; " + "null otherwise. " + "A stock that goes ex drops by about the dividend or entitlement without a real loss." + ), + ) class IndexConstituentResponse(BaseModel): @@ -191,6 +221,21 @@ class ScreenerRow(BaseModel): default=None, description="Sector name for sector_code, e.g. \"FERTILIZER\"; null if unknown.", ) + badge: str | None = Field( + default=None, + description=( + "Badge PSX shows next to the ticker (XD, XB, XR, NC, ...; several joined with +); " + "null if none." + ), + ) + ex_flag: str | None = Field( + default=None, + description=( + "Corporate-action badge only: XD ex-dividend, XB ex-bonus, XR ex-rights; " + "null otherwise. " + "A stock that goes ex drops by about the dividend or entitlement without a real loss." + ), + ) class ScreenerResponse(BaseModel): diff --git a/pyproject.toml b/pyproject.toml index 3c186a1..f61d080 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -10,7 +10,7 @@ readme = "README.md" requires-python = ">=3.11" license = {file = "LICENSE.md"} dependencies = [ - "psxdata[socks] @ https://github.com/psxdata/psxdata/archive/refs/tags/v1.4.0.zip", # TODO: back to ==1.4.0 once on PyPI + "psxdata[socks] @ https://github.com/psxdata/psxdata/archive/refs/tags/v1.5.0.zip", # TODO: back to ==1.5.0 once on PyPI "fastapi[standard]>=0.142.0,<0.143", "opentelemetry-sdk>=1.45,<1.46", "uvicorn[standard]>=0.23", diff --git a/tests/api/test_indices.py b/tests/api/test_indices.py index 8b9b855..e1b04cd 100644 --- a/tests/api/test_indices.py +++ b/tests/api/test_indices.py @@ -81,3 +81,21 @@ def test_get_index_psx_unavailable_returns_503(client: TestClient) -> None: resp = client.get("/indices/KSE100") assert resp.status_code == 503 assert resp.json()["error"]["code"] == "psx_unavailable" + + +def test_index_constituents_include_ex_flag(client: TestClient) -> None: + """badge/ex_flag (StringDtype from psxdata >= 1.5.0) pass through; becomes null.""" + df = pd.DataFrame({ + "symbol": ["ABL", "LUCK"], + "current_index": [1.0, 1.0], + "idx_weight": [1.0, 1.0], + "idx_point": [1.0, 1.0], + "market_cap_m": [1.0, 1.0], + "badge": pd.array(["XD", pd.NA], dtype="string"), + "ex_flag": pd.array(["XD", pd.NA], dtype="string"), + }) + with patch("psxdata.indices", return_value=df): + resp = client.get("/indices/KSE100") + rows = {r["symbol"]: r for r in resp.json()["data"]} + assert rows["ABL"]["ex_flag"] == "XD" and rows["ABL"]["badge"] == "XD" + assert rows["LUCK"]["ex_flag"] is None and rows["LUCK"]["badge"] is None diff --git a/tests/api/test_proxy.py b/tests/api/test_proxy.py index 6bf5699..3e3b61c 100644 --- a/tests/api/test_proxy.py +++ b/tests/api/test_proxy.py @@ -261,7 +261,8 @@ def test_hit_is_served_without_contacting_proxy(self, client, make_passthrough): def test_hit_on_data_cached_by_another_caller(self, client, make_passthrough, tmp_path): make_passthrough(resolver=refuse_network, connector=refuse_network) PSXClient(cache_dir=str(tmp_path / "cache"))._cache.set( - "screener_snapshot", _screener_df().assign(sector_code="0007", sector_name="X") + "screener_snapshot", + _screener_df().assign(sector_code="0007", sector_name="X", badge=None, ex_flag=None), ) with fake_psx(ScreenerScraper, _screener_df()) as reached: resp = _proxied(client, "/stocks/ENGRO/quote") diff --git a/tests/api/test_screener.py b/tests/api/test_screener.py index 2c007a7..fe3eaa2 100644 --- a/tests/api/test_screener.py +++ b/tests/api/test_screener.py @@ -93,3 +93,16 @@ def test_list_screener_includes_sector_code_and_name(client: TestClient) -> None rows = {r["symbol"]: r for r in resp.json()["data"]} assert (rows["FFC"]["sector_code"], rows["FFC"]["sector_name"]) == ("0809", "FERTILIZER") assert rows["PIAA"]["sector_code"] is None and rows["PIAA"]["sector_name"] is None + + +def test_list_screener_includes_ex_flag(client: TestClient) -> None: + df = pd.DataFrame({ + "symbol": ["ABL", "AAL"], + "price": [171.7, 1.0], + "badge": pd.array(["XD", "NC"], dtype="string"), + "ex_flag": pd.array(["XD", pd.NA], dtype="string"), + }) + with patch("psxdata.screener", return_value=df): + rows = {r["symbol"]: r for r in client.get("/screener").json()["data"]} + assert (rows["ABL"]["badge"], rows["ABL"]["ex_flag"]) == ("XD", "XD") + assert (rows["AAL"]["badge"], rows["AAL"]["ex_flag"]) == ("NC", None) diff --git a/tests/api/test_stocks.py b/tests/api/test_stocks.py index 7319bc5..6dbedaa 100644 --- a/tests/api/test_stocks.py +++ b/tests/api/test_stocks.py @@ -108,6 +108,16 @@ def test_quote_includes_sector_code_and_name(client: TestClient) -> None: assert resp.json()["data"]["sector_name"] is None +def test_quote_includes_ex_flag(client: TestClient) -> None: + df = pd.DataFrame({ + "symbol": ["ABL"], "price": [171.7], + "badge": pd.array(["XD"], dtype="string"), "ex_flag": pd.array(["XD"], dtype="string"), + }) + with patch("psxdata.quote", return_value=df): + data = client.get("/stocks/ABL/quote").json()["data"] + assert (data["badge"], data["ex_flag"]) == ("XD", "XD") + + def test_quote_returns_404_for_unknown_symbol(client: TestClient) -> None: screener = pd.DataFrame({"symbol": ["ENGRO", "LUCKXD"], "price": [481.99, 1100.0]}) with (