Skip to content

Commit

Permalink
[internal] Run pyupgrade src/python/pants/[util,options,testutil] (#1…
Browse files Browse the repository at this point in the history
…3107)

doing this in small batches to minimize interruption (i.e. other people need to rebase/deal with conflicts)
  • Loading branch information
asherf authored Oct 5, 2021
1 parent 04360e9 commit 7329d6d
Show file tree
Hide file tree
Showing 27 changed files with 136 additions and 151 deletions.
2 changes: 1 addition & 1 deletion src/python/pants/option/arg_splitter.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ def __init__(self, known_scope_infos: Iterable[ScopeInfo], buildroot: str) -> No

# We check for prefixes in reverse order, so we match the longest prefix first.
sorted_scope_infos = sorted(
[si for si in self._known_scope_infos if si.scope],
(si for si in self._known_scope_infos if si.scope),
key=lambda si: si.scope,
reverse=True,
)
Expand Down
22 changes: 11 additions & 11 deletions src/python/pants/option/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from dataclasses import dataclass
from functools import partial
from hashlib import sha1
from typing import Any, ClassVar, Dict, Iterable, List, Mapping, Sequence, Tuple, Union, cast
from typing import Any, ClassVar, Dict, Iterable, List, Mapping, Sequence, Union, cast

