From 351f2a9197a71b76b63d745b9d346e2b10c3509b Mon Sep 17 00:00:00 2001 From: Henry Schreiner Date: Tue, 12 Nov 2024 13:37:11 -0500 Subject: [PATCH] chore: add some preview check fixes Signed-off-by: Henry Schreiner --- nox/__init__.py | 2 +- nox/_parametrize.py | 6 +++--- nox/_resolver.py | 2 +- nox/_version.py | 2 +- nox/manifest.py | 2 +- nox/popen.py | 6 +++--- nox/project.py | 2 +- nox/registry.py | 6 ++++-- nox/sessions.py | 3 --- nox/tasks.py | 2 +- nox/tox_to_nox.py | 6 +++--- nox/virtualenv.py | 2 +- tests/test__version.py | 2 +- tests/test_command.py | 2 +- tests/test_logger.py | 6 +++--- tests/test_main.py | 2 +- tests/test_manifest.py | 2 +- tests/test_sessions.py | 8 ++------ tests/test_tasks.py | 2 +- tests/test_virtualenv.py | 22 +++++++++++----------- 20 files changed, 41 insertions(+), 46 deletions(-) diff --git a/nox/__init__.py b/nox/__init__.py index 2602946c..5338044d 100644 --- a/nox/__init__.py +++ b/nox/__init__.py @@ -26,11 +26,11 @@ __all__ = [ "Session", + "main", "needs_version", "options", "param", "parametrize", "project", "session", - "main", ] diff --git a/nox/_parametrize.py b/nox/_parametrize.py index 9fc8b52d..15f802e1 100644 --- a/nox/_parametrize.py +++ b/nox/_parametrize.py @@ -75,9 +75,9 @@ def copy(self) -> Param: def update(self, other: Param) -> None: self.id = ", ".join([str(self), str(other)]) - self.args = self.args + other.args - self.arg_names = self.arg_names + other.arg_names - self.tags = self.tags + other.tags + self.args += other.args + self.arg_names += other.arg_names + self.tags += other.tags def __eq__(self, other: object) -> bool: if isinstance(other, self.__class__): diff --git a/nox/_resolver.py b/nox/_resolver.py index 6e0fbab3..e95af676 100644 --- a/nox/_resolver.py +++ b/nox/_resolver.py @@ -119,7 +119,7 @@ def lazy_stable_topo_sort( ~nox._resolver.CycleError: If a dependency cycle is encountered. """ - visited = {node: False for node in dependencies} + visited = dict.fromkeys(dependencies, False) def prepended_by_dependencies( node: Node, diff --git a/nox/_version.py b/nox/_version.py index 1b6341d8..f233c09b 100644 --- a/nox/_version.py +++ b/nox/_version.py @@ -61,7 +61,7 @@ def _parse_needs_version(source: str, filename: str = "") -> str | None def _read_needs_version(filename: str) -> str | None: """Read ``nox.needs_version`` from the user's Noxfile.""" - with open(filename) as io: + with open(filename, encoding="utf-8") as io: source = io.read() return _parse_needs_version(source, filename=filename) diff --git a/nox/manifest.py b/nox/manifest.py index f42dc531..957d3858 100644 --- a/nox/manifest.py +++ b/nox/manifest.py @@ -375,7 +375,7 @@ def make_session( return sessions def next(self) -> SessionRunner: - return self.__next__() + return next(self) def notify( self, session: str | SessionRunner, posargs: Iterable[str] | None = None diff --git a/nox/popen.py b/nox/popen.py index 504a6634..b6f7bc6d 100644 --- a/nox/popen.py +++ b/nox/popen.py @@ -57,7 +57,7 @@ def decode_output(output: bytes) -> str: return output.decode("utf-8") except UnicodeDecodeError: second_encoding = locale.getpreferredencoding() - if second_encoding.casefold() in ("utf8", "utf-8"): + if second_encoding.casefold() in {"utf8", "utf-8"}: raise return output.decode(second_encoding) @@ -86,11 +86,11 @@ def popen( proc = subprocess.Popen(args, env=env, stdout=stdout, stderr=stderr) try: - out, err = proc.communicate() + out, _err = proc.communicate() sys.stdout.flush() except KeyboardInterrupt: - out, err = shutdown_process(proc, interrupt_timeout, terminate_timeout) + out, _err = shutdown_process(proc, interrupt_timeout, terminate_timeout) if proc.returncode != 0: raise diff --git a/nox/project.py b/nox/project.py index ce66c29b..d808c9a9 100644 --- a/nox/project.py +++ b/nox/project.py @@ -19,7 +19,7 @@ import tomllib -__all__ = ["load_toml", "python_versions", "dependency_groups"] +__all__ = ["dependency_groups", "load_toml", "python_versions"] def __dir__() -> list[str]: diff --git a/nox/registry.py b/nox/registry.py index 6558c0d4..054d3fc6 100644 --- a/nox/registry.py +++ b/nox/registry.py @@ -32,12 +32,13 @@ @overload -def session_decorator(__func: F) -> F: ... +def session_decorator(func: F, /) -> F: ... @overload def session_decorator( - __func: None = ..., + func: None = ..., + /, python: Python | None = ..., py: Python | None = ..., reuse_venv: bool | None = ..., @@ -53,6 +54,7 @@ def session_decorator( def session_decorator( func: F | None = None, + /, python: Python | None = None, py: Python | None = None, reuse_venv: bool | None = None, diff --git a/nox/sessions.py b/nox/sessions.py index 2f17fd42..8851c5a5 100644 --- a/nox/sessions.py +++ b/nox/sessions.py @@ -1145,9 +1145,6 @@ def __init__( def __bool__(self) -> bool: return self.status.value > 0 - def __nonzero__(self) -> bool: - return self.__bool__() - @property def imperfect(self) -> str: """Return the English imperfect tense for the status. diff --git a/nox/tasks.py b/nox/tasks.py index 304dcb92..99dd3eef 100644 --- a/nox/tasks.py +++ b/nox/tasks.py @@ -419,7 +419,7 @@ def create_report( return results # Write the JSON report. - with open(global_config.report, "w") as report_file: + with open(global_config.report, "w", encoding="utf-8") as report_file: json.dump( { "result": int(all(results)), diff --git a/nox/tox_to_nox.py b/nox/tox_to_nox.py index 34e8cc14..24db98ca 100644 --- a/nox/tox_to_nox.py +++ b/nox/tox_to_nox.py @@ -63,7 +63,7 @@ def fixname(envname: str) -> str: def write_output_to_file(output: str, filename: str) -> None: """Write output to a file.""" - with open(filename, "w") as outfile: + with open(filename, "w", encoding="utf-8") as outfile: outfile.write(output) @@ -88,11 +88,11 @@ def main() -> None: set_env = {} for var in section.get("set_env", "").strip().splitlines(): k, v = var.split("=") - if k not in ( + if k not in { "PYTHONHASHSEED", "PIP_DISABLE_PIP_VERSION_CHECK", "PYTHONIOENCODING", - ): + }: set_env[k] = v config[name]["set_env"] = set_env diff --git a/nox/virtualenv.py b/nox/virtualenv.py index 6cd3999b..17419dc8 100644 --- a/nox/virtualenv.py +++ b/nox/virtualenv.py @@ -456,7 +456,7 @@ def _clean_location(self) -> bool: def _read_pyvenv_cfg(self) -> dict[str, str] | None: """Read a pyvenv.cfg file into dict, returns None if missing.""" path = os.path.join(self.location, "pyvenv.cfg") - with contextlib.suppress(FileNotFoundError), open(path) as fp: + with contextlib.suppress(FileNotFoundError), open(path, encoding="utf-8") as fp: parts = (x.partition("=") for x in fp if "=" in x) return {k.strip(): v.strip() for k, _, v in parts} return None diff --git a/tests/test__version.py b/tests/test__version.py index 3d683970..61f3f90d 100644 --- a/tests/test__version.py +++ b/tests/test__version.py @@ -50,7 +50,7 @@ def test_needs_version_default() -> None: def test_get_nox_version() -> None: """It returns something that looks like a Nox version.""" result = get_nox_version() - year, month, day, *_ = (int(part) for part in result.split(".")) + year, _month, _day, *_ = (int(part) for part in result.split(".")) assert year >= 2020 diff --git a/tests/test_command.py b/tests/test_command.py index ff441581..be8bfadc 100644 --- a/tests/test_command.py +++ b/tests/test_command.py @@ -259,7 +259,7 @@ def test_fail_with_silent(capsys): ], silent=True, ) - out, err = capsys.readouterr() + _out, err = capsys.readouterr() assert "out" in err assert "err" in err diff --git a/tests/test_logger.py b/tests/test_logger.py index 598802bd..3f726b58 100644 --- a/tests/test_logger.py +++ b/tests/test_logger.py @@ -41,7 +41,7 @@ def test_formatter(caplog): logger.logger.info("bar") logger.logger.output("foo") - logs = [rec for rec in caplog.records if rec.levelname in ("INFO", "OUTPUT")] + logs = [rec for rec in caplog.records if rec.levelname in {"INFO", "OUTPUT"}] assert len(logs) == 1 assert not hasattr(logs[0], "asctime") @@ -50,7 +50,7 @@ def test_formatter(caplog): logger.logger.info("bar") logger.logger.output("foo") - logs = [rec for rec in caplog.records if rec.levelname in ("INFO", "OUTPUT")] + logs = [rec for rec in caplog.records if rec.levelname in {"INFO", "OUTPUT"}] assert len(logs) == 2 logs = [rec for rec in caplog.records if rec.levelname == "OUTPUT"] @@ -75,7 +75,7 @@ def test_no_color_timestamp(caplog, color): logger.logger.info("bar") logger.logger.output("foo") - logs = [rec for rec in caplog.records if rec.levelname in ("INFO", "OUTPUT")] + logs = [rec for rec in caplog.records if rec.levelname in {"INFO", "OUTPUT"}] assert len(logs) == 1 assert hasattr(logs[0], "asctime") diff --git a/tests/test_main.py b/tests/test_main.py index 6b91d74f..60db4980 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -540,7 +540,7 @@ def test_main_requires_chain_fail(run_nox, session): @pytest.mark.parametrize("session", ["w", "u"]) def test_main_requries_modern_param(run_nox, session): noxfile = os.path.join(RESOURCES, "noxfile_requires.py") - returncode, _, stderr = run_nox(f"--noxfile={noxfile}", f"--session={session}") + returncode, _, _stderr = run_nox(f"--noxfile={noxfile}", f"--session={session}") assert returncode == 0 diff --git a/tests/test_manifest.py b/tests/test_manifest.py index ac2e717d..db0b48e3 100644 --- a/tests/test_manifest.py +++ b/tests/test_manifest.py @@ -149,7 +149,7 @@ def test_iteration(): # Continuing past the end raises StopIteration. with pytest.raises(StopIteration): - manifest.__next__() + next(manifest) def test_len(): diff --git a/tests/test_sessions.py b/tests/test_sessions.py index b95892a4..54ef6f21 100644 --- a/tests/test_sessions.py +++ b/tests/test_sessions.py @@ -179,7 +179,7 @@ def test_virtualenv_as_none(self): assert session.venv_backend == "none" def test_interactive(self): - session, runner = self.make_session_and_runner() + session, _runner = self.make_session_and_runner() with mock.patch("nox.sessions.sys.stdin.isatty") as m_isatty: m_isatty.return_value = True @@ -426,7 +426,7 @@ def test_run_no_install_passthrough(self): session.conda_install("numpy") def test_run_no_conda_install(self): - session, runner = self.make_session_and_runner() + session, _runner = self.make_session_and_runner() with pytest.raises(TypeError, match="A session without a conda"): session.conda_install("numpy") @@ -1318,15 +1318,11 @@ def test__bool_true(self): for status in (nox.sessions.Status.SUCCESS, nox.sessions.Status.SKIPPED): result = nox.sessions.Result(session=object(), status=status) assert bool(result) - assert result.__bool__() - assert result.__nonzero__() def test__bool_false(self): for status in (nox.sessions.Status.FAILED, nox.sessions.Status.ABORTED): result = nox.sessions.Result(session=object(), status=status) assert not bool(result) - assert not result.__bool__() - assert not result.__nonzero__() def test__imperfect(self): result = nox.sessions.Result(object(), nox.sessions.Status.SUCCESS) diff --git a/tests/test_tasks.py b/tests/test_tasks.py index a81a54fb..da91c352 100644 --- a/tests/test_tasks.py +++ b/tests/test_tasks.py @@ -706,7 +706,7 @@ def test_create_report(): mock.ANY, indent=2, ) - open_.assert_called_once_with("/path/to/report", "w") + open_.assert_called_once_with("/path/to/report", "w", encoding="utf-8") def test_final_reduce(): diff --git a/tests/test_virtualenv.py b/tests/test_virtualenv.py index d084e805..dc787208 100644 --- a/tests/test_virtualenv.py +++ b/tests/test_virtualenv.py @@ -206,18 +206,18 @@ def test_condaenv_create_interpreter(make_conda): @has_conda def test_conda_env_create_verbose(make_conda): - venv, dir_ = make_conda() + venv, _dir = make_conda() with mock.patch("nox.virtualenv.nox.command.run") as mock_run: venv.create() - args, kwargs = mock_run.call_args + _args, kwargs = mock_run.call_args assert kwargs["log"] is False nox.options.verbose = True with mock.patch("nox.virtualenv.nox.command.run") as mock_run: venv.create() - args, kwargs = mock_run.call_args + _args, kwargs = mock_run.call_args assert kwargs["log"] @@ -236,7 +236,7 @@ def test_condaenv_bin_windows(make_conda): @has_conda def test_condaenv_(make_conda): - venv, dir_ = make_conda() + venv, _dir = make_conda() assert not venv.is_offline() @@ -402,7 +402,7 @@ def test_create(monkeypatch, make_one): def test_create_reuse_environment(make_one): - venv, location = make_one(reuse_existing=True) + venv, _location = make_one(reuse_existing=True) venv.create() reused = not venv.create() @@ -452,10 +452,10 @@ def test_not_stale_virtualenv_environment(make_one, monkeypatch): # Making the reuse requirement more strict monkeypatch.setenv("NOX_ENABLE_STALENESS_CHECK", "1") - venv, location = make_one(reuse_existing=True, venv_backend="virtualenv") + venv, _location = make_one(reuse_existing=True, venv_backend="virtualenv") venv.create() - venv, location = make_one(reuse_existing=True, venv_backend="virtualenv") + venv, _location = make_one(reuse_existing=True, venv_backend="virtualenv") reused = not venv.create() assert reused @@ -463,10 +463,10 @@ def test_not_stale_virtualenv_environment(make_one, monkeypatch): @has_conda def test_stale_virtualenv_to_conda_environment(make_one): - venv, location = make_one(reuse_existing=True, venv_backend="virtualenv") + venv, _location = make_one(reuse_existing=True, venv_backend="virtualenv") venv.create() - venv, location = make_one(reuse_existing=True, venv_backend="conda") + venv, _location = make_one(reuse_existing=True, venv_backend="conda") reused = not venv.create() # The environment is not reused because it is now conda style @@ -732,7 +732,7 @@ def test_inner_functions_reusing_venv(make_one, monkeypatch): reason="Python 2.7 unsupported for virtualenv>=20.22.0", ) def test_create_reuse_python2_environment(make_one): - venv, location = make_one(reuse_existing=True, interpreter="2.7") + venv, _location = make_one(reuse_existing=True, interpreter="2.7") try: venv.create() @@ -745,7 +745,7 @@ def test_create_reuse_python2_environment(make_one): def test_create_venv_backend(make_one): - venv, dir_ = make_one(venv_backend="venv") + venv, _dir = make_one(venv_backend="venv") venv.create()