Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ repos:
priority: 5
args: ["--fix", "--unsafe-fixes", "--exit-non-zero-on-fix"]
- repo: https://github.com/gaborbernat/simplint-pre-commit
rev: "f15525433ac9797aa1580f46fdadfe60dd77923c" # frozen: v0.8.0
rev: "5f12b15e1199c42bdeb3e13ea1c8b45952fbd802" # frozen: v0.11.0
hooks:
- id: simplint
priority: 6
Expand Down
1 change: 1 addition & 0 deletions docs/changelog/+simplint.change.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Restore ``MarkerEnvironment`` as a package-root export after its removal during simplint adoption.
34 changes: 18 additions & 16 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -133,12 +133,13 @@ lint.ignore = [
"missing-trailing-comma", # Conflict with formatter
"multi-line-summary-first-line", # `multi-line-summary-first-line` and `multi-line-summary-second-line` are incompatible
"single-line-implicit-string-concatenation", # Conflict with formatter
"undocumented-magic-method", # undocumented-public-api (simplint SMP903) covers the public API instead
"undocumented-public-class", # undocumented-public-api (simplint SMP903) covers the public API instead
"undocumented-public-function", # undocumented-public-api (simplint SMP903) covers the public API instead
"undocumented-public-init", # undocumented-public-api (simplint SMP903) covers the public API instead
"undocumented-public-method", # undocumented-public-api (simplint SMP903) covers the public API instead
"undocumented-public-nested-class", # undocumented-public-api (simplint SMP903) covers the public API instead
"undocumented-magic-method", # undocumented-public-api (simplint) covers the public API instead
"undocumented-public-class", # undocumented-public-api (simplint) covers the public API instead
"undocumented-public-function", # undocumented-public-api (simplint) covers the public API instead
"undocumented-public-init", # undocumented-public-api (simplint) covers the public API instead
"undocumented-public-method", # undocumented-public-api (simplint) covers the public API instead
"undocumented-public-nested-class", # undocumented-public-api (simplint) covers the public API instead
"unnecessary-regular-expression-compile", # conflicts with simplint inline-regex
]
lint.per-file-ignores."docs/**/*.py" = [
"implicit-namespace-package", # no __init__.py in docs directory
Expand All @@ -153,12 +154,9 @@ lint.per-file-ignores."src/python_discovery/_py_info_collect.py" = [
"ANN", # file must parse on Python 2.7 for the version gate: no annotations
"missing-required-import", # `from __future__ import annotations` is a SyntaxError below 3.7
"module-import-not-at-top-of-file", # the version gate must run before importing sysconfig, absent on 3.0/3.1
"PTH", # must use os.path — file runs as subprocess script with only stdlib
"PTH", # probe exchanges string paths with older interpreters
"UP", # pyupgrade pushes syntax newer than the 2.7 parse floor, e.g. f-strings
]
lint.per-file-ignores."src/python_discovery/_windows/_pep514.py" = [
"PTH", # os.path.exists is monkeypatched in tests; pathlib.Path.exists bypasses the mock
]
lint.per-file-ignores."tasks/**/*.py" = [
"D", # release helper scripts, not API
"implicit-namespace-package", # no __init__.py in tasks directory
Expand All @@ -175,12 +173,12 @@ lint.per-file-ignores."tests/**/*.py" = [
"magic-value-comparison", # Magic value used in comparison
"private-member-access", # private member access needed to test internals
"pytest-fixture-autouse", # shared setup fixtures are meant to apply to every test
"subprocess-without-shell-equals-true", # `subprocess` call: check for execution of untrusted input
"suspicious-subprocess-import", # subprocess import
"subprocess-without-shell-equals-true", # tests launch known interpreters and test-created scripts
"suspicious-subprocess-import", # subprocess calls exercise interpreter discovery
"too-many-arguments", # too many arguments (pytest fixtures)
"too-many-positional-arguments", # too many positional arguments (pytest fixtures)
]
lint.per-file-ignores."tests/windows/winreg_mock_values.py" = [
lint.per-file-ignores."tests/python_discovery/_windows/winreg_mock_values.py" = [
"undefined-name", # undefined name (winreg available only on Windows)
]
lint.isort = { known-first-party = [
Expand All @@ -199,7 +197,7 @@ count = true
max_supported_python = "3.15"

[tool.ty]
src.exclude = [ "tests/windows/winreg_mock_values.py" ]
src.exclude = [ "tests/python_discovery/_windows/winreg_mock_values.py" ]

[[tool.ty.overrides]]
include = [
Expand Down Expand Up @@ -243,8 +241,12 @@ paths.source = [
]
report.fail_under = 100
report.omit = [
"src/python_discovery/_windows/*",
"tests/windows/*",
"src/python_discovery/_windows/_cancel_io.py",
"src/python_discovery/_windows/_pep514.py",
"src/python_discovery/_windows/_propose.py",
"tests/python_discovery/_windows/conftest.py",
"tests/python_discovery/_windows/test_pep514.py",
"tests/python_discovery/_windows/winreg_mock_values.py",
]
report.partial_branches = [
"assert any\\(",
Expand Down
8 changes: 4 additions & 4 deletions src/python_discovery/_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,10 +231,10 @@ def locked(self) -> Generator[None]:

class NoOpContentStore(ContentStore):
# Content store that does nothing -- implements ContentStore protocol.
def exists(self) -> bool: # ruff:ignore[no-self-use]
def exists(self) -> bool: # ruff:ignore[no-self-use] cache protocol
return False

def read(self) -> dict | None: # ruff:ignore[no-self-use] keeps the cache interface uniform across implementations
def read(self) -> dict | None: # ruff:ignore[no-self-use] cache protocol
return None

def write(self, content: dict) -> None:
Expand All @@ -244,13 +244,13 @@ def remove(self) -> None:
pass

@contextmanager
def locked(self) -> Generator[None]: # ruff:ignore[no-self-use] keeps the cache interface uniform across implementations
def locked(self) -> Generator[None]: # ruff:ignore[no-self-use] cache protocol
yield


class NoOpCache(PyInfoCache):
# Cache that does nothing -- implements PyInfoCache protocol.
def py_info(self, path: Path) -> NoOpContentStore: # ruff:ignore[unused-method-argument, no-self-use]
def py_info(self, path: Path) -> NoOpContentStore: # ruff:ignore[unused-method-argument, no-self-use] protocol
return NoOpContentStore()

def py_info_clear(self) -> None:
Expand Down
9 changes: 5 additions & 4 deletions src/python_discovery/_cached_py_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
from pathlib import Path
from select import select
from shlex import quote
from subprocess import DEVNULL, PIPE, Popen, TimeoutExpired, list2cmdline # ruff:ignore[suspicious-subprocess-import] list2cmdline only formats the command for logging

# ruff:ignore[suspicious-subprocess-import] interpreter interrogation
from subprocess import DEVNULL, PIPE, Popen, TimeoutExpired, list2cmdline
from threading import Event, Thread
from time import monotonic
from typing import TYPE_CHECKING, Final, NamedTuple
Expand All @@ -28,7 +30,7 @@
from ._py_info_collect import MIN_INTERROGATE_VERSION, UNSUPPORTED_EXIT_CODE, UNSUPPORTED_MARKER

if sys.platform == "win32": # pragma: win32 cover
from ._windows._cancel_io import stop_query_reader
from ._windows import stop_query_reader

if TYPE_CHECKING:
from collections.abc import Generator, Mapping
Expand Down Expand Up @@ -207,7 +209,7 @@ def _run_subprocess(
env["PYTHONUTF8"] = "1"
_LOGGER.debug("get interpreter info via cmd: %s", cmd if isinstance(cmd, str) else LogCmd(cmd))
try:
process = Popen( # ruff:ignore[subprocess-without-shell-equals-true] argv list without a shell, so no shell injection
process = Popen( # ruff:ignore[subprocess-without-shell-equals-true] interrogate discovered interpreter
cmd,
universal_newlines=True,
stdin=DEVNULL,
Expand Down Expand Up @@ -460,5 +462,4 @@ def clear(cache: PyInfoCache) -> None:
"LogCmd",
"clear",
"from_exe",
"gen_cookie",
]
8 changes: 3 additions & 5 deletions src/python_discovery/_discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ def _propose_current_and_windows(
yield current_python, True

if IS_WIN: # pragma: win32 cover
from ._windows._propose import propose_interpreters as win_propose # ruff:ignore[import-outside-top-level] the module is Windows-only and imported only there
from ._windows import propose_interpreters as win_propose # ruff:ignore[import-outside-top-level] Windows-only

for interpreter in win_propose(spec, cache, env):
if _is_new_exe(str(interpreter.executable), tested_exes):
Expand Down Expand Up @@ -433,7 +433,7 @@ def _uv_specifier_matches(key: _UvKey, specifier: SimpleSpecifier) -> bool:
precision = numeric_version.count(".") + 1
release = ".".join(str(part) for part in key.version.release[:precision])
if key.version.pre_type is not None and (
precision == 3 or specifier.version.pre_type is not None # ruff:ignore[magic-value-comparison] 3 is the precision of a full version tuple
precision == 3 or specifier.version.pre_type is not None # ruff:ignore[magic-value-comparison] full version
):
release = f"{release}{key.version.pre_type}{key.version.pre_num}"
return specifier.contains(release)
Expand Down Expand Up @@ -529,7 +529,7 @@ def iter_interpreters(
yield from _iter_for_spec(spec_str, first_with, cache, env_map, predicate, seen)


def _iter_for_spec( # ruff:ignore[too-many-arguments, too-many-positional-arguments] each argument is a separate spec input
def _iter_for_spec( # ruff:ignore[too-many-arguments, too-many-positional-arguments] distinct spec inputs
spec_str: str | None,
try_first_with: tuple[str, ...],
cache: PyInfoCache | None,
Expand Down Expand Up @@ -576,11 +576,9 @@ class _UvInstall:


__all__ = [
"IS_WIN",
"LazyPathDump",
"get_interpreter",
"get_paths",
"iter_interpreters",
"path_exe_finder",
"propose_interpreters",
]
20 changes: 10 additions & 10 deletions src/python_discovery/_py_info.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# simplint: ignore[flattened-package] # ruff: ignore[commented-out-code] # keep the existing private path
"""Concrete Python interpreter information, collected in-process or via subprocess interrogation."""

from __future__ import annotations
Expand Down Expand Up @@ -44,7 +45,7 @@ def _get_path_extensions() -> list[str]:
EXTENSIONS: Final[list[str]] = _get_path_extensions()


class PythonInfo: # ruff:ignore[too-many-public-methods] PythonInfo is the public query surface; splitting it breaks callers
class PythonInfo: # ruff:ignore[too-many-public-methods] public query API
"""
Contains information for a Python interpreter.

Expand Down Expand Up @@ -200,7 +201,7 @@ def from_exe( # ruff:ignore[too-many-arguments] each argument is a separate pro
)

@classmethod
def _from_exe_with_args( # ruff:ignore[too-many-arguments] each argument is a separate probe input
def _from_exe_with_args( # ruff:ignore[too-many-arguments] distinct probe inputs
cls,
exe: str,
cache: PyInfoCache | None = None,
Expand All @@ -211,8 +212,7 @@ def _from_exe_with_args( # ruff:ignore[too-many-arguments] each argument is a s
resolve_to_host: bool = True,
env: Mapping[str, str] | None = None,
) -> PythonInfo | None:
# Interrogate an executable with optional platform-specific leading arguments.
from ._cached_py_info import from_exe # ruff:ignore[import-outside-top-level]
from ._cached_py_info import from_exe # ruff:ignore[import-outside-top-level] deferred import avoids a cycle

env = os.environ if env is None else env
proposed = from_exe(
Expand Down Expand Up @@ -472,12 +472,13 @@ def clear_cache(cls, cache: PyInfoCache) -> None:

:param cache: the cache store to clear.
"""
from ._cached_py_info import clear # ruff:ignore[import-outside-top-level] deferred import avoids a cycle with _cached_py_info
from ._cached_py_info import clear # ruff:ignore[import-outside-top-level] import cycle

clear(cache)
cls._cache_exe_discovery.clear()

def satisfies(self, spec: PythonSpec, *, impl_must_match: bool) -> bool: # ruff:ignore[too-many-return-statements] one return per distinct satisfaction outcome
# ruff:ignore[too-many-return-statements] distinct outcomes
def satisfies(self, spec: PythonSpec, *, impl_must_match: bool) -> bool:
"""
Check if a given specification can be satisfied by this python interpreter instance.

Expand Down Expand Up @@ -535,7 +536,8 @@ def _satisfies_version_specifier(self, spec: PythonSpec) -> bool:
release = ".".join(str(c) for c in [version_info.major, version_info.minor, version_info.micro][:precision])
if (
version_info.releaselevel != "final"
and (precision == 3 or specifier.version.pre_type is not None) # ruff:ignore[magic-value-comparison] 3 is the precision of a full version tuple
# ruff:ignore[magic-value-comparison] full version has three parts
and (precision == 3 or specifier.version.pre_type is not None)
and (suffix := {"alpha": "a", "beta": "b", "candidate": "rc"}.get(version_info.releaselevel))
):
release = f"{release}{suffix}{version_info.serial}"
Expand Down Expand Up @@ -629,7 +631,7 @@ def _possible_base(self) -> Generator[str, None, None]:
for base in possible_base:
lower = base.lower()
yield lower
from ._compat import fs_is_case_sensitive # ruff:ignore[import-outside-top-level] imported only when this path is walked
from ._compat import fs_is_case_sensitive # ruff:ignore[import-outside-top-level] cold path

if fs_is_case_sensitive(): # pragma: no branch, the branch depends on the host filesystem
if base != lower:
Expand Down Expand Up @@ -780,9 +782,7 @@ def normalize_isa(isa: str) -> str:


__all__ = [
"EXTENSIONS",
"KNOWN_ARCHITECTURES",
"InfoValue",
"MarkerEnvironment",
"PythonInfo",
"VersionInfo",
Expand Down
18 changes: 9 additions & 9 deletions src/python_discovery/_py_info_collect.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# simplint: ignore[flattened-package] # ruff: ignore[commented-out-code] # keep the existing private path
"""
Collect interpreter information, also run as the subprocess interrogation script (stdlib only).

Expand Down Expand Up @@ -246,18 +247,18 @@ def _distutils_install(): # pragma: <3.11 cover # 3.11+ uses the "venv" scheme
warnings.simplefilter("ignore")
try:
# ruff:ignore[import-outside-top-level] distutils is optional and removed in newer Pythons
from distutils import dist # ty: ignore[unresolved-import] distutils is removed from the stdlib in Python 3.12
from distutils import dist # ty: ignore[unresolved-import] removed in 3.12

# ruff:ignore[import-outside-top-level] distutils is optional and removed in newer Pythons
from distutils.command.install import SCHEME_KEYS # ty: ignore[unresolved-import] distutils is removed from the stdlib in Python 3.12
from distutils.command.install import SCHEME_KEYS # ty: ignore[unresolved-import] removed in 3.12
except ImportError: # pragma: no cover # if removed or not installed ignore
return {}

distribution = dist.Distribution({
"script_args": "--no-user-cfg",
}) # conf files not parsed so they do not hijack paths
if hasattr(sys, "_framework"): # pragma: no cover # macOS framework builds only
sys._framework = None # ty: ignore[invalid-assignment] # ruff:ignore[private-member-access] # disable macOS static paths for framework
sys._framework = None # ty: ignore[invalid-assignment] # ruff:ignore[private-member-access] macOS paths

with warnings.catch_warnings(): # disable warning for PEP-632
warnings.simplefilter("ignore")
Expand Down Expand Up @@ -311,7 +312,7 @@ def _get_tcl_tk_libs(): # pragma: no cover # tkinter availability varies; teste
else:
# Tk.__init__ reads profile scripts from HOME or the working directory; probing needs only library metadata.
class _TclWithoutProfile(tk.Tk):
def readprofile(self, *_args, **_kwargs): # ruff:ignore[no-self-use] # Tk override must be an instance method.
def readprofile(self, *_args, **_kwargs): # ruff:ignore[no-self-use] Tk override
return None

try:
Expand All @@ -324,17 +325,16 @@ def readprofile(self, *_args, **_kwargs): # ruff:ignore[no-self-use] # Tk overr
return tcl_lib, tk_lib

@staticmethod
def _resolve_tk_lib(tcl, tcl_lib): # pragma: no cover, Tcl probing needs a Tk install the test runners lack
def _resolve_tk_lib(tcl, tcl_lib): # pragma: no cover # Tk is absent on test runners
# Resolve the TK library path by direct query or path construction.
tk_lib = PythonInfoCollector._query_tk_library(tcl)
if tk_lib is not None:
return tk_lib
import _tkinter # ruff:ignore[import-outside-top-level,import-private-name] # novermin # linked Tk version
import _tkinter # ruff:ignore[import-outside-top-level,import-private-name] # simplint: ignore[private-import-path] # novermin

# Loading Tk can block on DISPLAY; the linked version is enough to locate its library.
tk_version = _tkinter.TK_VERSION
tcl_parent = os.path.dirname(tcl_lib)
for version in (tk_version, tk_version.split(".")[0]):
version_text = _tkinter.TK_VERSION
for version in (version_text, version_text.split(".", maxsplit=1)[0]):
tk_lib_path = os.path.join(tcl_parent, "tk{}".format(version))
if os.path.isdir(tk_lib_path) and os.path.exists(os.path.join(tk_lib_path, "tk.tcl")):
return tk_lib_path
Expand Down
10 changes: 7 additions & 3 deletions src/python_discovery/_py_spec.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# simplint: ignore[flattened-package] # ruff: ignore[commented-out-code] # keep the existing private path
"""A Python specification is an abstract requirement definition of an interpreter."""

from __future__ import annotations
Expand Down Expand Up @@ -131,6 +132,9 @@ def _parse_specifier(string_spec: str) -> PythonSpec | None:
)


_GET_REQUIRED_PRECISION_PATTERN: Final[re.Pattern[str]] = re.compile(r"a|b|rc")


class PythonSpec:
"""
Contains specification about a Python Interpreter.
Expand All @@ -152,7 +156,7 @@ class PythonSpec:
constraints, or ``None``.
"""

def __init__( # ruff:ignore[too-many-arguments, too-many-positional-arguments] making the arguments keyword-only breaks positional callers
def __init__( # ruff:ignore[too-many-arguments, too-many-positional-arguments] positional callers
self,
str_spec: str,
implementation: str | None,
Expand Down Expand Up @@ -243,7 +247,7 @@ def generate_re(self, *, windows: bool, all_implementations: bool = False) -> re
flags=re.IGNORECASE,
)

def satisfies(self, spec: PythonSpec) -> bool: # ruff:ignore[too-many-return-statements] one return per distinct satisfaction outcome
def satisfies(self, spec: PythonSpec) -> bool: # ruff:ignore[too-many-return-statements] distinct outcomes
"""
Check if this spec is compatible with the given *spec* (e.g. PEP-514 on Windows).

Expand Down Expand Up @@ -310,7 +314,7 @@ def _get_required_precision(item: SimpleSpecifier) -> int | None:
return None
if item.wildcard_precision is not None:
return item.wildcard_precision
return re.split(r"a|b|rc", item.version_str, maxsplit=1)[0].count(".") + 1
return _GET_REQUIRED_PRECISION_PATTERN.split(item.version_str, maxsplit=1)[0].count(".") + 1


__all__ = [
Expand Down
Loading