Skip to content

Commit

Permalink
Rip out more parts of the legacy parser (#21643)
Browse files Browse the repository at this point in the history
  • Loading branch information
benjyw authored Nov 14, 2024
1 parent 35b1b7f commit c24b407
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 317 deletions.
3 changes: 0 additions & 3 deletions src/python/pants/help/help_formatter_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

from pants.help.help_formatter import HelpFormatter
from pants.help.help_info_extracter import HelpInfoExtracter, OptionHelpInfo
from pants.option.config import Config
from pants.option.global_options import GlobalOptions
from pants.option.native_options import NativeOptionParser
from pants.option.parser import OptionValueHistory, Parser
Expand Down Expand Up @@ -61,8 +60,6 @@ def test_format_help_choices(self) -> None:
@classmethod
def _get_parser(cls) -> Tuple[Parser, NativeOptionParser]:
return Parser(
env={},
config=Config.load([]),
scope_info=GlobalOptions.get_scope_info(),
), NativeOptionParser(
args=[], env={}, config_sources=[], allow_pantsrc=False, include_derivation=True
Expand Down
22 changes: 17 additions & 5 deletions src/python/pants/help/help_info_extracter.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import inspect
import itertools
import json
import re
from collections import defaultdict, namedtuple
from dataclasses import dataclass
from enum import Enum
Expand Down Expand Up @@ -41,17 +42,19 @@
from pants.engine.rules import Rule, TaskRule
from pants.engine.target import Field, RegisteredTargetTypes, StringField, Target, TargetGenerator
from pants.engine.unions import UnionMembership, UnionRule, is_union
from pants.option.native_options import NativeOptionParser
from pants.option.native_options import NativeOptionParser, parse_dest
from pants.option.option_util import is_dict_option, is_list_option
from pants.option.options import Options
from pants.option.parser import OptionValueHistory, Parser
from pants.option.ranked_value import Rank, RankedValue
from pants.option.scope import ScopeInfo
from pants.option.scope import GLOBAL_SCOPE, ScopeInfo
from pants.util.frozendict import LazyFrozenDict
from pants.util.strutil import first_paragraph, strval

T = TypeVar("T")

_ENV_SANITIZER_RE = re.compile(r"[.-]")


class HelpJSONEncoder(json.JSONEncoder):
"""Class for JSON-encoding help data (including option values).
Expand Down Expand Up @@ -1106,9 +1109,18 @@ def get_option_help_info(self, args, kwargs):
removal_hint = kwargs.get("removal_hint")
choices = self.compute_choices(kwargs)

dest = Parser.parse_dest(*args, **kwargs)
# Global options have three env var variants. The last one is the most human-friendly.
env_var = Parser.get_env_var_names(self._scope, dest)[-1]
dest = parse_dest(*args, **kwargs)
udest = dest.upper()
if self._scope == GLOBAL_SCOPE:
# Global options have 2-3 env var variants, e.g., --pants-workdir can be
# set with PANTS_GLOBAL_PANTS_WORKDIR, PANTS_PANTS_WORKDIR, or PANTS_WORKDIR.
# The last one is the most human-friendly, so it's what we use in the help info.
if udest.startswith("PANTS_"):
env_var = udest
else:
env_var = f"PANTS_{udest}"
else:
env_var = f"PANTS_{_ENV_SANITIZER_RE.sub('_', self._scope.upper())}_{udest}"

target_field_name = f"{self._scope_prefix}_{option_field_name_for(args)}".replace("-", "_")
environment_aware = kwargs.get("environment_aware") is True
Expand Down
4 changes: 0 additions & 4 deletions src/python/pants/help/help_info_extracter_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,6 @@ def do_test(args, kwargs, expected_default_str):
# Defaults are computed in the parser and added into the kwargs, so we
# must jump through this hoop in this test.
parser = Parser(
env={},
config=Config.load([]),
scope_info=GlobalOptions.get_scope_info(),
)
native_parser = NativeOptionParser([], {}, [], allow_pantsrc=False, include_derivation=True)
Expand Down Expand Up @@ -201,8 +199,6 @@ def exp_to_len(exp):
return int(exp) # True -> 1, False -> 0.

parser = Parser(
env={},
config=Config.load([]),
scope_info=GlobalOptions.get_scope_info(),
)
native_parser = NativeOptionParser([], {}, [], allow_pantsrc=False, include_derivation=True)
Expand Down
3 changes: 0 additions & 3 deletions src/python/pants/help/help_tools_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
from pants.help.help_info_extracter import HelpInfoExtracter
from pants.help.help_tools import ToolHelpInfo
from pants.help.maybe_color import MaybeColor
from pants.option.config import Config
from pants.option.global_options import GlobalOptions
from pants.option.native_options import NativeOptionParser
from pants.option.parser import Parser
Expand All @@ -18,8 +17,6 @@
@pytest.fixture
def parser() -> Parser:
return Parser(
env={},
config=Config.load([]),
scope_info=GlobalOptions.get_scope_info(),
)

Expand Down
2 changes: 1 addition & 1 deletion src/python/pants/option/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ def create(
[line for line in [line.strip() for line in f] if line]
)

parser_by_scope = {si.scope: Parser(env, config, si) for si in complete_known_scope_infos}
parser_by_scope = {si.scope: Parser(si) for si in complete_known_scope_infos}
known_scope_to_info = {s.scope: s for s in complete_known_scope_infos}

config_to_pass = None if native_options_config_discovery else config.sources()
Expand Down
6 changes: 3 additions & 3 deletions src/python/pants/option/options_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@
UnknownFlagsError,
)
from pants.option.global_options import GlobalOptions
from pants.option.native_options import parse_dest
from pants.option.option_types import StrOption
from pants.option.options import Options
from pants.option.options_bootstrapper import OptionsBootstrapper
from pants.option.options_fingerprinter import OptionEncoder
from pants.option.parser import Parser
from pants.option.ranked_value import Rank, RankedValue
from pants.option.scope import GLOBAL_SCOPE, ScopeInfo
from pants.option.subsystem import Subsystem
Expand Down Expand Up @@ -1009,8 +1009,8 @@ def test_choices() -> None:


def test_parse_dest() -> None:
assert "thing" == Parser.parse_dest("--thing")
assert "other_thing" == Parser.parse_dest("--thing", dest="other_thing")
assert "thing" == parse_dest("--thing")
assert "other_thing" == parse_dest("--thing", dest="other_thing")


def test_validation() -> None:
Expand Down
Loading

0 comments on commit c24b407

Please sign in to comment.