diff --git a/pex/third_party/__init__.py b/pex/third_party/__init__.py index 39e7df9f4..4403b0ab2 100644 --- a/pex/third_party/__init__.py +++ b/pex/third_party/__init__.py @@ -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] ) ) @@ -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( @@ -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]] diff --git a/pex/vendor/__init__.py b/pex/vendor/__init__.py index 244d30b6d..1eb8a9e07 100644 --- a/pex/vendor/__init__.py +++ b/pex/vendor/__init__.py @@ -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 @@ -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. @@ -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.