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

Rip out more parts of the legacy parser #21643

Merged
merged 2 commits into from
Nov 14, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
18 changes: 14 additions & 4 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"[.-]")
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file is now the only place using this, so it moved here from parser.py



class HelpJSONEncoder(json.JSONEncoder):
"""Class for JSON-encoding help data (including option values).
Expand Down Expand Up @@ -1106,9 +1109,16 @@ 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)
dest = parse_dest(*args, **kwargs)
# Global options have three env var variants. The last one is the most human-friendly.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# Global options have three env var variants. The last one is the most human-friendly.
# Global options have three env var variants
# (eg. PANTS_GLOBAL_PANTS_WORKDIR, PANTS_PANTS_WORKDIR, PANTS_WORKDIR).
# The last one is the most human-friendly.

maybe port these from the comment since its being deleted

env_var = Parser.get_env_var_names(self._scope, dest)[-1]
udest = dest.upper()
if self._scope == GLOBAL_SCOPE:
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is (simplified) logic moved from parser.py.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for my information: we take only the last element, so we can discard the logic that computes the other values

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
Loading