Skip to content

Commit 63a8247

Browse files
stephenfinp1c2u
authored andcommitted
Remove use of distutils
Distutils is going away in Python 3.12. Running tests on a project that uses openapi-core with Python 3.10 yields the following warning: .../openapi_core/unmarshalling/schemas/util.py:5: DeprecationWarning: The distutils package is deprecated and slated for removal in Python 3.12. Use setuptools or check PEP 632 for potential alternatives from distutils.util import strtobool The offending API is really simple. Just vendor the code. Signed-off-by: Stephen Finucane <[email protected]>
1 parent 1627889 commit 63a8247

File tree

7 files changed

+38
-19
lines changed

7 files changed

+38
-19
lines changed

openapi_core/casting/schemas/factories.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from openapi_core.casting.schemas.casters import ArrayCaster
22
from openapi_core.casting.schemas.casters import CallableSchemaCaster
33
from openapi_core.casting.schemas.casters import DummyCaster
4-
from openapi_core.casting.schemas.util import forcebool
4+
from openapi_core.util import forcebool
55

66

77
class SchemaCastersFactory:

openapi_core/casting/schemas/util.py

-9
This file was deleted.

openapi_core/unmarshalling/schemas/unmarshallers.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@
2121
from openapi_core.unmarshalling.schemas.exceptions import UnmarshalError
2222
from openapi_core.unmarshalling.schemas.exceptions import ValidateError
2323
from openapi_core.unmarshalling.schemas.formatters import Formatter
24-
from openapi_core.unmarshalling.schemas.util import forcebool
2524
from openapi_core.unmarshalling.schemas.util import format_byte
2625
from openapi_core.unmarshalling.schemas.util import format_date
2726
from openapi_core.unmarshalling.schemas.util import format_number
2827
from openapi_core.unmarshalling.schemas.util import format_uuid
28+
from openapi_core.util import forcebool
2929

3030
log = logging.getLogger(__name__)
3131

openapi_core/unmarshalling/schemas/util.py

-8
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,12 @@
22
import datetime
33
from base64 import b64decode
44
from copy import copy
5-
from distutils.util import strtobool
65
from functools import lru_cache
76
from uuid import UUID
87

98
from openapi_schema_validator import oas30_format_checker
109

1110

12-
def forcebool(val):
13-
if isinstance(val, str):
14-
val = strtobool(val)
15-
16-
return bool(val)
17-
18-
1911
def format_date(value):
2012
return datetime.datetime.strptime(value, "%Y-%m-%d").date()
2113

openapi_core/util.py

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
"""OpenAPI core util module"""
2+
3+
4+
def forcebool(val):
5+
if isinstance(val, str):
6+
val = val.lower()
7+
if val in ("y", "yes", "t", "true", "on", "1"):
8+
return True
9+
elif val in ("n", "no", "f", "false", "off", "0"):
10+
return False
11+
else:
12+
raise ValueError(f"invalid truth value {val!r}")
13+
14+
return bool(val)

tests/unit/test_util.py

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import pytest
2+
3+
from openapi_core.util import forcebool
4+
5+
6+
class TestForcebool:
7+
@pytest.mark.parametrize("val", ["y", "yes", "t", "true", "on", "1", True])
8+
def test_true(self, val):
9+
result = forcebool(val)
10+
assert result is True
11+
12+
@pytest.mark.parametrize(
13+
"val", ["n", "no", "f", "false", "off", "0", False]
14+
)
15+
def test_false(self, val):
16+
result = forcebool(val)
17+
assert result is False
18+
19+
@pytest.mark.parametrize("val", ["random", "idontknow", ""])
20+
def test_value_error(self, val):
21+
with pytest.raises(ValueError):
22+
forcebool(val)

0 commit comments

Comments
 (0)