Skip to content

Commit

Permalink
Remove unnecessary util function force_text()
Browse files Browse the repository at this point in the history
With Python 2 support dropped, the str/bytes boundary is much more
explicit. It is known when a value is str or could be something else.

Coercing non-str values to str can now be handled using the builtin
str().
  • Loading branch information
jdufresne committed Jan 1, 2021
1 parent 0036708 commit fb5486b
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 23 deletions.
17 changes: 2 additions & 15 deletions piptools/utils.py
Expand Up @@ -254,17 +254,6 @@ def get_hashes_from_ireq(ireq):
return result


def force_text(s):
"""
Return a string representing `s`.
"""
if s is None:
return ""
if not isinstance(s, str):
return str(s)
return s


def get_compile_command(click_ctx):
"""
Returns a normalized compile command depending on cli context.
Expand Down Expand Up @@ -294,7 +283,7 @@ def get_compile_command(click_ctx):
# Re-add click-stripped '--' if any start with '-'
if any(val.startswith("-") and val != "-" for val in value):
right_args.append("--")
right_args.extend([shlex.quote(force_text(val)) for val in value])
right_args.extend([shlex.quote(val) for val in value])
continue

# Get the latest option name (usually it'll be a long name)
Expand Down Expand Up @@ -343,8 +332,6 @@ def get_compile_command(click_ctx):
# Instead, we try to get more legible quoting via repr:
left_args.append(f"{option_long_name}={repr(val)}")
else:
left_args.append(
f"{option_long_name}={shlex.quote(force_text(val))}"
)
left_args.append(f"{option_long_name}={shlex.quote(str(val))}")

return " ".join(["pip-compile", *sorted(left_args), *sorted(right_args)])
8 changes: 0 additions & 8 deletions tests/test_utils.py
Expand Up @@ -9,7 +9,6 @@
as_tuple,
dedup,
flat_map,
force_text,
format_requirement,
format_specifier,
get_compile_command,
Expand Down Expand Up @@ -203,13 +202,6 @@ def test_name_from_req_with_project_name(from_line):
assert name_from_req(ireq.req) == "bar"


@pytest.mark.parametrize(
("value", "expected_text"), ((None, ""), (42, "42"), ("foo", "foo"), ("bãr", "bãr"))
)
def test_force_text(value, expected_text):
assert force_text(value) == expected_text


@pytest.mark.parametrize(
("cli_args", "expected_command"),
(
Expand Down

0 comments on commit fb5486b

Please sign in to comment.