[ 
https://issues.apache.org/jira/browse/BEAM-4007?focusedWorklogId=118492&page=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel#worklog-118492
 ]

ASF GitHub Bot logged work on BEAM-4007:
----------------------------------------

                Author: ASF GitHub Bot
            Created on: 02/Jul/18 23:55
            Start Date: 02/Jul/18 23:55
    Worklog Time Spent: 10m 
      Work Description: charlesccychen closed pull request #5337: [BEAM-4007] 
Futurize typehints subpackage
URL: https://github.com/apache/beam/pull/5337
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/sdks/python/apache_beam/typehints/__init__.py 
b/sdks/python/apache_beam/typehints/__init__.py
index e89afa1285a..23d0b40d07f 100644
--- a/sdks/python/apache_beam/typehints/__init__.py
+++ b/sdks/python/apache_beam/typehints/__init__.py
@@ -17,6 +17,8 @@
 
 """A package defining the syntax and decorator semantics for type-hints."""
 
+from __future__ import absolute_import
+
 # pylint: disable=wildcard-import
 from apache_beam.typehints.typehints import *
 from apache_beam.typehints.decorators import *
diff --git a/sdks/python/apache_beam/typehints/decorators.py 
b/sdks/python/apache_beam/typehints/decorators.py
index 88160c04660..6604cf12081 100644
--- a/sdks/python/apache_beam/typehints/decorators.py
+++ b/sdks/python/apache_beam/typehints/decorators.py
@@ -83,8 +83,13 @@ def foo((a, b)):
 defined, or before importing a module containing type-hinted functions.
 """
 
+from __future__ import absolute_import
+
 import inspect
 import types
+from builtins import next
+from builtins import object
+from builtins import zip
 
 from apache_beam.typehints import native_type_compatibility
 from apache_beam.typehints import typehints
@@ -175,7 +180,7 @@ def with_defaults(self, hints):
     return IOTypeHints(self.input_types or hints.input_types,
                        self.output_types or hints.output_types)
 
-  def __nonzero__(self):
+  def __bool__(self):
     return bool(self.input_types or self.output_types)
 
   def __repr__(self):
@@ -404,7 +409,7 @@ def with_output_types(*return_type_hint, **kwargs):
     from apache_beam.typehints import with_output_types
     from apache_beam.typehints import Set
 
-    class Coordinate:
+    class Coordinate(object):
       def __init__(self, x, y):
         self.x = x
         self.y = y
diff --git a/sdks/python/apache_beam/typehints/native_type_compatibility.py 
b/sdks/python/apache_beam/typehints/native_type_compatibility.py
index 0be931e8fe2..87fb2c80816 100644
--- a/sdks/python/apache_beam/typehints/native_type_compatibility.py
+++ b/sdks/python/apache_beam/typehints/native_type_compatibility.py
@@ -17,8 +17,12 @@
 
 """Module to convert Python's native typing types to Beam types."""
 
+from __future__ import absolute_import
+
 import collections
 import typing
+from builtins import next
+from builtins import range
 
 from apache_beam.typehints import typehints
 
diff --git 
a/sdks/python/apache_beam/typehints/native_type_compatibility_test.py 
b/sdks/python/apache_beam/typehints/native_type_compatibility_test.py
index 4171507f345..2abde69a95f 100644
--- a/sdks/python/apache_beam/typehints/native_type_compatibility_test.py
+++ b/sdks/python/apache_beam/typehints/native_type_compatibility_test.py
@@ -17,6 +17,8 @@
 
 """Test for Beam type compatibility library."""
 
+from __future__ import absolute_import
+
 import typing
 import unittest
 
diff --git a/sdks/python/apache_beam/typehints/opcodes.py 
b/sdks/python/apache_beam/typehints/opcodes.py
index 252bcf50e35..a9874cfbda3 100644
--- a/sdks/python/apache_beam/typehints/opcodes.py
+++ b/sdks/python/apache_beam/typehints/opcodes.py
@@ -33,8 +33,6 @@
 import types
 from functools import reduce
 
-import six
-
 from . import typehints
 from .trivial_inference import BoundMethod
 from .trivial_inference import Const
@@ -47,6 +45,11 @@
 from .typehints import Tuple
 from .typehints import Union
 
+try:                # Python 2
+  unicode           # pylint: disable=unicode-builtin
+except NameError:   # Python 3
+  unicode = str
+
 
 def pop_one(state, unused_arg):
   del state.stack[-1:]
@@ -152,7 +155,7 @@ def binary_true_divide(state, unused_arg):
 def binary_subscr(state, unused_arg):
   index = state.stack.pop()
   base = state.stack.pop()
-  if base in (str, six.text_type):
+  if base in (str, unicode):
     out = base
   elif (isinstance(index, Const) and isinstance(index.value, int)
         and isinstance(base, typehints.TupleHint.TupleConstraint)):
