Skip to content

Commit

Permalink
Allow deducing flag name from option attribute name (pantsbuild#16040)
Browse files Browse the repository at this point in the history
Removes the need for specifying the flag name to options, as it will default to the attribute name kebab-cased (and underscore stripped).

Why? Well DRY for one. Avoid typos for two. But for three, because its nice to have the code you're reading be immediately mentally mappable to the flag.

Using `generate_docs` I can confirm there is 0 diff from `main` 💪 

[ci skip-rust]
  • Loading branch information
thejcannon authored Jul 5, 2022
1 parent 8efc396 commit 4297cc1
Show file tree
Hide file tree
Showing 104 changed files with 237 additions and 547 deletions.
2 changes: 0 additions & 2 deletions docs/markdown/Contributions/development/style-guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ $ build-support/githooks/pre-commit
>
> ```python
> StrOption(
> "--pants-bin-name",
> default="./pants",
> help="The name of the script or binary used to invoke pants. "
> "Useful when printing help messages.",
Expand All @@ -33,7 +32,6 @@ $ build-support/githooks/pre-commit
>
> ```python
> StrOption(
> "--pants-bin-name",
> default="./pants",
> help=(
> "The name of the script or binary used to invoke pants. "
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,37 @@ hidden: false
createdAt: "2020-10-12T16:19:01.543Z"
updatedAt: "2022-04-27T20:02:17.695Z"
---
2.14
----

See <https://github.com/pantsbuild/pants/blob/main/src/python/pants/notes/2.14.x.md> for the changelog.

### Optional Option flag name

Pants 2.14 adds support for deducing the flag name from the attribute name when declaring `XOption`s.
You can still provide the flag name in case the generated one shouldn't match the attribute name.

Before:

```python
my_version = StrOption("--my-version", ...)
_run = BoolOption("--run", ...)
```

Now:

```python
my_version = StrOption(...) # Still uses --my-version
_run = BoolOption(...) # Still uses --run
```

2.13
----

See <https://github.com/pantsbuild/pants/blob/main/src/python/pants/notes/2.13.x.md> for the changelog.

(TODO. Come back later)

2.12
----

Expand Down Expand Up @@ -227,7 +258,7 @@ This is a more declarative way to set up files than the older API of `RuleRunner
2.4
---

See <https://github.com/pantsbuild/pants/blob/main/src/python/pants/notes/2.4.x.md> for the changelog.
See <https://github.com/pantsbuild/pants/blob/main/src/python/pants/notes/2.4.x.md> for the changelog.

### `PexRequest` changes how entry point is set

Expand All @@ -237,14 +268,14 @@ See <https://github.com/pantsbuild/pants/pull/11620>. Instead of setting `entry_

See <https://github.com/pantsbuild/pants/pull/11641>. Pants now eagerly purges environment variables from the run, so using `os.environ` in plugins won't work anymore.

Instead, use `await Get(Environment, EnvironmentRequest(["MY_ENV_VAR"])`.
Instead, use `await Get(Environment, EnvironmentRequest(["MY_ENV_VAR"])`.

For `RuleRunner` tests, you must now either set `env` or the new `env_inherit` arguments for environment variables to be set. Tests are now hermetic.

2.3
---

There were no substantial changes to the Plugin API in 2.3. See <https://github.com/pantsbuild/pants/blob/main/src/python/pants/notes/2.3.x.md> for the changelog.
There were no substantial changes to the Plugin API in 2.3. See <https://github.com/pantsbuild/pants/blob/main/src/python/pants/notes/2.3.x.md> for the changelog.

2.2
---
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,23 +45,23 @@ async def demo(...) -> Foo:
In this example, the `search_path` is hardcoded. Instead, you may want to create a [subsystem](doc:rules-api-subsystems) to allow users to override the search path through a dedicated option. See [pex_environment.py](https://github.com/pantsbuild/pants/blob/57a47457bda0b0dfb0882d851ccd58a7535f15c1/src/python/pants/backend/python/rules/pex_environment.py#L60-L71) for an example that allows the user to use the special string `<PATH>` to read the user's `$PATH` environment variable.

> 📘 Checking for valid binaries (recommended)
>
>
> When setting up a `BinaryPathsRequest`, you can optionally pass the argument `test: BinaryPathTest`. When discovering a binary, Pants will run your test and only use the binary if the return code is 0. Pants will also fingerprint the output and invalidate the cache if the output changes from before, such as because the user upgraded the version of the tool.
>
>
> Why do this? This is helpful to ensure that all discovered binaries are valid and safe. This is also important for Pants to be able to detect when the user has changed the binary, such as upgrading its version.
>
> `BinaryPathTest` takes the argument `args: Iterable[str]`, which is the arguments that Pants should run on your binary to ensure that it's a valid program. Usually, you'll set `args=["--version"]`.
>
>
> `BinaryPathTest` takes the argument `args: Iterable[str]`, which is the arguments that Pants should run on your binary to ensure that it's a valid program. Usually, you'll set `args=["--version"]`.
>
> ```python
> from pants.core.util_rules.system_binaries import BinaryPathRequest, BinaryPathTest
>
>
> BinaryPathRequest(
> binary_name="docker",
> search_path=["/usr/bin", "/bin"],
> test=BinaryPathTest(args=["--version"]),
> )
> ```
>
>
> You can optionally set `fingerprint_stdout=False` to the `BinaryPathTest` constructor, but usually, you should keep the default of `True`.

`ExternalTool`: Install pre-compiled binaries
Expand Down Expand Up @@ -189,11 +189,11 @@ Instead of the normal `Get(ProcessResult, Process)`, you should use `Get(Process
> 📘 Use `PythonToolBase` when you need a Subsystem
>
>
> Often, you will want to create a [`Subsystem`](doc:rules-api-subsystems) for your Python tool
> to allow users to set options to configure the tool. You can subclass `PythonToolBase`, which
> subclasses `Subsystem`, to do this:
>
>
> ```python
> from pants.backend.python.subsystems.python_tool_base import PythonToolBase
> from pants.backend.python.target_types import ConsoleScript
Expand All @@ -209,18 +209,17 @@ Instead of the normal `Get(ProcessResult, Process)`, you should use `Get(Process
> default_interpreter_constraints = ["CPython>=3.6"]
>
> config = StrOption(
> "--config",
> default=None,
> advanced=True,
> help="Path to Black's pyproject.toml config file",
> )
> ```
>
>
> You must define the class properties `options_scope`, `default_version`, and `default_main`. You
> can optionally define `default_extra_requirements` and `default_interpreter_constraints`.
>
> Then, you can set up your `Pex` like this:
>
>
> ```python
> @rule
> async def demo(black: Black, ...) -> Foo:
Expand Down
17 changes: 11 additions & 6 deletions docs/markdown/Writing Plugins/rules-api/rules-api-subsystems.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,14 @@ from pants.option.subsystem import Subsystem
class ShellcheckSubsystem(Subsystem):
options_scope = "shellcheck"
help = "The Shellcheck linter."

config_discovery = BoolOption(
"--config-discovery",
default=True,
advanced=True,
help="Whether Pants should...",
)


def rules():
return [SubsystemRule(ShellcheckSubsystem)]
```
Expand All @@ -52,16 +51,22 @@ def rules():
The subsystem should now show up when you run `./pants help shellcheck`.

> 📘 `GoalSubsystem`
>
>
> As explained in [Goal rules](doc:rules-api-goal-rules), goals use a subclass of `Subsystem`: `GoalSubsystem` from `pants.engine.goal`.
>
>
> `GoalSubsystem` behaves the same way as a normal subsystem, except that you set the class property `name` rather than `options_scope`. The `name` will auto-populate the `options_scope`.
### Option types

These classes correspond to the option types at [Options](doc:options).

Every option type requires that you set the flag name (e.g. `-l` or `--level`) and the keyword argument `help`. Most types require that you set `default`. You can optionally set `advanced=True` with every option for it to only show up with `help-advanced`.
Every option type requires that you set the keyword argument `help`.

Most types require that you set `default`. You can optionally set `advanced=True` with every option
for it to only show up with `help-advanced`.

The option name will default to the class attribute name, e.g. `my_opt = StrOption()` will default to `--my-opt`.
You can instead pass a string positional argument, e.g. `my_opt = StrOption("--different-name")`.

[block:parameters]
{
Expand Down
1 change: 0 additions & 1 deletion pants-plugins/internal_plugins/releases/register.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ class PantsReleases(Subsystem):
help = "Options for Pants's release process."

_release_notes = DictOption[str](
"--release-notes",
help="A dict from branch name to release notes rst-file location.",
)

Expand Down
2 changes: 0 additions & 2 deletions src/python/pants/backend/cc/subsystems/cc_infer.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,13 @@ class CCInferSubsystem(Subsystem):
help = "Options controlling which dependencies will be inferred for CC targets."

includes = BoolOption(
"--includes",
default=True,
help="Infer a target's dependencies by parsing #include statements from sources.",
)

# TODO: This option may move to a proper `cc` subsystem once compilation is implemented. It may also
# change depending on how we want to model in-repo includes.
include_from_source_roots = BoolOption(
"--include-from-source-roots",
default=True,
help="Infer a target's dependencies by trying to include relative to source roots.",
)
1 change: 0 additions & 1 deletion src/python/pants/backend/codegen/avro/avro_subsystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ class AvroSubsystem(Subsystem):
help = "General Avro codegen settings."

tailor = BoolOption(
"--tailor",
default=True,
help="If true, add `avro_sources` targets with the `tailor` goal.",
advanced=True,
Expand Down
1 change: 0 additions & 1 deletion src/python/pants/backend/codegen/avro/java/subsystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ class AvroSubsystem(JvmToolBase):
default_lockfile_url = git_url(default_lockfile_path)

runtime_dependencies = TargetListOption(
"--runtime-dependencies",
help=lambda cls: softwrap(
f"""
A list of addresses to `jvm_artifact` targets for the runtime dependencies needed for
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ async def setup_buf_format(request: BufFormatRequest, buf: BufSubsystem) -> Proc

@rule(desc="Format with buf format", level=LogLevel.DEBUG)
async def run_buf_format(request: BufFormatRequest, buf: BufSubsystem) -> FmtResult:
if buf.skip_format:
if buf.format_skip:
return FmtResult.skip(formatter_name=request.name)
result = await Get(ProcessResult, BufFormatRequest, request)
output_snapshot = await Get(Snapshot, Digest, result.output_digest)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class BufLintRequest(LintTargetsRequest):

@rule(desc="Lint with buf lint", level=LogLevel.DEBUG)
async def run_buf(request: BufLintRequest, buf: BufSubsystem) -> LintResults:
if buf.skip_lint:
if buf.lint_skip:
return LintResults([], linter_name=request.name)

transitive_targets = await Get(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ class BufSubsystem(TemplatedExternalTool):
"linux_x86_64": "Linux-x86_64",
}

skip_format = SkipOption("fmt", "lint", flag_name="--format-skip")
skip_lint = SkipOption("lint", flag_name="--lint-skip")
format_args = ArgsListOption(example="--error-format json", flag_name="--format-args")
lint_args = ArgsListOption(example="--error-format json", flag_name="--lint-args")
format_skip = SkipOption("fmt", "lint")
lint_skip = SkipOption("lint")
format_args = ArgsListOption(example="--error-format json")
lint_args = ArgsListOption(example="--error-format json")

def generate_exe(self, plat: Platform) -> str:
return "./buf/bin/buf"
2 changes: 0 additions & 2 deletions src/python/pants/backend/codegen/protobuf/protoc.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,10 @@ class Protoc(TemplatedExternalTool):
}

dependency_inference = BoolOption(
"--dependency-inference",
default=True,
help="Infer Protobuf dependencies on other Protobuf files by analyzing import statements.",
)
tailor = BoolOption(
"--tailor",
default=True,
help="If true, add `protobuf_sources` targets with the `tailor` goal.",
advanced=True,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ class PythonProtobufSubsystem(Subsystem):
)

mypy_plugin = BoolOption(
"--mypy-plugin",
default=False,
help=softwrap(
"""
Expand All @@ -50,7 +49,6 @@ class PythonProtobufSubsystem(Subsystem):
)

infer_runtime_dependency = BoolOption(
"--infer-runtime-dependency",
default=True,
help=softwrap(
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ class ScalaPBSubsystem(JvmToolBase):
default_lockfile_url = git_url(default_lockfile_path)

_jvm_plugins = StrListOption(
"--jvm-plugins",
help=softwrap(
"""
A list of JVM-based `protoc` plugins to invoke when generating Scala code from protobuf files.
Expand Down
1 change: 0 additions & 1 deletion src/python/pants/backend/codegen/soap/soap_subsystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ class SoapSubsystem(Subsystem):
help = "General SOAP/WSDL codegen settings."

tailor = BoolOption(
"--tailor",
default=True,
help="If true, add `wsdl_sources` targets with the `tailor` goal.",
advanced=True,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ class ThriftPythonSubsystem(Subsystem):
),
)
infer_runtime_dependency = BoolOption(
"--infer-runtime-dependency",
default=True,
help=softwrap(
"""
Expand Down
2 changes: 0 additions & 2 deletions src/python/pants/backend/codegen/thrift/apache/subsystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ class ApacheThriftSubsystem(Subsystem):
help = "Apache Thrift IDL compiler (https://thrift.apache.org/)."

_thrift_search_paths = StrListOption(
"--thrift-search-paths",
default=["<PATH>"],
help=softwrap(
"""
Expand All @@ -30,7 +29,6 @@ class ApacheThriftSubsystem(Subsystem):
),
)
expected_version = StrOption(
"--expected-version",
default="0.15",
help=softwrap(
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ class ScroogeJavaSubsystem(Subsystem):
help = "Java-specific options for the Scrooge Thrift IDL compiler (https://twitter.github.io/scrooge/)."

_runtime_dependencies = TargetListOption(
"--runtime-dependencies",
help=softwrap(
"""
A list of addresses to `jvm_artifact` targets for the runtime
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ class ScroogeScalaSubsystem(Subsystem):
help = "Scala-specific options for the Scrooge Thrift IDL compiler (https://twitter.github.io/scrooge/)."

_runtime_dependencies = TargetListOption(
"--runtime-dependencies",
help=softwrap(
f"""
A list of addresses to `jvm_artifact` targets for the runtime
Expand Down
2 changes: 0 additions & 2 deletions src/python/pants/backend/codegen/thrift/subsystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,10 @@ class ThriftSubsystem(Subsystem):
help = "General Thrift IDL settings (https://thrift.apache.org/)."

dependency_inference = BoolOption(
"--dependency-inference",
default=True,
help="Infer Thrift dependencies on other Thrift files by analyzing import statements.",
)
tailor = BoolOption(
"--tailor",
default=True,
help="If true, add `thrift_sources` targets with the `tailor` goal.",
advanced=True,
Expand Down
2 changes: 0 additions & 2 deletions src/python/pants/backend/docker/lint/hadolint/subsystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ class Hadolint(TemplatedExternalTool):
skip = SkipOption("lint")
args = ArgsListOption(example="--format json")
config = FileOption(
"--config",
default=None,
advanced=True,
help=lambda cls: softwrap(
Expand All @@ -48,7 +47,6 @@ class Hadolint(TemplatedExternalTool):
),
)
config_discovery = BoolOption(
"--config-discovery",
default=True,
advanced=True,
help=lambda cls: softwrap(
Expand Down
Loading

0 comments on commit 4297cc1

Please sign in to comment.