MCPcopy
hub / github.com/pantsbuild/pants / RankedValue

Class RankedValue

src/python/pants/option/ranked_value.py:38–96  ·  view source on GitHub ↗

An option value, together with a rank inferred from its source. Allows us to control which source wins: e.g., a command-line flag overrides an environment variable which overrides a config, etc. For example: Consider this config: [compile.java] foo: 11 And this envi

Source from the content-addressed store, hash-verified

36
37@dataclass(frozen=True)
38class RankedValue:
39 """An option value, together with a rank inferred from its source.
40
41 Allows us to control which source wins: e.g., a command-line flag overrides an environment
42 variable which overrides a config, etc. For example:
43
44 Consider this config:
45
46 [compile.java]
47 foo: 11
48
49 And this environment variable:
50
51 PANTS_COMPILE_FOO: 22
52
53 If the command-line is
54
55 ./pants compile target
56
57 we expect the value of foo in the compile.java scope to be 22, because it was explicitly
58 set by the user in the enclosing compile scope. I.e., the outer scope's environment value
59 overrides the inner scope's config value.
60
61 However if the command-line is
62
63 ./pants compile.java --foo=33 target
64
65 we now expect the value of foo in the compile.java to be 33. I.e., the inner scope's flag
66 overrides the outer scope's environment value.
67
68 To tell these cases apart we need to know the "ranking" of the value.
69 """
70
71 @classmethod
72 def prioritized_iter(
73 cls,
74 flag_val: Value,
75 env_val: Value,
76 config_val: Value,
77 config_default_val: Value,
78 hardcoded_val: Value,
79 default: Value,
80 ) -> Iterator["RankedValue"]:
81 """Yield the non-None values from highest-ranked to lowest, wrapped in RankedValue
82 instances."""
83 if flag_val is not None:
84 yield RankedValue(Rank.FLAG, flag_val)
85 if env_val is not None:
86 yield RankedValue(Rank.ENVIRONMENT, env_val)
87 if config_val is not None:
88 yield RankedValue(Rank.CONFIG, config_val)
89 if config_default_val is not None:
90 yield RankedValue(Rank.CONFIG_DEFAULT, config_default_val)
91 if hardcoded_val is not None:
92 yield RankedValue(Rank.HARDCODED, hardcoded_val)
93 yield RankedValue(Rank.NONE, default)
94
95 rank: Rank

Callers 15

executeMethod · 0.90
test_is_python2Function · 0.90
test_is_not_python2Function · 0.90
_create_scoped_optionsFunction · 0.90
registerFunction · 0.90
test_unknown_valuesMethod · 0.90
test_value_rankingMethod · 0.90
test_is_flaggedMethod · 0.90
test_indexingMethod · 0.90
test_iteratorMethod · 0.90
test_copyMethod · 0.90

Calls

no outgoing calls

Tested by 15

test_is_python2Function · 0.72
test_is_not_python2Function · 0.72
test_unknown_valuesMethod · 0.72
test_value_rankingMethod · 0.72
test_is_flaggedMethod · 0.72
test_indexingMethod · 0.72
test_iteratorMethod · 0.72
test_copyMethod · 0.72
test_deepcopyMethod · 0.72
setUpMethod · 0.72