From b1dc8d0469dbc36a259b6bfc2436a75bde67467f Mon Sep 17 00:00:00 2001 From: David Michaels Date: Wed, 31 Jul 2024 16:52:29 -0400 Subject: [PATCH] Change to structured_data.py to NOT silently convert a string representing a floating point number to an integer. --- CHANGELOG.rst | 2 ++ dcicutils/misc_utils.py | 11 ++++++----- dcicutils/structured_data.py | 2 +- pyproject.toml | 2 +- 4 files changed, 10 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 9cd232e95..a0b2d83bd 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -12,6 +12,8 @@ Change Log * Changed from typing_extensions import Literal to import from typing; odd Python 3.12 issue but only in GitHub Actions (observed for submitr). * Added tomli dependency in pyproject.toml (came up in submitr GA for Pyhthon 3.12). +* Change to structured_data.py to NOT silently convert a string representing a floating + point number to an integer. 8.13.3 diff --git a/dcicutils/misc_utils.py b/dcicutils/misc_utils.py index e830e55e5..0073923dd 100644 --- a/dcicutils/misc_utils.py +++ b/dcicutils/misc_utils.py @@ -984,14 +984,15 @@ def str_to_bool(x: Optional[str]) -> Optional[bool]: raise ValueError(f"An argument to str_or_bool must be a string or None: {x!r}") -def to_integer(value: str, fallback: Optional[Any] = None) -> Optional[Any]: +def to_integer(value: str, fallback: Optional[Any] = None, strict: bool = False) -> Optional[Any]: try: return int(value) except Exception: - try: - return int(float(value)) - except Exception: - pass + if strict is not True: + try: + return int(float(value)) + except Exception: + pass return fallback diff --git a/dcicutils/structured_data.py b/dcicutils/structured_data.py index 2d058a588..7489b67e1 100644 --- a/dcicutils/structured_data.py +++ b/dcicutils/structured_data.py @@ -706,7 +706,7 @@ def map_enum(value: str, enum_specifiers: dict, src: Optional[str]) -> Any: def _map_function_integer(self, typeinfo: dict) -> Callable: def map_integer(value: str, src: Optional[str]) -> Any: - return to_integer(value, value) + return to_integer(value, value, strict=True) return map_integer def _map_function_number(self, typeinfo: dict) -> Callable: diff --git a/pyproject.toml b/pyproject.toml index b9134be25..1ab54ce1e 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "dcicutils" -version = "8.13.3.1b10" # TODO: To become 8.13.4 +version = "8.13.3.1b11" # TODO: To become 8.13.4 description = "Utility package for interacting with the 4DN Data Portal and other 4DN resources" authors = ["4DN-DCIC Team "] license = "MIT"