Skip to content

Commit

Permalink
lint: Fix use-implicit-booleaness-not-comparison-to-zero
Browse files Browse the repository at this point in the history
Reported by Pylint 3:
> 0 is considered false in a boolean context. Following this check
  blindly in weakly typed code base can create hard to debug issues. If
  the value can be something else that is falsey but not an int (for
  example ``None``, an empty string, or an empty sequence) the code will
  not be equivalent.

Signed-off-by: Stanislav Levin <[email protected]>
  • Loading branch information
stanislavlevin committed Nov 2, 2023
1 parent 52c3565 commit 3543413
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 4 deletions.
4 changes: 3 additions & 1 deletion tests/unit/test_build/test_backend_caller.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ def test_help():
args=[sys.executable, "-m", BACKEND_CALLER_MOD, "--help"],
capture_output=True,
)
# pylint: disable-next=use-implicit-booleaness-not-comparison-to-zero
assert result.returncode == 0
assert result.stdout.rstrip().startswith(b"usage: backend_caller.py ")
assert result.stderr == b""
Expand All @@ -135,7 +136,7 @@ def test_invalid_hook_choice():
", ".join([f"{x!r}" for x in backend_caller.SUPPORTED_HOOKS]),
)

assert result.returncode != 0
assert result.returncode
assert expected_err_msg.encode("utf-8") in result.stderr
assert result.stdout == b""

Expand Down Expand Up @@ -220,6 +221,7 @@ def test_logging_destination(level, destination):
)
cmd = [sys.executable, "-c", code]
result = subprocess.run(args=cmd, capture_output=True)
# pylint: disable-next=use-implicit-booleaness-not-comparison-to-zero
assert result.returncode == 0
if destination == "stderr":
log_out = result.stderr
Expand Down
12 changes: 10 additions & 2 deletions tests/unit/test_build/test_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,11 @@ def test_existent_outdir(mock_build, pyproject):
build_wheel(pyproject_path, outdir=outdir)


@pytest.mark.skipif(os.geteuid() == 0, reason="Requires unprivileged user")
@pytest.mark.skipif(
# pylint: disable-next=use-implicit-booleaness-not-comparison-to-zero
os.geteuid() == 0,
reason="Requires unprivileged user",
)
def test_uncreatable_outdir(mock_build, pyproject):
"""Check error if outdir is uncreatable(e.g. not enough permissions)"""
pyproject_path = pyproject()
Expand Down Expand Up @@ -375,7 +379,11 @@ def test_metadata_outdir_resolved(pyproject_metadata):
assert expected_outdir.exists()


@pytest.mark.skipif(os.geteuid() == 0, reason="Requires unprivileged user")
@pytest.mark.skipif(
# pylint: disable-next=use-implicit-booleaness-not-comparison-to-zero
os.geteuid() == 0,
reason="Requires unprivileged user",
)
def test_metadata_uncreatable_outdir(pyproject_metadata):
"""Check error if outdir is uncreatable(e.g. not enough permissions)"""
pyproject_path = pyproject_metadata()
Expand Down
8 changes: 7 additions & 1 deletion tests/unit/test_install/test_installer.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,11 @@ def test_bad_wheel(tmpdir, destdir):
install_wheel(bad_whl, destdir=destdir)


@pytest.mark.skipif(os.geteuid() == 0, reason="Requires unprivileged user")
@pytest.mark.skipif(
# pylint: disable-next=use-implicit-booleaness-not-comparison-to-zero
os.geteuid() == 0,
reason="Requires unprivileged user",
)
def test_unaccessible_destdir(wheel):
with pytest.raises(ValueError, match="Unable to create path for destdir"):
install_wheel(wheel(), destdir=Path("/unaccessible/dest/dir"))
Expand Down Expand Up @@ -521,6 +525,7 @@ def test_data_scripts(
assert script.read_text() == expected_content

result = subprocess.run([script], capture_output=True)
# pylint: disable-next=use-implicit-booleaness-not-comparison-to-zero
assert result.returncode == 0
assert result.stdout == b"Hello, World!\n"
assert result.stderr == b""
Expand Down Expand Up @@ -566,6 +571,7 @@ def test_entry_points_scripts(
str(dest_wheel.sitedir) + os.pathsep + new_env.get("PYTHONPATH", "")
)
result = subprocess.run([script], capture_output=True, env=new_env)
# pylint: disable-next=use-implicit-booleaness-not-comparison-to-zero
assert result.returncode == 0
assert result.stdout == b"Hello, World!\n"
assert result.stderr == b""
3 changes: 3 additions & 0 deletions tests/unit/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ def test_version():
args=[sys.executable, "-m", "pyproject_installer", "--version"],
capture_output=True,
)
# pylint: disable-next=use-implicit-booleaness-not-comparison-to-zero
assert result.returncode == 0
assert result.stdout.rstrip().decode("utf-8") == project_version
assert result.stderr == b""
Expand All @@ -62,6 +63,7 @@ def test_help():
args=[sys.executable, "-m", "pyproject_installer", "--help"],
capture_output=True,
)
# pylint: disable-next=use-implicit-booleaness-not-comparison-to-zero
assert result.returncode == 0
assert result.stdout.rstrip().startswith(
b"usage: python -m pyproject_installer "
Expand Down Expand Up @@ -137,6 +139,7 @@ def test_logging_destination(level, destination):
)
cmd = [sys.executable, "-c", code]
result = subprocess.run(args=cmd, capture_output=True)
# pylint: disable-next=use-implicit-booleaness-not-comparison-to-zero
assert result.returncode == 0
if destination == "stderr":
log_out = result.stderr
Expand Down
1 change: 1 addition & 0 deletions tests/unit/test_run/test_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,7 @@ def test_env_console_script(
command=[command],
capture_output=True,
)
# pylint: disable-next=use-implicit-booleaness-not-comparison-to-zero
assert res.returncode == 0
expected_out = f"Hello, World! ({command})"
assert res.stdout.strip().decode("utf-8") == expected_out
Expand Down

0 comments on commit 3543413

Please sign in to comment.