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

Add Target.class_has_field() #9329

Merged
merged 1 commit into from Mar 18, 2020

Conversation

Eric-Arellano
Copy link
Contributor

Problem

Currently, we have the instance method Target.has_field() (and Target.has_fields()), e.g. tgt.has_field(PythonSources).

In some cases, though, we need a classmethod rather than an instance method to be able to do this same type of check. For example, when generating an error message when running ./v2 binary on an invalid target type, we want to be able to calculate every single target type that does work with the goal. To do that, we need to be able to iterate over every Type[Target] registered and call PythonLibrary.has_fields([EntryPoint, PythonBinarySources]) so that we can decide if PythonLibrary is a valid target type or not.

The tricky part is that we must support plugin fields added to pre-existing target types, which are achieved by plugin authors registering UnionRule(PythonLibrary.PluginField, MyCustomField).

Solution

Add Target.class_has_field() and Target.class_has_fields().

Factor out Target.has_field() to deduplicate between the classmethod and instance method.

@Eric-Arellano
Copy link
Contributor Author

This is prework to have this error message:

No registered binary implementations work with src/python/pants/util:strutil (target type "python_library"). All registered binary implementations:

  • PythonBinaryImplementation, works with target types: [python_binary, my_custom_python_binary]
  • JavaBinaryImplementation, works with target types: [java_binary]

And this for ./v2 lint:

All registered linters:

  • BlackFormatter, works with target types: [python_binary, python_library, python_tests]
  • BanditLinter, works with target types: [python_binary, python_library, python_tests]

--

Credit goes to @blorente for the great names for these new class methods! Thank you!

@Eric-Arellano Eric-Arellano merged commit 250ed9d into pantsbuild:master Mar 18, 2020
@Eric-Arellano Eric-Arellano deleted the class-has-field branch March 18, 2020 21:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants