Repository: pantsbuild/pants

Issue:
The `poetry_requirements` macro does not handle the special `python` dependency.
Currently it just maps this to a requirement and that makes things go boom:

On the `experiment/pants` branch in https://github.com/Incognito/python-architecture-linter for example:
```console
$ PANTS_SHA=0182de9814c7694eed2b9407e3577684db26ea2e ./pants repl python_architecture_linter::
09:23:58.94 [WARN] The target python_architecture_linter/ast_validators/class_validators.py:../python_architecture_linter imports `astroid`, but Pants cannot safely infer a dependency because more than one target owns this module, so it is ambiguous which to use: ['modular_provider_architecture_definition:astroid', 'python_architecture_linter:astroid'].

Please explicitly include the dependency you want in the `dependencies` field of python_architecture_linter/ast_validators/class_validators.py:../python_architecture_linter, or ignore the ones you do not want by prefixing with `!` or `!!` so that one or no targets are left.

Alternatively, you can remove the ambiguity by deleting/changing some of the targets so that only 1 target owns this module. Refer to (https://www.pantsbuild.org/v2.6/docs/troubleshooting#import-errors-and-missing-dependencies).
09:23:58.94 [WARN] The target python_architecture_linter/ast_validators/function_validators.py:../python_architecture_linter imports `astroid`, but Pants cannot safely infer a dependency because more than one target owns this module, so it is ambiguous which to use: ['modular_provider_architecture_definition:astroid', 'python_architecture_linter:astroid'].

Please explicitly include the dependency you want in the `dependencies` field of python_architecture_linter/ast_validators/function_validators.py:../python_architecture_linter, or ignore the ones you do not want by prefixing with `!` or `!!` so that one or no targets are left.

Alternatively, you can remove the ambiguity by deleting/changing some of the targets so that only 1 target owns this module. Refer to (https://www.pantsbuild.org/v2.6/docs/troubleshooting#import-errors-and-missing-dependencies).
09:23:58.94 [WARN] The target python_architecture_linter/node_navigators.py imports `astroid`, but Pants cannot safely infer a dependency because more than one target owns this module, so it is ambiguous which to use: ['modular_provider_architecture_definition:astroid', 'python_architecture_linter:astroid'].

Please explicitly include the dependency you want in the `dependencies` field of python_architecture_linter/node_navigators.py, or ignore the ones you do not want by prefixing with `!` or `!!` so that one or no targets are left.

Alternatively, you can remove the ambiguity by deleting/changing some of the targets so that only 1 target owns this module. Refer to (https://www.pantsbuild.org/v2.6/docs/troubleshooting#import-errors-and-missing-dependencies).
09:23:58.94 [WARN] The target python_architecture_linter/ast_validators/nodeng_validator.py:../python_architecture_linter imports `astroid`, but Pants cannot safely infer a dependency because more than one target owns this module, so it is ambiguous which to use: ['modular_provider_architecture_definition:astroid', 'python_architecture_linter:astroid'].

Please explicitly include the dependency you want in the `dependencies` field of python_architecture_linter/ast_validators/nodeng_validator.py:../python_architecture_linter, or ignore the ones you do not want by prefixing with `!` or `!!` so that one or no targets are left.

Alternatively, you can remove the ambiguity by deleting/changing some of the targets so that only 1 target owns this module. Refer to (https://www.pantsbuild.org/v2.6/docs/troubleshooting#import-errors-and-missing-dependencies).
09:24:04.10 [INFO] Completed: Building requirements.pex with 9 requirements: astroid<3.0.0,>=2.5.8, black<22.0.0,>=21.6b0, coverage<6.0.0,>=5.5, flake8<4.0.0,>=3.9.2, isort<6.0.0,>=5.8.0, mypy<0.903.0,>=0.902, pytest-cov<3.0.0,>=2... (47 characters truncated)
09:24:04.10 [ERROR] Exception caught: (pants.engine.internals.scheduler.ExecutionError)
  File "/home/jsirois/.cache/pants/setup/bootstrap-Linux-x86_64/2.6.0rc0+git0182de98_py39/lib/python3.9/site-packages/pants/bin/local_pants_runner.py", line 234, in _run_inner
    return self._perform_run(goals)
  File "/home/jsirois/.cache/pants/setup/bootstrap-Linux-x86_64/2.6.0rc0+git0182de98_py39/lib/python3.9/site-packages/pants/bin/local_pants_runner.py", line 173, in _perform_run
    return self._perform_run_body(goals, poll=False)
  File "/home/jsirois/.cache/pants/setup/bootstrap-Linux-x86_64/2.6.0rc0+git0182de98_py39/lib/python3.9/site-packages/pants/bin/local_pants_runner.py", line 190, in _perform_run_body
    return self.graph_session.run_goal_rules(
  File "/home/jsirois/.cache/pants/setup/bootstrap-Linux-x86_64/2.6.0rc0+git0182de98_py39/lib/python3.9/site-packages/pants/init/engine_initializer.py", line 135, in run_goal_rules
    exit_code = self.scheduler_session.run_goal_rule(
  File "/home/jsirois/.cache/pants/setup/bootstrap-Linux-x86_64/2.6.0rc0+git0182de98_py39/lib/python3.9/site-packages/pants/engine/internals/scheduler.py", line 533, in run_goal_rule
    self._raise_on_error([t for _, t in throws])
  File "/home/jsirois/.cache/pants/setup/bootstrap-Linux-x86_64/2.6.0rc0+git0182de98_py39/lib/python3.9/site-packages/pants/engine/internals/scheduler.py", line 501, in _raise_on_error
    raise ExecutionError(

Exception message: 1 Exception encountered:

  ProcessExecutionFailure: Process 'Building requirements.pex with 9 requirements: astroid<3.0.0,>=2.5.8, black<22.0.0,>=21.6b0, coverage<6.0.0,>=5.5, flake8<4.0.0,>=3.9.2, isort<6.0.0,>=5.8.0, mypy<0.903.0,>=0.902, pytest-cov<3.0.0,>=2.12.1, pytest<7.0.0,>=6.2.4, python<4.0.0,>=3.9' failed with exit code 1.
stdout:

stderr:
ERROR: Could not find a version that satisfies the requirement python<4.0.0,>=3.9
ERROR: No matching distribution found for python<4.0.0,>=3.9
pid 20560 -> /home/jsirois/.cache/pants/named_caches/pex_root/venvs/8a893dcb01b3ee032bd99c2db556cfa3d015123d/832e95f85413646e8b7b056ab4e60414f83d7397/pex --disable-pip-version-check --no-python-version-warning --exists-action a --isolated -q --cache-dir /home/jsirois/.cache/pants/named_caches/pex_root --log /tmp/process-execution4WOjPI/.tmp/tmpeb9pu1nd/pip.log download --dest /tmp/process-execution4WOjPI/.tmp/tmpjrjd28_0/usr.bin.python3.9 astroid<3.0.0,>=2.5.8 black<22.0.0,>=21.6b0 coverage<6.0.0,>=5.5 flake8<4.0.0,>=3.9.2 isort<6.0.0,>=5.8.0 mypy<0.903.0,>=0.902 pytest-cov<3.0.0,>=2.12.1 pytest<7.0.0,>=6.2.4 python<4.0.0,>=3.9 --index-url https://pypi.org/simple/ --find-links https://binaries.pantsbuild.org/wheels/pantsbuild.pants/0182de9814c7694eed2b9407e3577684db26ea2e/2.6.0rc0%2Bgit0182de98/index.html --retries 5 --timeout 15 exited with 1 and STDERR:
None




(Use --print-stacktrace for more error details and/or --no-process-execution-local-cleanup to inspect chroots and/or -ldebug for more logs. See https://www.pantsbuild.org/v2.6/docs/troubleshooting for common issues. Consider reaching out for help: https://www.pantsbuild.org/v2.6/docs/getting-help.)
```


