• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

pantsbuild / pants / 22285099215

22 Feb 2026 08:52PM UTC coverage: 75.854% (-17.1%) from 92.936%
22285099215

Pull #23121

github

web-flow
Merge c7299df9c into ba8359840
Pull Request #23121: fix issue with optional fields in dependency validator

28 of 29 new or added lines in 2 files covered. (96.55%)

11174 existing lines in 400 files now uncovered.

53694 of 70786 relevant lines covered (75.85%)

1.88 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

65.0
/src/python/pants/backend/codegen/utils.py
1
# Copyright 2022 Pants project contributors (see CONTRIBUTORS.md).
2
# Licensed under the Apache License, Version 2.0 (see LICENSE).
3

4
from __future__ import annotations
2✔
5

6
from pants.backend.python.dependency_inference.module_mapper import PythonModuleOwners
2✔
7
from pants.engine.addresses import Address
2✔
8
from pants.util.docutil import doc_url
2✔
9
from pants.util.strutil import softwrap
2✔
10

11

12
class MissingPythonCodegenRuntimeLibrary(Exception):
2✔
13
    pass
2✔
14

15

16
class AmbiguousPythonCodegenRuntimeLibrary(Exception):
2✔
17
    pass
2✔
18

19

20
def find_python_runtime_library_or_raise_error(
2✔
21
    module_owners: PythonModuleOwners,
22
    codegen_address: Address,
23
    runtime_library_module: str,
24
    *,
25
    resolve: str,
26
    resolves_enabled: bool,
27
    recommended_requirement_name: str,
28
    recommended_requirement_url: str,
29
    disable_inference_option: str,
30
) -> Address:
31
    addresses = module_owners.unambiguous
1✔
32
    if module_owners.unambiguous:
1✔
33
        return module_owners.unambiguous[0]
1✔
34

UNCOV
35
    addresses = module_owners.ambiguous
×
36

UNCOV
37
    for_resolve_str = f" for the resolve '{resolve}'" if resolves_enabled else ""
×
UNCOV
38
    if not addresses:
×
UNCOV
39
        resolve_note = softwrap(
×
40
            (
41
                f"""
42
                Note that because `[python].enable_resolves` is set, you must specifically have a
43
                `python_requirement` target that uses the same resolve '{resolve}' as the target
44
                {codegen_address}. Alternatively, update {codegen_address} to use a different
45
                resolve.
46
                """
47
            )
48
            if resolves_enabled
49
            else ""
50
        )
UNCOV
51
        raise MissingPythonCodegenRuntimeLibrary(
×
52
            softwrap(
53
                f"""
54
                No `python_requirement` target was found with the module `{runtime_library_module}`
55
                in your project{for_resolve_str}, so the Python code generated from the target
56
                {codegen_address} will not work properly. See
57
                {doc_url("docs/python/overview/third-party-dependencies")} for how to add a requirement, such as
58
                adding to requirements.txt. Usually you will want to use the
59
                `{recommended_requirement_name}` project at {recommended_requirement_url}.
60

61
                {resolve_note}
62

63
                To ignore this error, set `{disable_inference_option} = false` in `pants.toml`.
64
                """
65
            )
66
        )
67

UNCOV
68
    alternative_solution = softwrap(
×
69
        (
70
            f"""
71
            Alternatively, change the resolve field for {codegen_address} to use a different resolve
72
            from `[python].resolves`.
73
            """
74
        )
75
        if resolves_enabled
76
        else (
77
            f"""
78
            Alternatively, if you do want to have multiple conflicting versions of the
79
            `{runtime_library_module}` requirement, set `{disable_inference_option} = false` in
80
            `pants.toml`. Then manually add a dependency on the relevant `python_requirement` target
81
            to each target that directly depends on this generated code (e.g. `python_source` targets).
82
            """
83
        )
84
    )
UNCOV
85
    raise AmbiguousPythonCodegenRuntimeLibrary(
×
86
        softwrap(
87
            f"""
88
            Multiple `python_requirement` targets were found with the module `{runtime_library_module}`
89
            in your project{for_resolve_str}, so it is ambiguous which to use for the runtime library
90
            for the Python code generated from the target {codegen_address}:
91
            {sorted(addr.spec for addr in addresses)}
92

93
            To fix, remove one of these `python_requirement` targets{for_resolve_str} so that
94
            there is no ambiguity and Pants can infer a dependency. It
95
            might also help to set
96
            `[python-infer].ambiguity-resolution = "by_source_root"`. {alternative_solution}
97
            """
98
        )
99
    )
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc