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

add a TypedCollection type constraint to reduce boilerplate for datatype tuple fields #7115

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
11f2c82
move Collection to src/python/pants/engine/objects.py
cosmicexplorer Jan 19, 2019
60c376d
refactor TypeConstraint to allow parameterized types such as collections
cosmicexplorer Jan 20, 2019
580054d
do the argument checking ourselves so we can use wrapper_type
cosmicexplorer Jan 20, 2019
2d79f91
add testing for TypedCollection by itself and in a datatype
cosmicexplorer Jan 20, 2019
5e1abe3
coup de gras -- convert Collection.of() to use TypedCollection
cosmicexplorer Jan 20, 2019
3ae3c64
add temporary `.types` property
cosmicexplorer Jan 20, 2019
823caca
clear up a docstring
cosmicexplorer Jan 20, 2019
979e2db
remove an assert and ensure wrapper_type is a type
cosmicexplorer Jan 20, 2019
77685d8
flesh out a needed docstring
cosmicexplorer Jan 20, 2019
65e650f
remove an old comment
cosmicexplorer Jan 20, 2019
20e55c8
py3 compat
cosmicexplorer Jan 20, 2019
03f3c1e
Revert "do the argument checking ourselves so we can use wrapper_type"
cosmicexplorer Jan 28, 2019
d418372
break my own heart and remove wrapper_type
cosmicexplorer Jan 28, 2019
9d3a319
respond to review comments
cosmicexplorer Feb 3, 2019
bff5668
test validate_satisfied_by() and use str() in error messages
cosmicexplorer Feb 3, 2019
5c50f87
remove uses of the previous variance symbol in testing
cosmicexplorer Feb 6, 2019
513cfb8
fix the last remnant of the far-too-concise __str__!
cosmicexplorer Feb 6, 2019
00fb59e
py3 fix, this time tested locally with ./pants3
cosmicexplorer Feb 7, 2019
2c68ba7
fix py3 compat in the other test_objects.py
cosmicexplorer Feb 7, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/python/pants/engine/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ python_library(
dependencies=[
'3rdparty/python/twitter/commons:twitter.common.collections',
'3rdparty/python:future',
':objects',
':rules',
':selectors',
'src/python/pants/base:project_tree',
Expand Down Expand Up @@ -121,7 +122,10 @@ python_library(
name='objects',
sources=['objects.py'],
dependencies=[
'3rdparty/python:future',
'src/python/pants/util:meta',
'src/python/pants/util:memo',
'src/python/pants/util:objects',
]
)

Expand Down Expand Up @@ -172,6 +176,7 @@ python_library(
':isolated_process',
':native',
':nodes',
':objects',
':rules',
'src/python/pants/base:exceptions',
'src/python/pants/base:specs',
Expand Down
4 changes: 2 additions & 2 deletions src/python/pants/engine/addressable.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
from future.utils import string_types

from pants.build_graph.address import Address, BuildFileAddress
from pants.engine.objects import Resolvable, Serializable
from pants.engine.objects import Collection, Resolvable, Serializable
from pants.util.collections_abc_backport import MutableMapping, MutableSequence
from pants.util.objects import Collection, TypeConstraintError
from pants.util.objects import TypeConstraintError


Addresses = Collection.of(Address)
Expand Down
3 changes: 2 additions & 1 deletion src/python/pants/engine/fs.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@
from future.utils import binary_type, text_type

from pants.base.project_tree import Dir, File
from pants.engine.objects import Collection
from pants.engine.rules import RootRule
from pants.option.custom_types import GlobExpansionConjunction
from pants.option.global_options import GlobMatchErrorBehavior
from pants.util.objects import Collection, datatype
from pants.util.objects import datatype


class FileContent(datatype([('path', text_type), ('content', binary_type)])):
Expand Down
1 change: 1 addition & 0 deletions src/python/pants/engine/legacy/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ python_library(
'src/python/pants/build_graph',
'src/python/pants/engine:build_files',
'src/python/pants/engine:mapper',
'src/python/pants/engine:objects',
'src/python/pants/engine:parser',
'src/python/pants/engine:selectors',
'src/python/pants/option',
Expand Down
3 changes: 2 additions & 1 deletion src/python/pants/engine/legacy/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,14 @@
from pants.engine.legacy.address_mapper import LegacyAddressMapper
from pants.engine.legacy.structs import BundleAdaptor, BundlesField, SourcesField, TargetAdaptor
from pants.engine.mapper import AddressMapper
from pants.engine.objects import Collection
from pants.engine.parser import SymbolTable, TargetAdaptorContainer
from pants.engine.rules import RootRule, rule
from pants.engine.selectors import Get, Select
from pants.option.global_options import GlobMatchErrorBehavior
from pants.source.filespec import any_matches_filespec
from pants.source.wrapped_globs import EagerFilesetWithSpec, FilesetRelPathWrapper
from pants.util.objects import Collection, datatype
from pants.util.objects import datatype


logger = logging.getLogger(__name__)
Expand Down
41 changes: 41 additions & 0 deletions src/python/pants/engine/objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,16 @@
from __future__ import absolute_import, division, print_function, unicode_literals

import inspect
import sys
from abc import abstractmethod, abstractproperty
from builtins import object
from collections import namedtuple

from future.utils import PY2

from pants.util.memo import memoized_classmethod
from pants.util.meta import AbstractClass
from pants.util.objects import Exactly, TypedCollection, datatype


class SerializationError(Exception):
Expand Down Expand Up @@ -146,3 +152,38 @@ def validate(self):

:raises: :class:`ValidationError` if this object is invalid.
"""


class Collection(object):
"""Constructs classes representing collections of objects of a particular type.

The produced class will expose its values under a field named dependencies - this is a stable API
which may be consumed e.g. over FFI from the engine.

Python consumers of a Collection should prefer to use its standard iteration API.

Note that elements of a Collection are type-checked upon construction.
"""

@memoized_classmethod
def of(cls, *element_types):
union = '|'.join(element_type.__name__ for element_type in element_types)
type_name = '{}.of({})'.format(cls.__name__, union)
if PY2:
type_name = type_name.encode('utf-8')
type_checked_collection_class = datatype([
# Create a datatype with a single field 'dependencies' which is type-checked on construction
# to be a collection containing elements of only the exact `element_types` specified.
('dependencies', TypedCollection(Exactly(*element_types)))
], superclass_name=cls.__name__)
supertypes = (cls, type_checked_collection_class)
properties = {'element_types': element_types}
collection_of_type = type(type_name, supertypes, properties)

# Expose the custom class type at the module level to be pickle compatible.
setattr(sys.modules[cls.__module__], type_name, collection_of_type)

return collection_of_type

def __iter__(self):
return iter(self.dependencies)
3 changes: 2 additions & 1 deletion src/python/pants/engine/scheduler.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@
from pants.engine.isolated_process import ExecuteProcessRequest, FallibleExecuteProcessResult
from pants.engine.native import Function, TypeConstraint, TypeId
from pants.engine.nodes import Return, Throw
from pants.engine.objects import Collection
from pants.engine.rules import RuleIndex, SingletonRule, TaskRule
from pants.engine.selectors import Params, Select, constraint_for
from pants.rules.core.exceptions import GracefulTerminationException
from pants.util.contextutil import temporary_file_path
from pants.util.dirutil import check_no_overlapping_paths
from pants.util.objects import Collection, datatype
from pants.util.objects import datatype
from pants.util.strutil import pluralize


Expand Down
Loading