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

Demystify get_all_scoped_flag_names mypy kludges. #10459

Merged
merged 4 commits into from
Jul 27, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 12 additions & 5 deletions src/python/pants/option/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

import Levenshtein
import yaml
from typing_extensions import Protocol

from pants.base.build_environment import get_buildroot
from pants.base.deprecated import validate_deprecation_semver, warn_or_error
Expand Down Expand Up @@ -181,9 +182,15 @@ def walk(self, callback: Callable) -> None:
@frozen_after_init
@dataclass(unsafe_hash=True)
class ParseArgsRequest:
flag_value_map: Dict
# N.B.: We use this callable protocol instead of Callable directly to work around the
# dataclass-specific issue described here: https://github.com/python/mypy/issues/6910
class FlagNameProvider(Protocol):
def __call__(self) -> Iterable:
Copy link
Contributor

Choose a reason for hiding this comment

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

Even better would be to set the return type as Sequence["Options.ScopedFlagNameForFuzzyMatching"]. We used to have this, but I had to remove it when we added file dependencies because we started erroring on a cycle with options.py. Now, we tolerate those cycles, so we can add that import back with:

from typing import TYPE_CHECKING

if TYPE_CHECKING:
   import pants.option.options import Options

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Even better would not be to have an un-needed cycle and that's easy. I'll follow up instead of snowballing this PR.

Copy link
Contributor

Choose a reason for hiding this comment

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

Agreed :) Thanks!

...

flag_value_map: Dict[str, List[Any]]
namespace: OptionValueContainer
get_all_scoped_flag_names: Callable[["Parser.ParseArgsRequest"], Sequence]
get_all_scoped_flag_names: FlagNameProvider
levenshtein_max_distance: int
passthrough_args: List[str]
# A passive option is one that doesn't affect functionality, or appear in help messages, but
Expand All @@ -197,7 +204,7 @@ def __init__(
self,
flags_in_scope: Iterable[str],
namespace: OptionValueContainer,
get_all_scoped_flag_names: Callable[[], Sequence],
get_all_scoped_flag_names: FlagNameProvider,
levenshtein_max_distance: int,
passthrough_args: List[str],
include_passive_options: bool = False,
Expand All @@ -215,7 +222,7 @@ def __init__(
"""
self.flag_value_map = self._create_flag_value_map(flags_in_scope)
self.namespace = namespace
self.get_all_scoped_flag_names = get_all_scoped_flag_names # type: ignore[assignment] # cannot assign a method
self.get_all_scoped_flag_names = get_all_scoped_flag_names
self.levenshtein_max_distance = levenshtein_max_distance
self.passthrough_args = passthrough_args
self.include_passive_options = include_passive_options
Expand Down Expand Up @@ -350,7 +357,7 @@ def add_flag_val(v: Optional[Union[int, float, bool, str]]) -> None:
return namespace

def _raise_error_for_invalid_flag_names(
self, flags: Sequence[str], all_scoped_flag_names: Sequence, max_edit_distance: int,
self, flags: Sequence[str], all_scoped_flag_names: Iterable, max_edit_distance: int,
) -> NoReturn:
"""Identify similar option names to unconsumed flags and raise a ParseError with those
names."""
Expand Down