Skip to content

Commit

Permalink
Lift Python version upper limit
Browse files Browse the repository at this point in the history
  • Loading branch information
soininen committed Apr 2, 2024
1 parent a427173 commit 25a119d
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 10 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/run_unit_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
strategy:
matrix:
os: [ubuntu-22.04, windows-latest]
python-version: [3.8, 3.9, "3.10", 3.11]
python-version: [3.8, 3.9, "3.10", 3.11, 3.12]
steps:
- uses: actions/checkout@v4
with:
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ classifiers = [
"License :: OSI Approved :: GNU Lesser General Public License v3 (LGPLv3)",
"Operating System :: OS Independent",
]
requires-python = ">=3.8.1, <3.12"
requires-python = ">=3.8.1"
dependencies = [
# v1.4 does not pass tests
"sqlalchemy >=1.3, <1.4",
Expand Down
24 changes: 24 additions & 0 deletions spinedb_api/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -886,3 +886,27 @@ def group_consecutive(list_of_numbers):
for _k, g in groupby(enumerate(sorted(list_of_numbers)), lambda x: x[0] - x[1]):
group = list(map(itemgetter(1), g))
yield group[0], group[-1]


_TRUTHS = {s.casefold() for s in ("yes", "true", "y", "t", "1")}
_FALSES = {s.casefold() for s in ("no", "false", "n", "f", "0")}


def string_to_bool(string):
"""Converts string to boolean.
Recognizes "yes", "true", "y", "t" and "1" as True, "no", "false", "n", "f" and "0" as False.
Case insensitive.
Raises Value error if value is not recognized.
Args:
string (str): string to convert
Returns:
bool: True or False
"""
if string.casefold() in _TRUTHS:
return True
if string.casefold() in _FALSES:
return False
raise ValueError(string)
5 changes: 3 additions & 2 deletions spinedb_api/import_mapping/import_mapping.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@
######################################################################################################################
""" Contains import mappings for database items such as entities, entity classes and parameter values. """

from distutils.util import strtobool
from enum import auto, Enum, unique

from spinedb_api.helpers import string_to_bool
from spinedb_api.mapping import Mapping, Position, unflatten, is_pivoted
from spinedb_api.exception import InvalidMappingComponent

Expand Down Expand Up @@ -808,7 +809,7 @@ class ScenarioActiveFlagMapping(ImportMapping):

def _import_row(self, source_data, state, mapped_data):
scenario = state[ImportKey.SCENARIO_NAME]
active = bool(strtobool(str(source_data)))
active = string_to_bool(str(source_data))
mapped_data.setdefault("scenarios", set()).add((scenario, active))


Expand Down
10 changes: 4 additions & 6 deletions spinedb_api/import_mapping/type_conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,11 @@
# this program. If not, see <http://www.gnu.org/licenses/>.
######################################################################################################################

"""
Type conversion functions.
"""
""" Type conversion functions. """

import re
from distutils.util import strtobool

from spinedb_api.helpers import string_to_bool
from spinedb_api.parameter_value import DateTime, Duration, ParameterValueFormatError


Expand Down Expand Up @@ -81,7 +79,7 @@ class BooleanConvertSpec(ConvertSpec):
RETURN_TYPE = bool

def __call__(self, value):
return self.RETURN_TYPE(strtobool(str(value)))
return self.RETURN_TYPE(string_to_bool(str(value)))


class IntegerSequenceDateTimeConvertSpec(ConvertSpec):
Expand Down
22 changes: 22 additions & 0 deletions tests/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
name_from_dimensions,
name_from_elements,
remove_credentials_from_url,
string_to_bool,
)


Expand Down Expand Up @@ -75,5 +76,26 @@ def test_returns_latest_version(self):
self.assertEqual(get_head_alembic_version(), "8b0eff478bcb")


class TestStringToBool(unittest.TestCase):
def test_truths(self):
self.assertTrue(string_to_bool("yes"))
self.assertTrue(string_to_bool("YES"))
self.assertTrue(string_to_bool("y"))
self.assertTrue(string_to_bool("true"))
self.assertTrue(string_to_bool("t"))
self.assertTrue(string_to_bool("1"))

def test_falses(self):
self.assertFalse(string_to_bool("NO"))
self.assertFalse(string_to_bool("no"))
self.assertFalse(string_to_bool("n"))
self.assertFalse(string_to_bool("false"))
self.assertFalse(string_to_bool("f"))
self.assertFalse(string_to_bool("0"))

def test_raises_value_error(self):
self.assertRaises(ValueError, string_to_bool, "no truth in this")


if __name__ == "__main__":
unittest.main()

0 comments on commit 25a119d

Please sign in to comment.