Skip to content

Commit 766f8c4

Browse files
Apply repo-review MyPy suggestions
MY103: MyPy warn unreachable Must have `warn_unreachable` (true/false) to pass this check. There are occasionally false positives (often due to platform or Python version static checks), so it's okay to set it to false if you need to. But try it first - it can catch real bugs too. MY104: MyPy enables ignore-without-code Must have `"ignore-without-code"` in `enable_error_code = [...]`. This will force all skips in your project to include the error code, which makes them more readable, and avoids skipping something unintended. MY105: MyPy enables redundant-expr Must have `"redundant-expr"` in `enable_error_code = [...]`. This helps catch useless lines of code, like checking the same condition twice. MY106: MyPy enables truthy-bool Must have `"truthy-bool"` in `enable_error_code = []`. This catches mistakes in using a value as truthy if it cannot be falsy.
1 parent 2b92ca6 commit 766f8c4

10 files changed

+22
-18
lines changed

pyproject.toml

+4
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,10 @@ title_format = "## [{version}](https://github.com/pypa/pipx/tree/{version}) - {p
119119
issue_format = "[#{issue}](https://github.com/pypa/pipx/issues/{issue})"
120120
package = "pipx"
121121

122+
[tool.mypy]
123+
warn_unreachable = true
124+
enable_error_code = [ "ignore-without-code", "redundant-expr", "truthy-bool" ]
125+
122126
[[tool.mypy.overrides]]
123127
module = [
124128
"pycowsay.*",

src/pipx/animate.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ def print_animation(
101101
def win_cursor(visible: bool) -> None:
102102
if sys.platform != "win32": # hello mypy
103103
return
104-
ci = _CursorInfo()
104+
ci = _CursorInfo() # type: ignore[unreachable]
105105
handle = ctypes.windll.kernel32.GetStdHandle(-11)
106106
ctypes.windll.kernel32.GetConsoleCursorInfo(handle, ctypes.byref(ci))
107107
ci.visible = visible

src/pipx/commands/environment.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def environment(value: str) -> ExitCode:
3838
"PIPX_HOME_ALLOW_SPACE": str(paths.ctx.allow_spaces_in_home_path).lower(),
3939
}
4040
if value is None:
41-
print("Environment variables (set by user):")
41+
print("Environment variables (set by user):") # type: ignore[unreachable]
4242
print("")
4343
for env_variable in ENVIRONMENT_VARIABLES:
4444
env_value = os.getenv(env_variable, "")

src/pipx/commands/inject.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def inject_dep(
3232
) -> bool:
3333
logger.debug("Injecting package %s", package_spec)
3434

35-
if not venv_dir.exists() or not next(venv_dir.iterdir()):
35+
if not venv_dir.exists() or next(venv_dir.iterdir(), None) is None:
3636
raise PipxError(
3737
f"""
3838
Can't inject {package_spec!r} into nonexistent Virtual Environment

src/pipx/commands/run.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -198,11 +198,11 @@ def run(
198198
# we can't parse this as a package
199199
package_name = app
200200

201-
content = None if spec is not None else maybe_script_content(app, is_path)
201+
content = None if spec is not None else maybe_script_content(app, is_path) # type: ignore[redundant-expr]
202202
if content is not None:
203203
run_script(content, app_args, python, pip_args, venv_args, verbose, use_cache)
204204
else:
205-
package_or_url = spec if spec is not None else app
205+
package_or_url = spec if spec is not None else app # type: ignore[redundant-expr]
206206
run_package(
207207
package_name,
208208
package_or_url,

src/pipx/commands/uninject.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ def uninject(
125125
) -> ExitCode:
126126
"""Returns pipx exit code"""
127127

128-
if not venv_dir.exists() or not next(venv_dir.iterdir()):
128+
if not venv_dir.exists() or next(venv_dir.iterdir(), None) is None:
129129
raise PipxError(f"Virtual environment {venv_dir.name} does not exist.")
130130

131131
venv = Venv(venv_dir, verbose=verbose)

src/pipx/main.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ def run_pipx_command(args: argparse.Namespace, subparsers: Dict[str, argparse.Ar
277277
not args.no_cache,
278278
)
279279
# We should never reach here because run() is NoReturn.
280-
return ExitCode(1)
280+
return ExitCode(1) # type: ignore[unreachable]
281281
elif args.command == "install":
282282
return commands.install(
283283
None,
@@ -409,7 +409,7 @@ def run_pipx_command(args: argparse.Namespace, subparsers: Dict[str, argparse.Ar
409409
python_flag_passed=python_flag_passed,
410410
)
411411
elif args.command == "runpip":
412-
if not venv_dir:
412+
if not venv_dir: # type: ignore[truthy-bool]
413413
raise PipxError("Developer error: venv_dir is not defined.")
414414
return commands.run_pip(package, venv_dir, args.pipargs, args.verbose)
415415
elif args.command == "ensurepath":

src/pipx/paths.py

+7-7
Original file line numberDiff line numberDiff line change
@@ -107,25 +107,25 @@ def shared_libs(self) -> Path:
107107
return (self._base_shared_libs or self.home / "shared").resolve()
108108

109109
def make_local(self) -> None:
110-
self._base_home = OVERRIDE_PIPX_HOME or get_expanded_environ("PIPX_HOME")
110+
self._base_home = OVERRIDE_PIPX_HOME or get_expanded_environ("PIPX_HOME") # type: ignore[redundant-expr]
111111
self._default_home = DEFAULT_PIPX_HOME
112-
self._base_bin = OVERRIDE_PIPX_BIN_DIR or get_expanded_environ("PIPX_BIN_DIR")
112+
self._base_bin = OVERRIDE_PIPX_BIN_DIR or get_expanded_environ("PIPX_BIN_DIR") # type: ignore[redundant-expr]
113113
self._default_bin = DEFAULT_PIPX_BIN_DIR
114-
self._base_man = OVERRIDE_PIPX_MAN_DIR or get_expanded_environ("PIPX_MAN_DIR")
114+
self._base_man = OVERRIDE_PIPX_MAN_DIR or get_expanded_environ("PIPX_MAN_DIR") # type: ignore[redundant-expr]
115115
self._default_man = DEFAULT_PIPX_MAN_DIR
116-
self._base_shared_libs = OVERRIDE_PIPX_SHARED_LIBS or get_expanded_environ("PIPX_SHARED_LIBS")
116+
self._base_shared_libs = OVERRIDE_PIPX_SHARED_LIBS or get_expanded_environ("PIPX_SHARED_LIBS") # type: ignore[redundant-expr]
117117
self._default_log = Path(user_log_path("pipx"))
118118
self._default_cache = Path(user_cache_path("pipx"))
119119
self._default_trash = self._default_home / "trash"
120120
self._fallback_home = next(iter([fallback for fallback in FALLBACK_PIPX_HOMES if fallback.exists()]), None)
121121
self._home_exists = self._base_home is not None or any(fallback.exists() for fallback in FALLBACK_PIPX_HOMES)
122122

123123
def make_global(self) -> None:
124-
self._base_home = OVERRIDE_PIPX_GLOBAL_HOME or get_expanded_environ("PIPX_GLOBAL_HOME")
124+
self._base_home = OVERRIDE_PIPX_GLOBAL_HOME or get_expanded_environ("PIPX_GLOBAL_HOME") # type: ignore[redundant-expr]
125125
self._default_home = DEFAULT_PIPX_GLOBAL_HOME
126-
self._base_bin = OVERRIDE_PIPX_GLOBAL_BIN_DIR or get_expanded_environ("PIPX_GLOBAL_BIN_DIR")
126+
self._base_bin = OVERRIDE_PIPX_GLOBAL_BIN_DIR or get_expanded_environ("PIPX_GLOBAL_BIN_DIR") # type: ignore[redundant-expr]
127127
self._default_bin = DEFAULT_PIPX_GLOBAL_BIN_DIR
128-
self._base_man = OVERRIDE_PIPX_GLOBAL_MAN_DIR or get_expanded_environ("PIPX_GLOBAL_MAN_DIR")
128+
self._base_man = OVERRIDE_PIPX_GLOBAL_MAN_DIR or get_expanded_environ("PIPX_GLOBAL_MAN_DIR") # type: ignore[redundant-expr]
129129
self._default_man = DEFAULT_PIPX_GLOBAL_MAN_DIR
130130
self._default_log = self._default_home / "logs"
131131
self._default_cache = self._default_home / ".cache"

src/pipx/shared_libs.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ def upgrade(self, *, pip_args: List[str], verbose: bool = False, raises: bool =
114114
return
115115

116116
if pip_args is None:
117-
pip_args = []
117+
pip_args = [] # type: ignore[unreachable]
118118

119119
logger.info(f"Upgrading shared libraries in {self.root}")
120120

tests/test_upgrade_shared.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def test_upgrade_shared(shared_libs, capsys, caplog):
2323
assert "Upgrading shared libraries in" in caplog.text
2424
assert "Already upgraded libraries in" not in caplog.text
2525
assert shared_libs.has_been_updated_this_run is True
26-
assert shared_libs.is_valid is True
26+
assert shared_libs.is_valid is True # type: ignore[unreachable]
2727
shared_libs.has_been_updated_this_run = False
2828
assert run_pipx_cli(["upgrade-shared", "-v"]) == 0
2929
captured = capsys.readouterr()
@@ -59,7 +59,7 @@ def pip_version():
5959
assert shared_libs.is_valid is False
6060
assert run_pipx_cli(["upgrade-shared", "-v", "--pip-args=pip==24.0"]) == 0
6161
assert shared_libs.is_valid is True
62-
assert pip_version() == "24.0"
62+
assert pip_version() == "24.0" # type: ignore[unreachable]
6363
shared_libs.has_been_updated_this_run = False # reset for next run
6464
assert run_pipx_cli(["upgrade-shared", "-v", "--pip-args=pip==23.3.2"]) == 0
6565
assert shared_libs.is_valid is True

0 commit comments

Comments
 (0)