Please sign in to comment.
add a TypedCollection type constraint to reduce boilerplate for datat…
…ype tuple fields (pantsbuild#7115) ### Problem *Resolves pantsbuild#6936.* There's been a [TODO in `pants.util.objects.Collection`](https://github.com/pantsbuild/pants/blob/c342fd3432aa0d73e402d2db7e013ecfcc76e9c8/src/python/pants/util/objects.py#L413) for a while to typecheck datatype tuple fields. pantsbuild#6936 has some thoughts on how to do this, but after realizing I could split out `TypeConstraint` into a base class and then introduce `BasicTypeConstraint` for type constraints which only act on the type, I think that ticket is invalidated as this solution is much cleaner. ### Solution - Split out logic for basic type checking (without looking at the object itself) into a `BasicTypeConstraint` class, which `Exactly` and friends inherit from. - Create the `TypedCollection` type constraint, which checks that its argument is iterable and then validates each element of the collection with a `BasicTypeConstraint` constructor argument. - Note that `TypedCollection` is a `TypeConstraint`, but not a `BasicTypeConstraint`, as it has to inspect the actual object object to determine whether each element matches the provided `BasicTypeConstraint`. - Move `pants.util.objects.Collection` into `src/python/pants/engine/objects.py`, as it is specifically for engine objects. - Use `TypedCollection` for the `dependencies` field of the datatype returned by `Collection.of()`. ### Result - `datatype` consumers and creators no longer have to have lots of boilerplate when using collections arguments, and those arguments can now be typechecked and made hashable for free! ### TODO in followup: `wrapper_type` See pantsbuild#7172.
- Loading branch information...
Showing with 384 additions and 134 deletions.
- +5 −0 src/python/pants/engine/BUILD
- +2 −2 src/python/pants/engine/addressable.py
- +2 −1 src/python/pants/engine/fs.py
- +1 −0 src/python/pants/engine/legacy/BUILD
- +2 −1 src/python/pants/engine/legacy/graph.py
- +41 −0 src/python/pants/engine/objects.py
- +2 −1 src/python/pants/engine/scheduler.py
- +140 −91 src/python/pants/util/objects.py
- +10 −0 tests/python/pants_test/engine/BUILD
- +3 −3 tests/python/pants_test/engine/legacy/test_graph_integration.py
- +6 −6 tests/python/pants_test/engine/test_engine.py
- +1 −1 tests/python/pants_test/engine/test_mapper.py
- +33 −0 tests/python/pants_test/engine/test_objects.py
- +2 −2 tests/python/pants_test/engine/test_scheduler.py
- +134 −26 tests/python/pants_test/util/test_objects.py
Oops, something went wrong.