From 1a332802ff8afde3c899df46901f00bde523e4e3 Mon Sep 17 00:00:00 2001 From: Ran Benita Date: Sat, 27 Apr 2024 01:11:21 +0300 Subject: [PATCH 1/2] Avoid slicing `sys.version_info` in version conditionals It is unnecessary, and some static analyzers don't handle it. --- src/_pytest/_code/code.py | 4 ++-- src/_pytest/config/argparsing.py | 2 +- src/_pytest/fixtures.py | 2 +- src/_pytest/runner.py | 2 +- testing/code/test_excinfo.py | 2 +- testing/test_runner.py | 2 +- testing/test_skipping.py | 2 +- 7 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/_pytest/_code/code.py b/src/_pytest/_code/code.py index 3b4a62a4fa0..c65ce79f7e5 100644 --- a/src/_pytest/_code/code.py +++ b/src/_pytest/_code/code.py @@ -52,7 +52,7 @@ from _pytest.pathlib import bestrelpath -if sys.version_info[:2] < (3, 11): +if sys.version_info < (3, 11): from exceptiongroup import BaseExceptionGroup _TracebackStyle = Literal["long", "short", "line", "no", "native", "value", "auto"] @@ -703,7 +703,7 @@ def _stringify_exception(self, exc: BaseException) -> str: # Workaround for https://github.com/python/cpython/issues/98778 on # Python <= 3.9, and some 3.10 and 3.11 patch versions. HTTPError = getattr(sys.modules.get("urllib.error", None), "HTTPError", ()) - if sys.version_info[:2] <= (3, 11) and isinstance(exc, HTTPError): + if sys.version_info < (3, 12) and isinstance(exc, HTTPError): notes = [] else: raise diff --git a/src/_pytest/config/argparsing.py b/src/_pytest/config/argparsing.py index 95dc28d4a16..9006351af72 100644 --- a/src/_pytest/config/argparsing.py +++ b/src/_pytest/config/argparsing.py @@ -448,7 +448,7 @@ def parse_args( # type: ignore getattr(parsed, FILE_OR_DIR).extend(unrecognized) return parsed - if sys.version_info[:2] < (3, 9): # pragma: no cover + if sys.version_info < (3, 9): # pragma: no cover # Backport of https://github.com/python/cpython/pull/14316 so we can # disable long --argument abbreviations without breaking short flags. def _parse_optional( diff --git a/src/_pytest/fixtures.py b/src/_pytest/fixtures.py index 5303948c4d5..656160ff7dc 100644 --- a/src/_pytest/fixtures.py +++ b/src/_pytest/fixtures.py @@ -69,7 +69,7 @@ from _pytest.scope import Scope -if sys.version_info[:2] < (3, 11): +if sys.version_info < (3, 11): from exceptiongroup import BaseExceptionGroup diff --git a/src/_pytest/runner.py b/src/_pytest/runner.py index 3f706b927c8..9bc544ea742 100644 --- a/src/_pytest/runner.py +++ b/src/_pytest/runner.py @@ -39,7 +39,7 @@ from _pytest.outcomes import TEST_OUTCOME -if sys.version_info[:2] < (3, 11): +if sys.version_info < (3, 11): from exceptiongroup import BaseExceptionGroup if TYPE_CHECKING: diff --git a/testing/code/test_excinfo.py b/testing/code/test_excinfo.py index 419c11abcc0..dd4bd22c8b8 100644 --- a/testing/code/test_excinfo.py +++ b/testing/code/test_excinfo.py @@ -28,7 +28,7 @@ if TYPE_CHECKING: from _pytest._code.code import _TracebackStyle -if sys.version_info[:2] < (3, 11): +if sys.version_info < (3, 11): from exceptiongroup import ExceptionGroup diff --git a/testing/test_runner.py b/testing/test_runner.py index 6e034e94532..8b41ec28a38 100644 --- a/testing/test_runner.py +++ b/testing/test_runner.py @@ -23,7 +23,7 @@ import pytest -if sys.version_info[:2] < (3, 11): +if sys.version_info < (3, 11): from exceptiongroup import ExceptionGroup diff --git a/testing/test_skipping.py b/testing/test_skipping.py index 3f1c83f5bbe..a1511b26d1c 100644 --- a/testing/test_skipping.py +++ b/testing/test_skipping.py @@ -1146,7 +1146,7 @@ def test_func(): if pypy_version_info is not None and pypy_version_info < (6,): markline = markline[1:] - if sys.version_info[:2] >= (3, 10): + if sys.version_info >= (3, 10): expected = [ "*ERROR*test_nameerror*", "*asd*", From 80ca255d424da35f8a28216dc25170850779ff93 Mon Sep 17 00:00:00 2001 From: Ran Benita Date: Sat, 27 Apr 2024 11:03:52 +0300 Subject: [PATCH 2/2] pathlib: make `absolutepath` support `os.PathLike[str]` This slightly simplifies a bit of path. --- src/_pytest/nodes.py | 2 +- src/_pytest/pathlib.py | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/_pytest/nodes.py b/src/_pytest/nodes.py index 1b91bdb6e43..974d756a2be 100644 --- a/src/_pytest/nodes.py +++ b/src/_pytest/nodes.py @@ -765,7 +765,7 @@ def location(self) -> Tuple[str, Optional[int], str]: and lineno is a 0-based line number. """ location = self.reportinfo() - path = absolutepath(os.fspath(location[0])) + path = absolutepath(location[0]) relfspath = self.session._node_location_to_relpath(path) assert type(location[2]) is str return (relfspath, location[1], location[2]) diff --git a/src/_pytest/pathlib.py b/src/_pytest/pathlib.py index 190d9dd8ce6..e14c2acd328 100644 --- a/src/_pytest/pathlib.py +++ b/src/_pytest/pathlib.py @@ -924,13 +924,13 @@ def visit( yield from visit(entry.path, recurse) -def absolutepath(path: Union[Path, str]) -> Path: +def absolutepath(path: "Union[str, os.PathLike[str]]") -> Path: """Convert a path to an absolute path using os.path.abspath. Prefer this over Path.resolve() (see #6523). Prefer this over Path.absolute() (not public, doesn't normalize). """ - return Path(os.path.abspath(str(path))) + return Path(os.path.abspath(path)) def commonpath(path1: Path, path2: Path) -> Optional[Path]: