are we going to recreate a runtime type checking s...
# development
a
are we going to recreate a runtime type checking system?
h
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
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
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 🎉