[BEAM-1345] Clearly delineate public api in apache_beam/typehints.
Project: http://git-wip-us.apache.org/repos/asf/beam/repo Commit: http://git-wip-us.apache.org/repos/asf/beam/commit/7881fd5e Tree: http://git-wip-us.apache.org/repos/asf/beam/tree/7881fd5e Diff: http://git-wip-us.apache.org/repos/asf/beam/diff/7881fd5e Branch: refs/heads/master Commit: 7881fd5e6b53f6a26e23f877e47a410f6a515ae7 Parents: 16a3fff Author: Robert Bradshaw <[email protected]> Authored: Thu May 11 12:54:13 2017 -0700 Committer: Robert Bradshaw <[email protected]> Committed: Thu May 11 15:15:07 2017 -0700 ---------------------------------------------------------------------- sdks/python/apache_beam/pipeline.py | 2 +- sdks/python/apache_beam/transforms/core.py | 8 ++++---- sdks/python/apache_beam/transforms/ptransform.py | 14 +++++++------- sdks/python/apache_beam/typehints/decorators.py | 17 +++++++++++++---- sdks/python/apache_beam/typehints/opcodes.py | 2 ++ .../apache_beam/typehints/trivial_inference.py | 2 ++ sdks/python/apache_beam/typehints/typecheck.py | 15 +++++++++------ sdks/python/apache_beam/typehints/typehints.py | 18 ++++++++++++++++++ .../apache_beam/typehints/typehints_test.py | 19 ++++++++++--------- 9 files changed, 66 insertions(+), 31 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/beam/blob/7881fd5e/sdks/python/apache_beam/pipeline.py ---------------------------------------------------------------------- diff --git a/sdks/python/apache_beam/pipeline.py b/sdks/python/apache_beam/pipeline.py index ec8dde4..79480d7 100644 --- a/sdks/python/apache_beam/pipeline.py +++ b/sdks/python/apache_beam/pipeline.py @@ -53,11 +53,11 @@ import shutil import tempfile from apache_beam import pvalue -from apache_beam import typehints from apache_beam.internal import pickler from apache_beam.runners import create_runner from apache_beam.runners import PipelineRunner from apache_beam.transforms import ptransform +from apache_beam.typehints import typehints from apache_beam.typehints import TypeCheckError from apache_beam.options.pipeline_options import PipelineOptions from apache_beam.options.pipeline_options import SetupOptions http://git-wip-us.apache.org/repos/asf/beam/blob/7881fd5e/sdks/python/apache_beam/transforms/core.py ---------------------------------------------------------------------- diff --git a/sdks/python/apache_beam/transforms/core.py b/sdks/python/apache_beam/transforms/core.py index a1964cf..abe699f 100644 --- a/sdks/python/apache_beam/transforms/core.py +++ b/sdks/python/apache_beam/transforms/core.py @@ -40,15 +40,15 @@ from apache_beam.transforms.window import TimestampedValue from apache_beam.transforms.window import GlobalWindows from apache_beam.transforms.window import WindowFn from apache_beam.typehints import Any -from apache_beam.typehints import get_type_hints -from apache_beam.typehints import is_consistent_with from apache_beam.typehints import Iterable from apache_beam.typehints import KV from apache_beam.typehints import trivial_inference -from apache_beam.typehints import TypeCheckError from apache_beam.typehints import Union -from apache_beam.typehints import WithTypeHints +from apache_beam.typehints.decorators import get_type_hints +from apache_beam.typehints.decorators import TypeCheckError +from apache_beam.typehints.decorators import WithTypeHints from apache_beam.typehints.trivial_inference import element_type +from apache_beam.typehints.typehints import is_consistent_with from apache_beam.utils import urns from apache_beam.options.pipeline_options import TypeOptions http://git-wip-us.apache.org/repos/asf/beam/blob/7881fd5e/sdks/python/apache_beam/transforms/ptransform.py ---------------------------------------------------------------------- diff --git a/sdks/python/apache_beam/transforms/ptransform.py b/sdks/python/apache_beam/transforms/ptransform.py index d1f9835..8898c36 100644 --- a/sdks/python/apache_beam/transforms/ptransform.py +++ b/sdks/python/apache_beam/transforms/ptransform.py @@ -46,16 +46,16 @@ from google.protobuf import wrappers_pb2 from apache_beam import error from apache_beam import pvalue -from apache_beam import typehints from apache_beam.internal import pickler from apache_beam.internal import util from apache_beam.transforms.display import HasDisplayData from apache_beam.transforms.display import DisplayDataItem -from apache_beam.typehints import getcallargs_forhints -from apache_beam.typehints import TypeCheckError -from apache_beam.typehints import validate_composite_type_param -from apache_beam.typehints import WithTypeHints +from apache_beam.typehints import typehints +from apache_beam.typehints.decorators import getcallargs_forhints +from apache_beam.typehints.decorators import TypeCheckError +from apache_beam.typehints.decorators import WithTypeHints from apache_beam.typehints.trivial_inference import instance_to_type +from apache_beam.typehints.typehints import validate_composite_type_param from apache_beam.utils import proto_utils from apache_beam.utils import urns @@ -491,7 +491,7 @@ class PTransformWithSideInputs(PTransform): """ def __init__(self, fn, *args, **kwargs): - if isinstance(fn, type) and issubclass(fn, typehints.WithTypeHints): + if isinstance(fn, type) and issubclass(fn, WithTypeHints): # Don't treat Fn class objects as callables. raise ValueError('Use %s() not %s.' % (fn.__name__, fn.__name__)) self.fn = self.make_fn(fn) @@ -577,7 +577,7 @@ class PTransformWithSideInputs(PTransform): continue if not typehints.is_consistent_with( bindings.get(arg, typehints.Any), hint): - raise typehints.TypeCheckError( + raise TypeCheckError( 'Type hint violation for \'%s\': requires %s but got %s for %s' % (self.label, hint, bindings[arg], arg)) http://git-wip-us.apache.org/repos/asf/beam/blob/7881fd5e/sdks/python/apache_beam/typehints/decorators.py ---------------------------------------------------------------------- diff --git a/sdks/python/apache_beam/typehints/decorators.py b/sdks/python/apache_beam/typehints/decorators.py index 4eabdba..6ed388a 100644 --- a/sdks/python/apache_beam/typehints/decorators.py +++ b/sdks/python/apache_beam/typehints/decorators.py @@ -86,11 +86,20 @@ defined, or before importing a module containing type-hinted functions. import inspect import types -from apache_beam.typehints import check_constraint -from apache_beam.typehints import CompositeTypeHintError -from apache_beam.typehints import SimpleTypeHintError from apache_beam.typehints import typehints -from apache_beam.typehints import validate_composite_type_param +from apache_beam.typehints.typehints import check_constraint +from apache_beam.typehints.typehints import CompositeTypeHintError +from apache_beam.typehints.typehints import SimpleTypeHintError +from apache_beam.typehints.typehints import validate_composite_type_param + + +__all__ = [ + 'with_input_types', + 'with_output_types', + 'WithTypeHints', + 'TypeCheckError', +] + # This is missing in the builtin types module. str.upper is arbitrary, any # method on a C-implemented type will do. http://git-wip-us.apache.org/repos/asf/beam/blob/7881fd5e/sdks/python/apache_beam/typehints/opcodes.py ---------------------------------------------------------------------- diff --git a/sdks/python/apache_beam/typehints/opcodes.py b/sdks/python/apache_beam/typehints/opcodes.py index 042acc0..83f444c 100644 --- a/sdks/python/apache_beam/typehints/opcodes.py +++ b/sdks/python/apache_beam/typehints/opcodes.py @@ -23,6 +23,8 @@ FrameState object, the second the integer opcode argument. Bytecodes with more complicated behavior (e.g. modifying the program counter) are handled inline rather than here. + +For internal use only; no backwards-compatibility guarantees. """ import types http://git-wip-us.apache.org/repos/asf/beam/blob/7881fd5e/sdks/python/apache_beam/typehints/trivial_inference.py ---------------------------------------------------------------------- diff --git a/sdks/python/apache_beam/typehints/trivial_inference.py b/sdks/python/apache_beam/typehints/trivial_inference.py index 4581aa1..977ea06 100644 --- a/sdks/python/apache_beam/typehints/trivial_inference.py +++ b/sdks/python/apache_beam/typehints/trivial_inference.py @@ -16,6 +16,8 @@ # """Trivial type inference for simple functions. + +For internal use only; no backwards-compatibility guarantees. """ import __builtin__ import collections http://git-wip-us.apache.org/repos/asf/beam/blob/7881fd5e/sdks/python/apache_beam/typehints/typecheck.py ---------------------------------------------------------------------- diff --git a/sdks/python/apache_beam/typehints/typecheck.py b/sdks/python/apache_beam/typehints/typecheck.py index 160d104..09b73f9 100644 --- a/sdks/python/apache_beam/typehints/typecheck.py +++ b/sdks/python/apache_beam/typehints/typecheck.py @@ -15,7 +15,10 @@ # limitations under the License. # -"""Runtime type checking support.""" +"""Runtime type checking support. + +For internal use only; no backwards-compatibility guarantees. +""" import collections import inspect @@ -25,13 +28,13 @@ import types from apache_beam.pvalue import TaggedOutput from apache_beam.transforms.core import DoFn from apache_beam.transforms.window import WindowedValue -from apache_beam.typehints import check_constraint -from apache_beam.typehints import CompositeTypeHintError -from apache_beam.typehints import GeneratorWrapper -from apache_beam.typehints import SimpleTypeHintError -from apache_beam.typehints import TypeCheckError from apache_beam.typehints.decorators import _check_instance_type from apache_beam.typehints.decorators import getcallargs_forhints +from apache_beam.typehints.decorators import GeneratorWrapper +from apache_beam.typehints.decorators import TypeCheckError +from apache_beam.typehints.typehints import check_constraint +from apache_beam.typehints.typehints import CompositeTypeHintError +from apache_beam.typehints.typehints import SimpleTypeHintError class AbstractDoFnWrapper(DoFn): http://git-wip-us.apache.org/repos/asf/beam/blob/7881fd5e/sdks/python/apache_beam/typehints/typehints.py ---------------------------------------------------------------------- diff --git a/sdks/python/apache_beam/typehints/typehints.py b/sdks/python/apache_beam/typehints/typehints.py index 9b41adb..cc430be 100644 --- a/sdks/python/apache_beam/typehints/typehints.py +++ b/sdks/python/apache_beam/typehints/typehints.py @@ -68,6 +68,24 @@ import copy import types +__all__ = [ + 'Any', + 'Union', + 'Optional', + 'Tuple', + 'Tuple', + 'List', + 'KV', + 'Dict', + 'Set', + 'Iterable', + 'Iterator', + 'Generator', + 'WindowedValue', + 'TypeVariable', +] + + # A set of the built-in Python types we don't support, guiding the users # to templated (upper-case) versions instead. DISALLOWED_PRIMITIVE_TYPES = (list, set, tuple, dict) http://git-wip-us.apache.org/repos/asf/beam/blob/7881fd5e/sdks/python/apache_beam/typehints/typehints_test.py ---------------------------------------------------------------------- diff --git a/sdks/python/apache_beam/typehints/typehints_test.py b/sdks/python/apache_beam/typehints/typehints_test.py index f90b5e9..f1b92e0 100644 --- a/sdks/python/apache_beam/typehints/typehints_test.py +++ b/sdks/python/apache_beam/typehints/typehints_test.py @@ -21,9 +21,8 @@ import inspect import unittest -import apache_beam.typehints as typehints +import apache_beam.typehints.typehints as typehints from apache_beam.typehints import Any -from apache_beam.typehints import is_consistent_with from apache_beam.typehints import Tuple from apache_beam.typehints import TypeCheckError from apache_beam.typehints import Union @@ -34,6 +33,8 @@ from apache_beam.typehints.decorators import _interleave_type_check from apache_beam.typehints.decorators import _positional_arg_hints from apache_beam.typehints.decorators import get_type_hints from apache_beam.typehints.decorators import getcallargs_forhints +from apache_beam.typehints.decorators import GeneratorWrapper +from apache_beam.typehints.typehints import is_consistent_with def check_or_interleave(hint, value, var): @@ -712,7 +713,7 @@ class TestGeneratorWrapper(TypeHintTestCase): l = [] interleave_func = lambda x: l.append(x) - wrapped_gen = typehints.GeneratorWrapper(count(4), interleave_func) + wrapped_gen = GeneratorWrapper(count(4), interleave_func) # Should function as a normal generator. self.assertEqual(0, next(wrapped_gen)) @@ -1032,12 +1033,12 @@ class CombinedReturnsAndTakesTestCase(TypeHintTestCase): class DecoratorHelpers(TypeHintTestCase): def test_hint_helper(self): - self.assertTrue(typehints.is_consistent_with(Any, int)) - self.assertTrue(typehints.is_consistent_with(int, Any)) - self.assertTrue(typehints.is_consistent_with(str, object)) - self.assertFalse(typehints.is_consistent_with(object, str)) - self.assertTrue(typehints.is_consistent_with(str, Union[str, int])) - self.assertFalse(typehints.is_consistent_with(Union[str, int], str)) + self.assertTrue(is_consistent_with(Any, int)) + self.assertTrue(is_consistent_with(int, Any)) + self.assertTrue(is_consistent_with(str, object)) + self.assertFalse(is_consistent_with(object, str)) + self.assertTrue(is_consistent_with(str, Union[str, int])) + self.assertFalse(is_consistent_with(Union[str, int], str)) def test_positional_arg_hints(self): self.assertEquals(typehints.Any, _positional_arg_hints('x', {}))
