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

Move option registration validation earlier #21645

Merged
merged 2 commits into from
Nov 14, 2024

Conversation

benjyw
Copy link
Contributor

@benjyw benjyw commented Nov 14, 2024

Previously we validated when parsing, now we do so at registration.

This uncovered a couple of tests that were implicitly relying on
incorrect registration, so fixed those.

Also removed more legacy parser cruft.

@benjyw benjyw added the category:internal CI, fixes for not-yet-released features, etc. label Nov 14, 2024
@benjyw benjyw requested a review from tdyas November 14, 2024 15:54
@@ -23,7 +23,7 @@
logger = logging.getLogger()


def parse_dest(*args, **kwargs):
def parse_dest(*args: str, **kwargs) -> str:
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Turns out this annotation means "all *args must be strings" to mypy, which is good.

@@ -60,7 +60,7 @@ def test_format_help_choices(self) -> None:
@classmethod
def _get_parser(cls) -> Tuple[Parser, NativeOptionParser]:
return Parser(
scope_info=GlobalOptions.get_scope_info(),
scope=GlobalOptions.options_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.

We don't need "ScopeInfo" in some places now, and hopefully in all places soon...

@@ -1014,7 +1014,7 @@ def test_parse_dest() -> None:


def test_validation() -> None:
def assertError(expected_error, *args, **kwargs):
def assert_error(expected_error, *args, **kwargs):
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Just a rename for convention.

@@ -1046,7 +1046,7 @@ def test_shadowing() -> None:
args=["./pants"],
)
options.register("", "--opt1")
options.register("foo", "-o", "--opt2")
options.register("foo", "--opt2")
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 was a test that didn't adhere to our own validation rules, but skirted them since we don't parse in this test.

@@ -213,9 +180,9 @@ def normalize_kwargs(orig_args, orig_kwargs):

def register(self, *args, **kwargs) -> None:
"""Register an option."""
if args:
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 was needed because we were working with unvalidated args... 🤦

if (
isinstance(type_arg, type)
and not isinstance(default_value, type_arg)
and not (issubclass(type_arg, bool) and default_value == UnsetBool)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Previously validation tripped up on UnsetBool. I think the only reason this didn't blow up is that we don't actually use UnsetBool anywhere, and the tests were not validating. But now that they are, they caught this.

Possibly we can get rid of UnsetBool, I don't see why None couldn't be used, but one thing at a time.

@benjyw benjyw merged commit 2347436 into pantsbuild:main Nov 14, 2024
24 checks passed
@benjyw benjyw deleted the rip_out_legacy_parser3 branch November 14, 2024 20:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
category:internal CI, fixes for not-yet-released features, etc.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants