Skip to content

Commit

Permalink
Allow BinaryPaths to close over their type. (pantsbuild#18760)
Browse files Browse the repository at this point in the history
This fix allows subclasses to seal in their type and eliminate consumers
needing to cast.
  • Loading branch information
jsirois authored Apr 18, 2023
1 parent 717adfe commit 5b05b42
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def verbosity(self) -> int:


@dataclass(frozen=True)
class PythonExecutable(BinaryPath, EngineAwareReturnType):
class PythonExecutable(BinaryPath["PythonExecutable"], EngineAwareReturnType):
"""The BinaryPath of a Python executable for user code, along with some extras."""

append_only_caches: FrozenDict[str, str] = FrozenDict({})
Expand Down
11 changes: 7 additions & 4 deletions src/python/pants/core/util_rules/system_binaries.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from dataclasses import dataclass
from enum import Enum
from textwrap import dedent # noqa: PNT20
from typing import Iterable, Mapping, Sequence
from typing import Generic, Iterable, Mapping, Sequence, TypeVar

from pants.base.deprecated import warn_or_error
from pants.core.subsystems import python_bootstrap
Expand Down Expand Up @@ -40,8 +40,11 @@
SEARCH_PATHS = ("/usr/bin", "/bin", "/usr/local/bin", "/opt/homebrew/bin")


BinaryPathT = TypeVar("BinaryPathT", bound="BinaryPath")


@dataclass(frozen=True)
class BinaryPath:
class BinaryPath(Generic[BinaryPathT]):
path: str
fingerprint: str

Expand All @@ -58,8 +61,8 @@ def _fingerprint(content: bytes | bytearray | memoryview | None = None) -> str:

@classmethod
def fingerprinted(
cls, path: str, representative_content: bytes | bytearray | memoryview
) -> BinaryPath:
cls: type[BinaryPathT], path: str, representative_content: bytes | bytearray | memoryview
) -> BinaryPathT:
return cls(path, fingerprint=cls._fingerprint(representative_content))


Expand Down
7 changes: 2 additions & 5 deletions src/python/pants/init/plugin_resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,8 @@ async def resolve_plugins(

python: PythonExecutable | None = None
if not request.interpreter_constraints:
python = cast(
PythonExecutable,
PythonExecutable.fingerprinted(
sys.executable, ".".join(map(str, sys.version_info[:3])).encode("utf8")
),
python = PythonExecutable.fingerprinted(
sys.executable, ".".join(map(str, sys.version_info[:3])).encode("utf8")
)

plugins_pex = await Get(
Expand Down

0 comments on commit 5b05b42

Please sign in to comment.