diff --git a/sdks/python/apache_beam/typehints/trivial_inference.py 
b/sdks/python/apache_beam/typehints/trivial_inference.py
index 92770fba10a..d8181ad7c78 100644
--- a/sdks/python/apache_beam/typehints/trivial_inference.py
+++ b/sdks/python/apache_beam/typehints/trivial_inference.py
@@ -28,11 +28,19 @@
 import pprint
 import sys
 import types
+from builtins import object
+from builtins import zip
 from functools import reduce
 
 from apache_beam.typehints import Any
 from apache_beam.typehints import typehints
-from six.moves import builtins
+
+# pylint: disable=wrong-import-order, wrong-import-position, ungrouped-imports
+try:                  # Python 2
+  import __builtin__ as builtins
+except ImportError:   # Python 3
+  import builtins
+# pylint: enable=wrong-import-order, wrong-import-position, ungrouped-imports
 
 
 class TypeInferenceError(ValueError):
@@ -115,6 +123,9 @@ def __init__(self, f, local_vars=None, stack=()):
   def __eq__(self, other):
     return isinstance(other, FrameState) and self.__dict__ == other.__dict__
 
+  def __hash__(self):
+    return hash(tuple(sorted(self.__dict__.items())))
+
   def copy(self):
     return FrameState(self.f, self.vars, self.stack)
 
diff --git a/sdks/python/apache_beam/typehints/trivial_inference_test.py 
b/sdks/python/apache_beam/typehints/trivial_inference_test.py
index ad0a41c9059..9ce78fe4e5c 100644
--- a/sdks/python/apache_beam/typehints/trivial_inference_test.py
+++ b/sdks/python/apache_beam/typehints/trivial_inference_test.py
@@ -16,6 +16,9 @@
 #
 
 """Tests for apache_beam.typehints.trivial_inference."""
+
+from __future__ import absolute_import
+
 import unittest
 
 from apache_beam.typehints import trivial_inference
diff --git a/sdks/python/apache_beam/typehints/typecheck.py 
b/sdks/python/apache_beam/typehints/typecheck.py
index 8ee0269c6d6..0754ea9ed47 100644
--- a/sdks/python/apache_beam/typehints/typecheck.py
+++ b/sdks/python/apache_beam/typehints/typecheck.py
@@ -20,13 +20,13 @@
 For internal use only; no backwards-compatibility guarantees.
 """
 
+from __future__ import absolute_import
 
 import collections
 import inspect
-import sys
 import types
 
-import six
+from future.utils import raise_with_traceback
 
 from apache_beam import pipeline
 from apache_beam.pvalue import TaggedOutput
@@ -41,6 +41,11 @@
 from apache_beam.typehints.typehints import SimpleTypeHintError
 from apache_beam.typehints.typehints import check_constraint
 
+try:                # Python 2
+  unicode           # pylint: disable=unicode-builtin
+except NameError:   # Python 3
+  unicode = str
+
 
 class AbstractDoFnWrapper(DoFn):
   """An abstract class to create wrapper around DoFn"""
@@ -87,14 +92,14 @@ def wrapper(self, method, args, kwargs):
     except TypeCheckError as e:
       error_msg = ('Runtime type violation detected within ParDo(%s): '
                    '%s' % (self.full_label, e))
-      six.raise_from(TypeCheckError(error_msg), sys.exc_info()[2])
+      raise_with_traceback(TypeCheckError(error_msg))
     else:
       return self._check_type(result)
 
   def _check_type(self, output):
     if output is None:
       return output
-    elif isinstance(output, (dict,) + six.string_types):
+    elif isinstance(output, (dict, bytes, str, unicode)):
       object_type = type(output).__name__
       raise TypeCheckError('Returning a %s from a ParDo or FlatMap is '
                            'discouraged. Please use list("%s") if you really '
@@ -176,12 +181,12 @@ def _type_check(self, type_constraint, datum, is_input):
     try:
       check_constraint(type_constraint, datum)
     except CompositeTypeHintError as e:
-      six.raise_from(TypeCheckError(e.args[0]), sys.exc_info()[2])
+      raise_with_traceback(TypeCheckError(e.args[0]))
     except SimpleTypeHintError:
       error_msg = ("According to type-hint expected %s should be of type %s. "
                    "Instead, received '%s', an instance of type %s."
                    % (datum_type, type_constraint, datum, type(datum)))
-      six.raise_from(TypeCheckError(error_msg), sys.exc_info()[2])
+      raise_with_traceback(TypeCheckError(error_msg))
 
 
 class TypeCheckCombineFn(core.CombineFn):
@@ -206,7 +211,7 @@ def add_input(self, accumulator, element, *args, **kwargs):
       except TypeCheckError as e:
         error_msg = ('Runtime type violation detected within %s: '
                      '%s' % (self._label, e))
-        six.raise_from(TypeCheckError(error_msg), sys.exc_info()[2])
+        raise_with_traceback(TypeCheckError(error_msg))
     return self._combinefn.add_input(accumulator, element, *args, **kwargs)
 
   def merge_accumulators(self, accumulators, *args, **kwargs):
@@ -221,7 +226,7 @@ def extract_output(self, accumulator, *args, **kwargs):
       except TypeCheckError as e:
         error_msg = ('Runtime type violation detected within %s: '
                      '%s' % (self._label, e))
-        six.raise_from(TypeCheckError(error_msg), sys.exc_info()[2])
+        raise_with_traceback(TypeCheckError(error_msg))
     return result
 
 
diff --git a/sdks/python/apache_beam/typehints/typed_pipeline_test.py 
b/sdks/python/apache_beam/typehints/typed_pipeline_test.py
index 598847e023c..feee4862303 100644
--- a/sdks/python/apache_beam/typehints/typed_pipeline_test.py
+++ b/sdks/python/apache_beam/typehints/typed_pipeline_test.py
@@ -16,6 +16,9 @@
 #
 
 """Unit tests for the type-hint objects and decorators."""
+
+from __future__ import absolute_import
+
 import inspect
 import typing
 import unittest
diff --git a/sdks/python/apache_beam/typehints/typehints.py 
b/sdks/python/apache_beam/typehints/typehints.py
index af9e1fef70b..5da65fa352d 100644
--- a/sdks/python/apache_beam/typehints/typehints.py
+++ b/sdks/python/apache_beam/typehints/typehints.py
@@ -63,12 +63,16 @@
 
 """
 
