aloof-angle-91616
03/13/2020, 4:13 PMhundreds-father-404
03/13/2020, 4:16 PMCompatibility.value
is List[str]
but that Compatibility.raw_value
is Optional[Union[str, Iterable[str]]]
.
The only point that is not safe is that we assume the type annotation for raw_value
is correct. There’s nothing stopping a user from saying compatibility=1.0
in a BUILD file. Our type hint that raw_value: Optional[Union[str, Iterable[str]]]
is now a lie. So, possibly, we want to have very basic Python runtime type checks.
For a boolean field, for example, nothing more complex than if not isinstance(raw_value, bool): raise ValueError
aloof-angle-91616
03/13/2020, 7:35 PMhundreds-father-404
03/13/2020, 7:48 PMchecking for runtime types in a one-off manner without greater structure becomes super complex super easilyThere is a very nice structure thanks to @average-vr-56795’s suggestion. See https://github.com/pantsbuild/pants/pull/9296#discussion_r392434402 tl;dr: the vast majority of
Field
definitions won’t need to do anything more than declare their alias and possibly a default value, thanks to Field types we expose like BoolField
, StringField
, etc.
class ZipSafe(BoolField):
alias: ClassVar = "zip_safe"
default: ClassVar = True
That’s it 🎉