Discussion:

Resolving patch:
diff --git a/src/python/pants/backend/python/macros/poetry_requirements.py b/src/python/pants/backend/python/macros/poetry_requirements.py
--- a/src/python/pants/backend/python/macros/poetry_requirements.py
+++ b/src/python/pants/backend/python/macros/poetry_requirements.py
@@ -185,6 +185,11 @@ def parse_pyproject_toml(toml_contents: str, file_path: str) -> set[Requirement]
             )
         )
     dependencies = poetry_vals.get("dependencies", {})
+    # N.B.: The "python" dependency is a special dependency required by Poetry that only serves to
+    # constraint the python interpreter versions the project works with; so we skip that.
+    # See: https://python-poetry.org/docs/pyproject/#dependencies-and-dev-dependencies
+    dependencies.pop("python", None)
+
     dev_dependencies = poetry_vals.get("dev-dependencies", {})
     if not dependencies and not dev_dependencies:
         logger.warning(


Test patch:
diff --git a/src/python/pants/backend/python/macros/poetry_requirements_test.py b/src/python/pants/backend/python/macros/poetry_requirements_test.py
--- a/src/python/pants/backend/python/macros/poetry_requirements_test.py
+++ b/src/python/pants/backend/python/macros/poetry_requirements_test.py
@@ -183,7 +183,6 @@ def test_parse_multi_reqs() -> None:
     """
     retval = parse_pyproject_toml(toml_str, "/path/to/file")
     actual_reqs = {
-        Requirement.parse("python<4.0.0,>=3.8"),
         Requirement.parse("junk@ https://github.com/myrepo/junk.whl"),
         Requirement.parse("poetry@ git+https://github.com/python-poetry/poetry.git@v1.1.1"),
         Requirement.parse('requests<3.0.0,>=2.25.1; python_version > "2.7"'),