Skip to content

Commit c050b71

Browse files
committed
Fixes #69
Better error handling for issues with runner metadata CLOTH_NUMBER.
1 parent ce398dd commit c050b71

3 files changed

Lines changed: 21 additions & 4 deletions

File tree

‎betfair_parser/spec/betting/type_definitions.py‎

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from functools import partial
33

44
import msgspec
5+
from msgspec.structs import force_setattr
56

67
from betfair_parser.spec.betting.enums import (
78
BetDelayModel,
@@ -259,7 +260,7 @@ class RunnerMetaData(BaseMessage, frozen=True, rename="upper"):
259260
adjusted_rating: int | None = None # Race-specific ratings that reflect weights allocated in the race
260261
age: int | None = None # The age of the horse
261262
bred: _MetaCountryCode | None = None # The country in which the horse was born
262-
cloth_number: int | None = None # The number on the saddle-cloth
263+
cloth_number: int | str | None = None # The number on the saddle-cloth
263264
cloth_number_alpha: str | None = None # The number on the saddle cloth for US paired runners, e.g. "1A"
264265
colour_type: str | None = None # The colour of the horse
265266
colours_description: str | None = None # The textual description of the jockey silk
@@ -291,7 +292,6 @@ class RunnerMetaData(BaseMessage, frozen=True, rename="upper"):
291292
weight_value: float | None = None # The weight of the horse
292293

293294
def __post_init__(self):
294-
force_setattr = msgspec.structs.force_setattr
295295
cur_year = datetime.now().year
296296
if self.weight_value is not None and self.weight_value <= 0:
297297
force_setattr(self, "weight_value", None)
@@ -303,10 +303,16 @@ def __post_init__(self):
303303
force_setattr(self, "dam_year_born", None)
304304
if self.damsire_year_born is not None and not cur_year - 60 < self.damsire_year_born < cur_year:
305305
force_setattr(self, "damsire_year_born", None)
306-
if self.cloth_number is not None and not 0 < self.cloth_number < 50:
307-
force_setattr(self, "cloth_number", None)
308306
if self.age is not None and not 1 < self.age < 30:
309307
force_setattr(self, "age", None)
308+
if isinstance(self.cloth_number, str):
309+
try:
310+
cloth_number = int("".join(filter(str.isdigit, self.cloth_number)))
311+
except ValueError:
312+
cloth_number = None
313+
force_setattr(self, "cloth_number", cloth_number)
314+
if self.cloth_number is not None and not 0 < self.cloth_number < 50: # type: ignore[operator]
315+
force_setattr(self, "cloth_number", None)
310316

311317
@property
312318
def colours_url(self):

‎tests/integration/test_integration.py‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,16 @@ def test_responses(path):
7272
assert not resp.error
7373
assert resp.result
7474

75+
if isinstance(resp.result, list) and isinstance(resp.result[0], betting.MarketCatalogue):
76+
for mc in resp.result:
77+
for runner in mc.runners:
78+
metadata = runner.metadata
79+
if not metadata:
80+
continue
81+
assert isinstance(metadata.cloth_number, int)
82+
assert isinstance(metadata.cloth_number_alpha, str)
83+
assert metadata.cloth_number_alpha.startswith(str(metadata.cloth_number))
84+
7585

7686
LINE_COUNT = {
7787
"1.164917629.bz2": 298,

‎tests/resources/responses/betting/list_market_catalog_invalid_cloth_number.json‎

Lines changed: 1 addition & 0 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)