Skip to content

Commit

Permalink
Change to structured_data.py to NOT silently convert a string represe…
Browse files Browse the repository at this point in the history
…nting a floating point number to an integer.
  • Loading branch information
dmichaels-harvard committed Jul 31, 2024
1 parent 1ce283a commit b1dc8d0
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 7 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 6 additions & 5 deletions dcicutils/misc_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
2 changes: 1 addition & 1 deletion dcicutils/structured_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>"]
license = "MIT"
Expand Down

0 comments on commit b1dc8d0

Please sign in to comment.