Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor PythonSetup.interpreter_or_constraints() to take a compatibility value rather than a PythonTarget #7691

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/python/pants/backend/python/interpreter_cache.py
Expand Up @@ -71,7 +71,7 @@ def partition_targets_by_compatibility(self, targets):

for target in targets:
if isinstance(target, PythonTarget):
c = self.python_setup.compatibility_or_constraints(target)
c = self.python_setup.compatibility_or_constraints(target.compatibility)
tgts_by_compatibilities[c].append(target)
filters.update(c)
return tgts_by_compatibilities, filters
Expand Down
6 changes: 4 additions & 2 deletions src/python/pants/backend/python/subsystems/pex_build_util.py
Expand Up @@ -360,8 +360,10 @@ def add_interpreter_constraints_from(self, constraint_tgts):
# TODO this would be a great place to validate the constraints and present a good error message
# if they are incompatible because all the sources of the constraints are available.
# See: https://github.com/pantsbuild/pex/blob/584b6e367939d24bc28aa9fa36eb911c8297dac8/pex/interpreter_constraints.py
constraint_tuples = {self._python_setup_subsystem.compatibility_or_constraints(tgt)
for tgt in constraint_tgts}
constraint_tuples = {
self._python_setup_subsystem.compatibility_or_constraints(tgt.compatibility)
for tgt in constraint_tgts
}
for constraint_tuple in constraint_tuples:
for constraint in constraint_tuple:
self.add_interpreter_constraint(constraint)
Expand Down
10 changes: 6 additions & 4 deletions src/python/pants/backend/python/subsystems/python_setup.py
Expand Up @@ -122,14 +122,16 @@ def artifact_cache_dir(self):
def scratch_dir(self):
return os.path.join(self.get_options().pants_workdir, *self.options_scope.split('.'))

def compatibility_or_constraints(self, target):
def compatibility_or_constraints(self, compatibility):
"""
Return either the compatibility of the given target, or the interpreter constraints.
If interpreter constraints are supplied by the CLI flag, return those only.
Return either the given compatibility, or the interpreter constraints. If interpreter
constraints are supplied by the CLI flag, return those only.

:param compatibility: Optional[List[str]], e.g. None or ['CPython>3'].
"""
if self.get_options().is_flagged('interpreter_constraints'):
return tuple(self.interpreter_constraints)
return tuple(target.compatibility or self.interpreter_constraints)
return tuple(compatibility or self.interpreter_constraints)

@classmethod
def expand_interpreter_search_paths(cls, interpreter_search_paths, pyenv_root_func=None):
Expand Down
Expand Up @@ -139,7 +139,7 @@ def create_pex(self, pex_info=None, pin_selected_interpreter=False):
else:
constraints = {
constraint for rt in relevant_targets if is_python_target(rt)
for constraint in PythonSetup.global_instance().compatibility_or_constraints(rt)
for constraint in PythonSetup.global_instance().compatibility_or_constraints(rt.compatibility)
}
self.context.log.debug('target set {} has constraints: {}'
.format(relevant_targets, constraints))
Expand Down
4 changes: 3 additions & 1 deletion src/python/pants/backend/python/tasks/select_interpreter.py
Expand Up @@ -29,7 +29,9 @@ def __init__(self, python_setup):
def compute_fingerprint(self, python_target):
# Consider the target's compatibility requirements, and if those are missing then fall back
# to the global interpreter constraints. Only these two values can affect the selected interpreter.
hash_elements_for_target = sorted(self.python_setup.compatibility_or_constraints(python_target))
hash_elements_for_target = sorted(
self.python_setup.compatibility_or_constraints(python_target.compatibility)
)
if not hash_elements_for_target:
return None
hasher = hashlib.sha1()
Expand Down
2 changes: 1 addition & 1 deletion src/python/pants/backend/python/tasks/unpack_wheels.py
Expand Up @@ -69,7 +69,7 @@ def _get_matching_wheel(self, pex_path, interpreter, requirements, module_name):

@memoized_method
def _compatible_interpreter(self, unpacked_whls):
constraints = PythonSetup.global_instance().compatibility_or_constraints(unpacked_whls)
constraints = PythonSetup.global_instance().compatibility_or_constraints(unpacked_whls.compatibility)
allowable_interpreters = PythonInterpreterCache.global_instance().setup(filters=constraints)
return min(allowable_interpreters)

Expand Down