Skip to content

Commit

Permalink
Eliminate ugly interface TODO.
Browse files Browse the repository at this point in the history
  • Loading branch information
jsirois committed Dec 2, 2023
1 parent 004d728 commit 2f520de
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 16 deletions.
6 changes: 3 additions & 3 deletions pex/third_party/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ def _vendored_path_items():
for spec in vendor.iter_vendor_specs(
# N.B.: The VendorImporter should only see the versions of vendored projects that
# support the current Python interpreter.
filter_requires_python=True
filter_requires_python=sys.version_info[:2]
)
)

Expand Down Expand Up @@ -280,7 +280,7 @@ def expose(

def iter_available():
yield "pex", root # The pex distribution itself is trivially available to expose.
for spec in vendor.iter_vendor_specs(interpreter=interpreter):
for spec in vendor.iter_vendor_specs(filter_requires_python=interpreter):
yield spec.key, spec.relpath

path_by_key = OrderedDict(
Expand Down Expand Up @@ -473,7 +473,7 @@ def isolated(interpreter=None):
# PEX_ROOT or built PEXs.
vendor_lockfiles = tuple(
os.path.join(os.path.relpath(vendor_spec.relpath, module), "constraints.txt")
for vendor_spec in vendor.iter_vendor_specs(interpreter=interpreter)
for vendor_spec in vendor.iter_vendor_specs(filter_requires_python=interpreter)
)

pex_zip_paths = None # type: Optional[Tuple[str, str]]
Expand Down
29 changes: 16 additions & 13 deletions pex/vendor/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from pex.typing import TYPE_CHECKING

if TYPE_CHECKING:
from typing import Iterable, Iterator, Optional, Sequence, Set, Text, Tuple
from typing import Iterable, Iterator, Optional, Sequence, Set, Text, Tuple, Union

from pex.interpreter import PythonInterpreter

Expand Down Expand Up @@ -158,15 +158,22 @@ def create_packages(self):
touch(os.path.join(self.ROOT, *relpath))


def iter_vendor_specs(
filter_requires_python=False, # type: bool
interpreter=None, # type: Optional[PythonInterpreter]
):
# type: (...) -> Iterator[VendorSpec]
def iter_vendor_specs(filter_requires_python=None):
# type: (Optional[Union[Tuple[int, int], PythonInterpreter]]) -> Iterator[VendorSpec]
"""Iterate specifications for code vendored by pex.
:param filter_requires_python: An optional interpreter (or its major and minor version) to
tailor the vendor specs to.
:return: An iterator over specs of all vendored code.
"""
python_major_minor = None # type: Optional[Tuple[int, int]]
if filter_requires_python:
python_major_minor = (
filter_requires_python
if isinstance(filter_requires_python, tuple)
else filter_requires_python.version[:2]
)

# We use this for a better @dataclass that is also Python2.7 and PyPy compatible.
# N.B.: The `[testenv:typecheck]` section in `tox.ini` should have its deps list updated to
# reflect this attrs version.
Expand All @@ -180,22 +187,18 @@ def iter_vendor_specs(

# We use this via pex.third_party at runtime to check for compatible wheel tags and at build
# time to implement resolving distributions from a PEX repository.
# TODO(John Sirois): XXX: filter_requires_python = False + interpreter non None is a bit
# contradictory.
filter_requires_python = filter_requires_python or interpreter is not None
python_major_minor = interpreter.version[:2] if interpreter else sys.version_info[:2]
if not filter_requires_python or python_major_minor < (3, 6):
if not python_major_minor or python_major_minor < (3, 6):
# N.B.: The pyparsing constraint is needed for 2.7 support.
yield VendorSpec.pinned(
"packaging", "20.9", import_path="packaging_20_9", constraints=("pyparsing<3",)
)
if not filter_requires_python or python_major_minor == (3, 6):
if not python_major_minor or python_major_minor == (3, 6):
# N.B.: The pyparsing constraint is needed because our import re-writer (RedBaron) chokes on
# newer versions.
yield VendorSpec.pinned(
"packaging", "21.3", import_path="packaging_21_3", constraints=("pyparsing<3",)
)
if not filter_requires_python or python_major_minor >= (3, 7):
if not python_major_minor or python_major_minor >= (3, 7):
yield VendorSpec.pinned("packaging", "23.1", import_path="packaging_23_1")

# We use toml to read pyproject.toml when building sdists from local source projects.
Expand Down

0 comments on commit 2f520de

Please sign in to comment.