https://pantsbuild.org/ logo
#development
Title
# development
a

aloof-angle-91616

03/13/2020, 4:13 PM
are we going to recreate a runtime type checking system?
h

hundreds-father-404

03/13/2020, 4:16 PM
What do you mean? We use MyPy for everything, like indicating that
Compatibility.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
a

aloof-angle-91616

03/13/2020, 7:35 PM
checking for runtime types in a one-off manner without greater structure becomes super complex super easily
it would be really interesting to see if we can apply mypy to BUILD files instead
h

hundreds-father-404

03/13/2020, 7:48 PM
checking for runtime types in a one-off manner without greater structure becomes super complex super easily
There 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.
Copy code
class ZipSafe(BoolField):
   alias: ClassVar = "zip_safe"
   default: ClassVar = True
That’s it 🎉