[pants_ng] An NG subsystem implementation#23165
[pants_ng] An NG subsystem implementation#23165benjyw wants to merge 4 commits intopantsbuild:mainfrom
Conversation
|
Reviewers: The one downside is that mypy doesn't like methods without bodies. So the Another solution could be to have the body return the default, instead of the Thoughts? |
Maybe have |
|
Another thought is to provide a Mypy plugin to handle typing of Regardless of the solution, I believe we should figure out how to configure mypy to not error since having to disable a type error to use PantsNG is a major anti-pattern in my view. |
|
Alas there does not appear to be a way to do this in a mypy plugin (possibly at all, and certainly not one that doesn't cause more problems than it solves). mypy plugins allow you to modify signatures and types, but they don't interact with this body detection feature. TBH this check is pretty low value to begin with, so I'm comfortable with disabling it globally in the repo. But would like to hear more opinions, and I will continue to try and think some sort of solution. |
|
Hmm, Claude appears to have found a way to do this with a mypy plugin, by monkeying with the parse tree. I'll take it! |
|
OK, pushed a mypy plugin that does the trick. |
| types-setuptools==82.0.0.20260210 | ||
| types-toml==0.10.8.20240310 | ||
| typing-extensions==4.15 | ||
| mypy~=1.19.1 |
There was a problem hiding this comment.
Now that we have an in-repo mypy plugin, we need mypy as a dev-time requirement.
| def _has_option_decorator(node: Decorator) -> bool: | ||
| for dec in node.decorators: | ||
| if hasattr(dec, "callee"): | ||
| if hasattr(dec.callee, "name") and dec.callee.name == "option": |
There was a problem hiding this comment.
Maybe this should check the full-qualified name of option via dec.callee.fullname? Any use of just option could trigger this plugin, right?
There was a problem hiding this comment.
fullname doesn't work. It's an empty string.
There was a problem hiding this comment.
So, yes, if someone has an unrelated @option decorator and wants mypy to error if the function it decorates has an empty body, then we'll trip them up. What are the odds of that?
There was a problem hiding this comment.
And we know we don't have this case in our own repo, so it's just external users with plugins, and they also have the option of marking their options @abstract or of ignoring the error with a pragma.
|
Thanks for the great review @tdyas. I will wait for more feedback, since the API here is high-touch for plugin developers. |
Like OG subsystems, these are containers of options
read from flags, env vars and config files.
Unlike OG subsystems, these are not necessarily
singletons - you can have multiple instances of the
same subsystem active for different partitions of
sources that have different local config files.
Making OG subsystems non-singleton was not feasible,
for both design and performance reasons.
These NG subsystems are lighter weight and more
streamlined than their OG counterparts:
@optiondecoratorinstead of the large menagerie of field type classes.
AI disclosure: Claude found a way to create a mypy plugin
that prevents spurious empty body errors. I had to manually
tweak the plugin to make it less verbose and more streamlined.