Is there a way to create a field that has a consta...
# plugins
r
Is there a way to create a field that has a constant value and can't be overwritten? Aside from just throwing an error if it isn't the default value.
c
For the field class there’s a ‘compute_value’ method that can return your constant value. In there you’d probably want to throw if the passed in value is not None to avoid confusion if someone tries to provide a value for it.
g
Is the value important, or just that the field exists?
r
The value is important, it's basically for a target that I want to make it so you can't change the value of the
sources
field.
c
If the field is a subclass of StringField or StringSequenceField (like SingleSourcesField or MultipleSourcesField) then there's
valid_choices
which might work?
c
If you have a custom field, just override this method to return your constant value (and/or validate that
raw_value
is None) https://github.com/pantsbuild/pants/blob/842c566b31b3f9ee3a06640a339d3d7f93508c09/src/python/pants/engine/internals/native_engine.pyi#L341