+from __future__ import absolute_import
+
 import collections
 import copy
 import sys
 import types
+from builtins import next
+from builtins import zip
 
-import six
+from future.utils import with_metaclass
 
 __all__ = [
     'Any',
@@ -411,17 +415,25 @@ def __eq__(self, other):
   def __repr__(self):
     return 'Any'
 
+  def __hash__(self):
+    # TODO(BEAM - 3730)
+    return hash(id(self))
+
   def type_check(self, instance):
     pass
 
 
 class TypeVariable(AnyTypeConstraint):
 
+  def __init__(self, name):
+    self.name = name
+
   def __eq__(self, other):
     return type(self) == type(other) and self.name == other.name
 
-  def __init__(self, name):
-    self.name = name
+  def __hash__(self):
+    # TODO(BEAM - 3730)
+    return hash(id(self))
 
   def __repr__(self):
     return 'TypeVariable[%s]' % self.name
@@ -992,8 +1004,8 @@ def __getitem__(self, type_param):
 IteratorTypeConstraint = IteratorHint.IteratorTypeConstraint
 
 
[email protected]_metaclass(GetitemConstructor)
-class WindowedTypeConstraint(TypeConstraint):
+class WindowedTypeConstraint(with_metaclass(GetitemConstructor,
+                                            TypeConstraint)):
   """A type constraint for WindowedValue objects.
 
   Mostly for internal use.
diff --git a/sdks/python/apache_beam/typehints/typehints_test.py 
b/sdks/python/apache_beam/typehints/typehints_test.py
index 70ebcb3a3ff..53cdece0ce4 100644
--- a/sdks/python/apache_beam/typehints/typehints_test.py
+++ b/sdks/python/apache_beam/typehints/typehints_test.py
@@ -16,9 +16,14 @@
 #
 
 """Unit tests for the type-hint objects and decorators."""
+
+from __future__ import absolute_import
+
 import functools
 import inspect
 import unittest
+from builtins import next
+from builtins import range
 
 import apache_beam.typehints.typehints as typehints
 from apache_beam.typehints import Any
diff --git a/sdks/python/tox.ini b/sdks/python/tox.ini
index c80cefd24cb..b7b4f87c825 100644
--- a/sdks/python/tox.ini
+++ b/sdks/python/tox.ini
@@ -113,6 +113,7 @@ modules =
   apache_beam/pvalue_test
   apache_beam/testing
   apache_beam/tools
+  apache_beam/typehints
 commands =
   python --version
   pip --version


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


Issue Time Tracking
-------------------

    Worklog Id:     (was: 118492)
    Time Spent: 4h 10m  (was: 4h)

> Futurize and fix python 2 compatibility for typehints subpackage
> ----------------------------------------------------------------
>
>                 Key: BEAM-4007
>                 URL: https://issues.apache.org/jira/browse/BEAM-4007
>             Project: Beam
>          Issue Type: Sub-task
>          Components: sdk-py-core
>            Reporter: Robbe
>            Assignee: Robbe
>            Priority: Major
>          Time Spent: 4h 10m
>  Remaining Estimate: 0h
>




--
This message was sent by Atlassian JIRA
(v7.6.3#76005)

Reply via email to