-
-
Notifications
You must be signed in to change notification settings - Fork 703
Fix NG subsystem mypy plugin #23334
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
Open
benjyw
wants to merge
1
commit into
pantsbuild:main
Choose a base branch
from
benjyw:fix-subsystem-mypy-plugin
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Fix NG subsystem mypy plugin #23334
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| # Copyright 2026 Pants project contributors (see CONTRIBUTORS.md). | ||
| # Licensed under the Apache License, Version 2.0 (see LICENSE). | ||
|
|
||
| import tempfile | ||
| import textwrap | ||
| from pathlib import Path | ||
|
|
||
| import mypy.api | ||
|
|
||
| from pants.util.contextutil import environment_as | ||
|
|
||
| _FOO_GOAL_SUBSYSTEM_SOURCE = textwrap.dedent("""\ | ||
| from pants.ng.goal import GoalSubsystemNg | ||
| from pants.ng.subsystem import option | ||
|
|
||
| class FooSubsystem(GoalSubsystemNg): | ||
| options_scope = "foo" | ||
| help = "Run foo" | ||
|
|
||
| @option(default=0, help="How much to foo") | ||
| def level(self) -> int: ... | ||
| """) | ||
|
|
||
|
|
||
| _BAR_SUBSYSTEM_SOURCE = textwrap.dedent("""\ | ||
| from pants.ng.subsystem import ContextualSubsystem, option | ||
|
|
||
| class Bar(ContextualSubsystem): | ||
| options_scope = "bar" | ||
| help = "Options for bar." | ||
|
|
||
| @option(default="A", help="how to bar") | ||
| def how_to(self) -> str: ... | ||
|
|
||
| @option(default=false, help="whether to do the bar") | ||
| def do_it(self) -> bool: ... | ||
| """) | ||
|
|
||
|
|
||
| def _run_mypy(source: str, *, with_plugin: bool) -> tuple[str, str]: | ||
| """Run mypy on the given source string and return stdout.""" | ||
| # SubsystemNg and its subclasses depend on various code from the | ||
| # codebase, so we must point mypy at it. | ||
| import pants | ||
|
|
||
| pants_src = str(Path(pants.__file__).parents[1]) | ||
| plugin_path = str(Path(__file__).parent / "subsystem_mypy_plugin.py") | ||
|
|
||
| with ( | ||
| tempfile.NamedTemporaryFile(suffix=".py", mode="w") as src_f, | ||
| tempfile.NamedTemporaryFile(suffix=".ini", mode="w") as cfg_f, | ||
| ): | ||
| src_f.write(source) | ||
| src_f.flush() | ||
| cfg_f.write("[mypy]\n") | ||
| cfg_f.flush() | ||
| with environment_as(MYPYPATH=pants_src): | ||
| args = [ | ||
| src_f.name, | ||
| "--no-error-summary", | ||
| "--no-incremental", | ||
| f"--config-file={cfg_f.name}", | ||
| ] | ||
| if with_plugin: | ||
| args.extend(["--plugin", plugin_path]) | ||
| stdout, stderr, _code = mypy.api.run(args) | ||
| return stdout, stderr | ||
|
|
||
|
|
||
| def test_plugin_suppresses_empty_body_on_lint_subsystem() -> None: | ||
| stdout_noplugin, stderr_noplugin = _run_mypy(_FOO_GOAL_SUBSYSTEM_SOURCE, with_plugin=False) | ||
| assert "empty-body" in stdout_noplugin, ( | ||
| f"Expected [empty-body] error without plugin:\n{stdout_noplugin} (err: {stderr_noplugin})" | ||
| ) | ||
|
|
||
| stdout_plugin, stderr_plugin = _run_mypy(_FOO_GOAL_SUBSYSTEM_SOURCE, with_plugin=True) | ||
| assert "empty-body" not in stdout_plugin, ( | ||
| f"Plugin did not suppress [empty-body] error:\n{stdout_plugin} (err: {stderr_plugin})" | ||
| ) | ||
|
|
||
|
|
||
| def test_plugin_suppresses_empty_body_on_contextual_subsystem() -> None: | ||
| stdout_noplugin, stderr_noplugin = _run_mypy(_BAR_SUBSYSTEM_SOURCE, with_plugin=False) | ||
| assert "empty-body" in stdout_noplugin, ( | ||
| f"Expected [empty-body] error without plugin:\n{stdout_noplugin} (err: {stderr_noplugin})" | ||
| ) | ||
|
|
||
| stdout_plugin, stderr_plugin = _run_mypy(_BAR_SUBSYSTEM_SOURCE, with_plugin=True) | ||
| assert "empty-body" not in stdout_plugin, ( | ||
| f"Plugin did not suppress [empty-body] error:\n{stdout_plugin} (err: {stderr_plugin})" | ||
| ) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can these be discovered by the plugin from semantic analysis in a performant manner?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think that can be done reliably, let alone performantly?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I asked Codex and it suggests looking at
ctx.cls.info.mroto access the MRO for the class:Open question whether that is performant enough.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe add a conformance check in the plugin for all direct subclasses of
pants.ng.subsystem.SubsystemNgthat they are listed in_SUBSYSTEM_BASE_CLASSES?Basically, it would be better if the plugin either discovers the contents of
_SUBSYSTEM_BASE_CLASSESor otherwise flags an error when items are missing from it.