From f968cf256481bf5f4dbeb93110779d071dc286b4 Mon Sep 17 00:00:00 2001 From: Nightriff <66378309+Nightriff@users.noreply.github.com> Date: Tue, 27 May 2025 23:00:07 -0700 Subject: [PATCH 1/2] int() throws a ValueError, not a TypeError --- lib/str_to_int.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/str_to_int.py b/lib/str_to_int.py index 50d24ff..e9ebe34 100644 --- a/lib/str_to_int.py +++ b/lib/str_to_int.py @@ -24,5 +24,5 @@ def str_to_int(string, *, default=None): otherwise returns default""" try: return int(string) - except TypeError: + except ValueError: return default From b9a4ec462ebab2b5558e4293443aca02090a7e3d Mon Sep 17 00:00:00 2001 From: Nightriff <66378309+Nightriff@users.noreply.github.com> Date: Mon, 9 Jun 2025 20:48:08 -0700 Subject: [PATCH 2/2] Catch both errors and document the scenarios they're thrown --- lib/str_to_int.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/str_to_int.py b/lib/str_to_int.py index e9ebe34..80a710c 100644 --- a/lib/str_to_int.py +++ b/lib/str_to_int.py @@ -23,6 +23,8 @@ def str_to_int(string, *, default=None): """Returns the integer value of the string if the string is numeric, otherwise returns default""" try: + # TypeError is raised when the string is None. + # ValueError is raised when the string is not numeric. return int(string) - except ValueError: + except (ValueError, TypeError): return default