Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make a typed datatype #5716

Closed
illicitonion opened this issue Apr 17, 2018 · 9 comments
Closed

Make a typed datatype #5716

illicitonion opened this issue Apr 17, 2018 · 9 comments
Assignees

Comments

@illicitonion
Copy link
Contributor

We're making a lot of the datatypes to exist as v2 arguments to rules, and a lot of them have type-checks on their arguments. We should make it easy to write something like:

class CatExecutionRequest(typeddatatype('CatExecutionRequest', [('shell_cat_binary', ShellCat), ('input_file_globs', PathGlobs)])

to reduce boilerplate.

@baroquebobcat
Copy link
Contributor

Oh, that's a nice idea

@cosmicexplorer
Copy link
Contributor

cosmicexplorer commented Apr 18, 2018

I happened to stumble upon @jsirois's comment in #5580 and ensuing discussion today and it seems like relevant context, in the sense of: if we're doing type checking with some well-defined magic in datatype ctors, would we want to apply that process to e.g. @rule arguments?

If we're doing type checking, would we assume isinstance is sufficient, or do we expose type(x) == y as an option? Would it make sense to consider the design/implementation/UX of existing python type hints?

edit: see the changes to test_isolated_process.py in #5703 for an example of the type checking we do on CatExecutionRequest.

@stuhood
Copy link
Member

stuhood commented Apr 18, 2018

I'd be interested in seeing benchmarks on this one, as we create a lot of objects. datatype is also used outside of the engine in a few places.


if we're doing type checking with some well-defined magic in datatype ctors, would we want to apply that process to e.g. @rule arguments?

We already do, in the sense that when the engine invokes rules, it guarantees not to invoke them with arguments of the wrong type. Primarily: select "noops" (...but should probably fail) if an output has the wrong type.

@cosmicexplorer
Copy link
Contributor

cosmicexplorer commented Apr 18, 2018 via email

@stuhood
Copy link
Member

stuhood commented Apr 18, 2018 via email

@cosmicexplorer
Copy link
Contributor

cosmicexplorer commented Apr 18, 2018

That was what I was thinking as well, that the result of a rule would be typechecked by the engine instead of through any datatype ctor. Not sure how that might be done -- if there are any fidgety bits (e.g. unions) it might be good to make an issue about typechecking the result of a rule.

@cosmicexplorer
Copy link
Contributor

It's also possible (if we're talking about a way to easily define simple datatypes) it might be appropriate to apply this to options (e.g. a class could be declared which ensures its argument is an existing file/directory). While nothing would change from the pants user's point of view, it might be easier for implementors of subsystems and tasks to do appropriate error checking if we could match args to more complex types.

@stuhood
Copy link
Member

stuhood commented Apr 20, 2018

e.g. a class could be declared which ensures its argument is an existing file/directory

Options already do this. See file_option and dir_option.

stuhood pushed a commit that referenced this issue Apr 27, 2018
…me argument (#5723)

### Problem

See #5716. We had to introduce a ton of boilerplate to check argument types in some very simple usage of `datatype` subclasses in tests in #5703. This is one way to make that easier. 

### Solution

- Move `TypeConstraint` and subclasses from `src/python/pants/engine/addressable.py` to `src/python/pants/util/objects.py`, where they probably should have been anyway, to avoid an import cycle.
- Remove the class name argument from `datatype()` (and update all call sites).
- Allow specifying a tuple `('field_name', FieldType)` in the list of fields to `datatype()` to have the field type-checked by an `Exactly(FieldType)` type constraint at construction.
- Override `__str__()` and `__repr__()` for `datatype` objects, and add testing for the str and repr.
- Add testing for informative error messages in `datatype` construction.

### Result

We can now concisely declare `datatype` objects without having to repeat the class name, and can opt-in to a type check for specific fields.
@stuhood
Copy link
Member

stuhood commented May 21, 2018

Thanks!

@stuhood stuhood closed this as completed May 21, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants