Compare commits

...

10 Commits

Author SHA1 Message Date
Adam Hopkins
0a84b6cfdb Merge branch 'main' into fix-2388-strtobool 2022-02-02 20:44:19 +02:00
Stephen Sadowski
2750f0f82a lint: fixed linting (again) across all files 2022-02-01 15:15:55 -06:00
Stephen Sadowski
0f5de2ef2c fix: replace strtobool inside setup.py; including from sanic.utils is a no-go 2022-02-01 15:07:12 -06:00
Stephen Sadowski
aef131af90 fix: linting errors 2022-02-01 14:33:34 -06:00
Stephen Sadowski
6e40292974 test: add strtobool validation test for deprecation replacement 2022-02-01 14:15:35 -06:00
Stephen Sadowski
db92c1fedd fix: update sanic.utils.str_to_bool to match deprecated strtobool, changed setup.py to use sanic.utils.str_to_bool 2022-02-01 14:11:52 -06:00
Stephen Sadowski
bbffdcbd68 fix: added message when ValueError is raised for strtobool replacement 2022-01-24 06:52:36 -06:00
Stephen Sadowski
768d0e09b5 fix: removed distutils dependency in setup.py per PEP 632 2022-01-23 19:57:30 -06:00
Stephen Sadowski
9d04f24298 fix: E501 line too long. 2022-01-23 18:48:49 -06:00
Stephen Sadowski
6f18c82879 fix: replace distutils.strtobool() with locally implemented version per PEP 632 2022-01-23 09:43:14 -06:00
5 changed files with 60 additions and 30 deletions

View File

@@ -6,5 +6,5 @@ data = ""
for i in range(1, 250000):
data += str(i)
r = requests.post('http://0.0.0.0:8000/stream', data=data)
r = requests.post("http://0.0.0.0:8000/stream", data=data)
print(r.text)

View File

@@ -1,12 +1,26 @@
import asyncio
from distutils.util import strtobool
from os import getenv
from sanic.compat import OS_IS_WINDOWS
from sanic.log import error_logger
def strtobool(query: str) -> bool:
"""
reimplement strtobool per PEP 632 and python 3.12 deprecation
True values are y, yes, t, true, on and 1; false values are n, no, f,
false, off and 0. Raises ValueError if val is anything else.
"""
if query.lower() in ["y", "yes", "t", "true", "on", "1"]:
return True
elif query.lower() in ["n", "no", "f", "false", "off", "0"]:
return False
else:
raise ValueError(f"String value {query} cannot be converted to bool")
def try_use_uvloop() -> None:
"""
Use uvloop instead of the default asyncio loop.

View File

@@ -11,35 +11,18 @@ from sanic.helpers import import_string
def str_to_bool(val: str) -> bool:
"""Takes string and tries to turn it into bool as human would do.
"""
reimplement strtobool per PEP 632 and python 3.12 deprecation
If val is in case insensitive (
"y", "yes", "yep", "yup", "t",
"true", "on", "enable", "enabled", "1"
) returns True.
If val is in case insensitive (
"n", "no", "f", "false", "off", "disable", "disabled", "0"
) returns False.
Else Raise ValueError."""
val = val.lower()
if val in {
"y",
"yes",
"yep",
"yup",
"t",
"true",
"on",
"enable",
"enabled",
"1",
}:
True values are y, yes, t, true, on and 1; false values are n, no, f,
false, off and 0. Raises ValueError if val is anything else.
"""
if val.lower() in ["y", "yes", "t", "true", "on", "1"]:
return True
elif val in {"n", "no", "f", "false", "off", "disable", "disabled", "0"}:
elif val.lower() in ["n", "no", "f", "false", "off", "0"]:
return False
else:
raise ValueError(f"Invalid truth value {val}")
raise ValueError(f"String value {val} cannot be converted to bool")
def load_module_from_file_location(

View File

@@ -6,8 +6,6 @@ import os
import re
import sys
from distutils.util import strtobool
from setuptools import find_packages, setup
from setuptools.command.test import test as TestCommand
@@ -61,7 +59,7 @@ setup_kwargs = {
"Build fast. Run fast."
),
"long_description": long_description,
"packages": find_packages(),
"packages": find_packages(include=[]),
"package_data": {"sanic": ["py.typed"]},
"platforms": "any",
"python_requires": ">=3.7",
@@ -132,6 +130,23 @@ dev_require = tests_require + [
all_require = list(set(dev_require + docs_require))
# trying to self-refernce this from within sanic prior to install is
# problematic
def strtobool(val: str) -> bool:
"""
reimplement strtobool per PEP 632 and python 3.12 deprecation
True values are y, yes, t, true, on and 1; false values are n, no, f,
false, off and 0. Raises ValueError if val is anything else.
"""
if val.lower() in ["y", "yes", "t", "true", "on", "1"]:
return True
elif val.lower() in ["n", "no", "f", "false", "off", "0"]:
return False
else:
raise ValueError(f"String value {val} cannot be converted to bool")
if strtobool(os.environ.get("SANIC_NO_UJSON", "no")):
print("Installing without uJSON")
requirements.remove(ujson)

View File

@@ -6,6 +6,7 @@ import pytest
from sanic.exceptions import LoadFileException
from sanic.utils import load_module_from_file_location
from sanic.utils import str_to_bool as strtobool
@pytest.mark.parametrize(
@@ -48,3 +49,20 @@ def test_load_module_from_file_location_using_env():
module = load_module_from_file_location(location)
assert isinstance(module, ModuleType)
@pytest.mark.parametrize(
"valid,values",
(
(True, ["y", "yes", "t", "true", "on", "1", "Y", "yEs", "True"]),
(False, ["n", "no", "f", "false", "off", "0", "N", "No", "False"]),
(None, ["yyy", "foo"]),
),
)
def test_strtobool(valid, values):
for value in values:
if valid is None:
with pytest.raises(ValueError):
strtobool(value)
else:
assert strtobool(value) is valid