New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: replace strtobool for local function #128
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import pytest | ||
from util import strtobool | ||
|
||
|
||
def test_true_values(): | ||
true_list = ["y", "yes", "t", "true", "on", "1"] | ||
for item in true_list: | ||
assert strtobool(item) == 1 | ||
|
||
|
||
def test_false_values(): | ||
false_list = ["n", "no", "f", "false", "off", "0"] | ||
for item in false_list: | ||
assert strtobool(item) == 0 | ||
|
||
|
||
def test_invalid_value_text(): | ||
invalid_list = ["Invalid_Value_1", "1nv4l1d_V4lu3_2", "Invalid_Value_3"] | ||
for value in invalid_list: | ||
with pytest.raises(ValueError, match="invalid truth value '%s'".format(value)): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Please check pytest's parametrize |
||
strtobool(value) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
def strtobool(value): | ||
value = value.lower() | ||
if value in ["y", "yes", "t", "true", "on", "1"]: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These comparisons are O(N) and always recreate the list. Make the list definition global and use a set instead of a list. |
||
result = True | ||
elif value in ["n", "no", "f", "false", "off", "0"]: | ||
result = False | ||
else: | ||
raise ValueError("invalid truth value '%s'".format(value)) | ||
return result |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
utils
is a symptom of lack of a proper abstraction. Please, do not use it. Keep thestrtobool
function on the main filedecouple.py
since it's not used anywhere else.