import toml
from typing_extensions import Protocol
Expand Down Expand Up @@ -96,7 +96,7 @@ def load(

@classmethod
def _parse_toml(
cls, config_content: str, normalized_seed_values: Dict[str, str]
cls, config_content: str, normalized_seed_values: dict[str, str]
) -> _ConfigValues:
"""Attempt to parse as TOML, raising an exception on failure."""
toml_values = cast(Dict[str, Any], toml.loads(config_content))
Expand All @@ -107,15 +107,15 @@ def _parse_toml(
return _ConfigValues(toml_values)

@staticmethod
def _determine_seed_values(*, seed_values: SeedValues | None = None) -> Dict[str, str]:
def _determine_seed_values(*, seed_values: SeedValues | None = None) -> dict[str, str]:
"""We pre-populate several default values to allow %([key-name])s interpolation.
This sets up those defaults and checks if the user overrode any of the values.
"""
safe_seed_values = seed_values or {}
buildroot = cast(str, safe_seed_values.get("buildroot", get_buildroot()))

all_seed_values: Dict[str, str] = {
all_seed_values: dict[str, str] = {
"buildroot": buildroot,
"homedir": os.path.expanduser("~"),
"user": getpass.getuser(),
Expand Down Expand Up @@ -152,15 +152,15 @@ def get(self, section, option, type_=str, default=None):
)

@abstractmethod
def configs(self) -> Sequence["_SingleFileConfig"]:
def configs(self) -> Sequence[_SingleFileConfig]:
"""Returns the underlying single-file configs represented by this object."""

@abstractmethod
def sources(self) -> List[str]:
def sources(self) -> list[str]:
"""Returns the sources of this config as a list of filenames."""

@abstractmethod
def sections(self) -> List[str]:
def sections(self) -> list[str]:
"""Returns the sections in this config (not including DEFAULT)."""

@abstractmethod
Expand Down Expand Up @@ -194,7 +194,7 @@ def get_source_for_option(self, section: str, option: str) -> str | None:
class _ConfigValues:
"""The parsed contents of a TOML config file."""

values: Dict[str, Any]
values: dict[str, Any]

@staticmethod
def _is_an_option(option_value: _TomlValue | dict) -> bool:
Expand All @@ -212,7 +212,7 @@ def _possibly_interpolate_value(
*,
option: str,
section: str,
section_values: Dict,
section_values: dict,
) -> str:
"""For any values with %(foo)s, substitute it with the corresponding value from DEFAULT or
the same section."""
Expand Down Expand Up @@ -254,7 +254,7 @@ def _stringify_val(
*,
option: str,
section: str,
section_values: Dict,
section_values: dict,
interpolate: bool = True,
list_prefix: str | None = None,
) -> str:
Expand Down Expand Up @@ -489,7 +489,7 @@ class TomlSerializer:
parsed: Mapping[str, dict[str, int | float | str | bool | list | dict]]

def normalize(self) -> dict:
def normalize_section_value(option, option_value) -> Tuple[str, Any]:
def normalize_section_value(option, option_value) -> tuple[str, Any]:
# With TOML, we store dict values as strings (for now).
if isinstance(option_value, dict):
option_value = str(option_value)
Expand Down
14 changes: 7 additions & 7 deletions src/python/pants/option/custom_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import re
import shlex
from enum import Enum
from typing import Dict, Iterable, List, Pattern, Sequence
from typing import Iterable, Pattern, Sequence

from pants.option.errors import ParseError
from pants.util.eval import parse_expression
Expand Down Expand Up @@ -165,7 +165,7 @@ def _convert_list(val, member_type, is_enum):
return [item if isinstance(item, member_type) else member_type(item) for item in converted]


def _flatten_shlexed_list(shlexed_args: Sequence[str]) -> List[str]:
def _flatten_shlexed_list(shlexed_args: Sequence[str]) -> list[str]:
"""Convert a list of shlexed args into a flattened list of individual args.
For example, ['arg1 arg2=foo', '--arg3'] would be converted to ['arg1', 'arg2=foo', '--arg3'].
Expand Down Expand Up @@ -203,7 +203,7 @@ def _get_modifier_expr_re(cls) -> Pattern[str]:
return re.compile(r"(?<=\]|\))\s*,\s*(?=[+-](?:\[|\())")

@classmethod
def _split_modifier_expr(cls, s: str) -> List[str]:
def _split_modifier_expr(cls, s: str) -> list[str]:
# This check ensures that the first expression (before the first split point) is a modification.
if s.startswith("+") or s.startswith("-"):
return cls._get_modifier_expr_re().split(s)
Expand Down Expand Up @@ -232,13 +232,13 @@ def merge(cls, components: Iterable[ListValueComponent]) -> ListValueComponent:
raise ParseError(f"Unknown action for list value: {component._action}")
return cls(action, appends, filters)

def __init__(self, action: str, appends: List, filters: List) -> None:
def __init__(self, action: str, appends: list, filters: list) -> None:
self._action = action
self._appends = appends
self._filters = filters

@property
def val(self) -> List:
def val(self) -> list:
ret = list(self._appends)
for x in self._filters:
# Note: can't do ret.remove(x) because that only removes the first instance of x.
Expand Down Expand Up @@ -315,7 +315,7 @@ class DictValueComponent:
EXTEND = "EXTEND"

@classmethod
def merge(cls, components: Iterable["DictValueComponent"]) -> DictValueComponent:
def merge(cls, components: Iterable[DictValueComponent]) -> DictValueComponent:
"""Merges components into a single component, applying their actions appropriately.
This operation is associative: M(M(a, b), c) == M(a, M(b, c)) == M(a, b, c).
Expand All @@ -334,7 +334,7 @@ def merge(cls, components: Iterable["DictValueComponent"]) -> DictValueComponent
raise ParseError(f"Unknown action for dict value: {component.action}")
return cls(action, val)

def __init__(self, action: str, val: Dict) -> None:
def __init__(self, action: str, val: dict) -> None:
self.action = action
self.val = val

Expand Down
8 changes: 4 additions & 4 deletions src/python/pants/option/global_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from datetime import datetime
from enum import Enum
from pathlib import Path
from typing import Any, Dict, List, Tuple, cast
from typing import Any, cast

from pants.base.build_environment import (
get_buildroot,
Expand Down Expand Up @@ -334,8 +334,8 @@ class ExecutionOptions:
remote_cache_rpc_concurrency: int

remote_execution_address: str | None
remote_execution_extra_platform_properties: List[str]
remote_execution_headers: Dict[str, str]
remote_execution_extra_platform_properties: list[str]
remote_execution_headers: dict[str, str]
remote_execution_overall_deadline_secs: int
remote_execution_rpc_concurrency: int

Expand Down Expand Up @@ -1556,7 +1556,7 @@ def add(absolute_path, include=False):
@staticmethod
def compute_pantsd_invalidation_globs(
buildroot: str, bootstrap_options: OptionValueContainer
) -> Tuple[str, ...]:
) -> tuple[str, ...]:
"""Computes the merged value of the `--pantsd-invalidation-globs` option.
Combines --pythonpath and --pants-config-files files that are in {buildroot} dir with those
Expand Down
19 changes: 9 additions & 10 deletions src/python/pants/option/option_value_container.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@

import copy
from dataclasses import dataclass
from typing import Dict, Iterator, List, Optional
from typing import Iterator

from pants.option.ranked_value import Rank, RankedValue, Value

Key = str


class OptionValueContainerBuilder:
def __init__(self, value_map: Optional[Dict[Key, RankedValue]] = None) -> None:
self._value_map: Dict[Key, RankedValue] = value_map if value_map else {}
def __init__(self, value_map: dict[Key, RankedValue] | None = None) -> None:
self._value_map: dict[Key, RankedValue] = value_map if value_map else {}

def update(self, other: "OptionValueContainerBuilder") -> None:
def update(self, other: OptionValueContainerBuilder) -> None:
"""Set other's values onto this object.
For each key, highest ranked value wins. In a tie, other's value wins.
Expand Down Expand Up @@ -60,9 +60,9 @@ class OptionValueContainer:
See ranked_value.py for more details.
"""

_value_map: Dict[Key, RankedValue]
_value_map: dict[Key, RankedValue]

def get_explicit_keys(self) -> List[Key]:
def get_explicit_keys(self) -> list[Key]:
"""Returns the keys for any values that were set explicitly (via flag, config, or env
var)."""
ret = []
Expand Down Expand Up @@ -108,13 +108,13 @@ def is_default(self, key: Key) -> bool:
"""
return self.get_rank(key) in (Rank.NONE, Rank.HARDCODED)

def get(self, key: Key, default: Optional[Value] = None):
def get(self, key: Key, default: Value | None = None):
# Support dict-like dynamic access. See also __getitem__ below.
if key not in self._value_map:
return default
return self._get_underlying_value(key)

def as_dict(self) -> Dict[Key, Value]:
def as_dict(self) -> dict[Key, Value]:
return {key: self.get(key) for key in self._value_map}

def to_builder(self) -> OptionValueContainerBuilder:
Expand Down Expand Up @@ -143,5 +143,4 @@ def __getattr__(self, key: Key):

def __iter__(self) -> Iterator[Key]:
"""Returns an iterator over all option names, in lexicographical order."""
for name in sorted(self._value_map.keys()):
yield name
yield from sorted(self._value_map.keys())
36 changes: 18 additions & 18 deletions src/python/pants/option/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import dataclasses
import logging
from typing import Dict, Iterable, List, Mapping, Optional, Sequence
from typing import Iterable, Mapping, Sequence

from pants.base.build_environment import get_buildroot
from pants.base.deprecated import warn_or_error
Expand Down Expand Up @@ -79,7 +79,7 @@ def complete_scopes(cls, scope_infos: Iterable[ScopeInfo]) -> FrozenOrderedSet[S
Also validates that scopes do not collide.
"""
ret: OrderedSet[ScopeInfo] = OrderedSet()
original_scopes: Dict[str, ScopeInfo] = {}
original_scopes: dict[str, ScopeInfo] = {}
for si in sorted(scope_infos, key=lambda _si: _si.scope):
if si.scope in original_scopes:
raise cls.DuplicateScopeError(
Expand All @@ -100,7 +100,7 @@ def create(
config: Config,
known_scope_infos: Iterable[ScopeInfo],
args: Sequence[str],
bootstrap_option_values: Optional[OptionValueContainer] = None,
bootstrap_option_values: OptionValueContainer | None = None,
allow_unknown_options: bool = False,
) -> Options:
"""Create an Options instance.
Expand Down Expand Up @@ -131,7 +131,7 @@ def create(
spec_files = bootstrap_option_values.spec_files
if spec_files:
for spec_file in spec_files:
with open(spec_file, "r") as f:
with open(spec_file) as f:
split_args.specs.extend(
[line for line in [line.strip() for line in f] if line]
)
Expand All @@ -154,14 +154,14 @@ def create(

def __init__(
self,
goals: List[str],
scope_to_flags: Dict[str, List[str]],
specs: List[str],
passthru: List[str],
help_request: Optional[HelpRequest],
parser_by_scope: Dict[str, Parser],
bootstrap_option_values: Optional[OptionValueContainer],
known_scope_to_info: Dict[str, ScopeInfo],
goals: list[str],
scope_to_flags: dict[str, list[str]],
specs: list[str],
passthru: list[str],
help_request: HelpRequest | None,
parser_by_scope: dict[str, Parser],
bootstrap_option_values: OptionValueContainer | None,
known_scope_to_info: dict[str, ScopeInfo],
allow_unknown_options: bool = False,
) -> None:
"""The low-level constructor for an Options instance.
Expand All @@ -179,34 +179,34 @@ def __init__(
self._allow_unknown_options = allow_unknown_options

@property
def help_request(self) -> Optional[HelpRequest]:
def help_request(self) -> HelpRequest | None:
"""
:API: public
"""
return self._help_request

@property
def specs(self) -> List[str]:
def specs(self) -> list[str]:
"""The specifications to operate on, e.g. the target addresses and the file names.
:API: public
"""
return self._specs

@property
def goals(self) -> List[str]:
def goals(self) -> list[str]:
"""The requested goals, in the order specified on the cmd line.
:API: public
"""
return self._goals

@property
def known_scope_to_info(self) -> Dict[str, ScopeInfo]:
def known_scope_to_info(self) -> dict[str, ScopeInfo]:
return self._known_scope_to_info

@property
def scope_to_flags(self) -> Dict[str, List[str]]:
def scope_to_flags(self) -> dict[str, list[str]]:
return self._scope_to_flags

def verify_configs(self, global_config: Config) -> None:
Expand Down Expand Up @@ -396,7 +396,7 @@ def __getitem__(self, scope: str) -> OptionValueContainer:
# Consider killing if tests consolidate on using TestOptions instead of the raw dicts.
return self.for_scope(scope)

def bootstrap_option_values(self) -> Optional[OptionValueContainer]:
def bootstrap_option_values(self) -> OptionValueContainer | None:
"""Return the option values for bootstrap options.
General code can also access these values in the global scope. But option registration code
Expand Down
Loading

0 comments on commit 7329d6d

Please sign in to comment.