beam-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From al...@apache.org
Subject [beam] branch release-2.11.0 updated: Fix typehinting for Python 3 versions < 3.5.3
Date Wed, 20 Feb 2019 20:49:41 GMT
This is an automated email from the ASF dual-hosted git repository.

altay pushed a commit to branch release-2.11.0
in repository https://gitbox.apache.org/repos/asf/beam.git


The following commit(s) were added to refs/heads/release-2.11.0 by this push:
     new 2352b99  Fix typehinting for Python 3 versions < 3.5.3
     new cc72017  Merge pull request #7900 from tvalentyn/py352_fix
2352b99 is described below

commit 2352b99bd88563ab31ceda0d9f450ddbadc4772c
Author: robbe <robbe.sneyders@ml6.eu>
AuthorDate: Tue Feb 19 17:12:00 2019 +0100

    Fix typehinting for Python 3 versions < 3.5.3
---
 .../typehints/native_type_compatibility.py         | 22 +++++++++++++++++++++-
 .../typehints/native_type_compatibility_test.py    | 10 ----------
 .../apache_beam/typehints/typed_pipeline_test.py   |  6 ------
 3 files changed, 21 insertions(+), 17 deletions(-)

diff --git a/sdks/python/apache_beam/typehints/native_type_compatibility.py b/sdks/python/apache_beam/typehints/native_type_compatibility.py
index 87fb2c8..05c7393 100644
--- a/sdks/python/apache_beam/typehints/native_type_compatibility.py
+++ b/sdks/python/apache_beam/typehints/native_type_compatibility.py
@@ -20,6 +20,7 @@
 from __future__ import absolute_import
 
 import collections
+import sys
 import typing
 from builtins import next
 from builtins import range
@@ -37,7 +38,16 @@ _TypeMapEntry = collections.namedtuple(
 
 def _get_arg(typ, index):
   """Returns the index-th argument to the given type."""
-  return typ.__args__[index]
+  try:
+    return typ.__args__[index]
+  except AttributeError:
+    if (3, 0, 0) <= sys.version_info[0:3] < (3, 5, 3):
+      # On Python versions < 3.5.3, the Tuple and Union type from typing do
+      # not have an __args__ attribute, but a __tuple_params__, and a
+      # __union_params__ and __union_set_params__ argument respectively.
+      args = next(value for key, value in typ.__dict__.items()
+                  if key.endswith('_params__') and 'set' not in key)
+      return args[index]
 
 
 def _len_arg(typ):
@@ -45,6 +55,16 @@ def _len_arg(typ):
   try:
     return len(typ.__args__)
   except AttributeError:
+    if (3, 0, 0) <= sys.version_info[0:3] < (3, 5, 3):
+      # On Python versions < 3.5.3, the Tuple and Union type from typing do
+      # not have an __args__ attribute, but a __tuple_params__, and a
+      # __union_params__ and __union_set_params__ argument respectively.
+      try:
+        args = next(value for key, value in typ.__dict__.items()
+                    if key.endswith('_params__') and 'set' not in key)
+        return len(args)
+      except StopIteration:
+        return 0
     # For Any type, which takes no arguments.
     return 0
 
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 71d54a1..fe0f733 100644
--- a/sdks/python/apache_beam/typehints/native_type_compatibility_test.py
+++ b/sdks/python/apache_beam/typehints/native_type_compatibility_test.py
@@ -19,8 +19,6 @@
 
 from __future__ import absolute_import
 
-import os
-import sys
 import typing
 import unittest
 
@@ -39,10 +37,6 @@ class _TestClass(object):
 
 class NativeTypeCompatibilityTest(unittest.TestCase):
 
-  @unittest.skipIf(sys.hexversion < 0x030503F0 and
-                   os.environ.get('RUN_SKIPPED_PY3_TESTS') != '1',
-                   'Tuple typehinting depends on typing changes in Python '
-                   '3.5.3')
   def test_convert_to_beam_type(self):
     test_cases = [
         ('raw bytes', bytes, bytes),
@@ -92,10 +86,6 @@ class NativeTypeCompatibilityTest(unittest.TestCase):
         typehints.List[typing.Dict[int, str]],
         typehints.List[typehints.Dict[int, str]])
 
-  @unittest.skipIf(sys.hexversion < 0x030503F0 and
-                   os.environ.get('RUN_SKIPPED_PY3_TESTS') != '1',
-                   'Tuple typehinting depends on typing changes in Python '
-                   '3.5.3')
   def test_convert_to_beam_types(self):
     typing_types = [bytes, typing.List[bytes],
                     typing.List[typing.Tuple[bytes, int]],
diff --git a/sdks/python/apache_beam/typehints/typed_pipeline_test.py b/sdks/python/apache_beam/typehints/typed_pipeline_test.py
index 10c5e19..b630307 100644
--- a/sdks/python/apache_beam/typehints/typed_pipeline_test.py
+++ b/sdks/python/apache_beam/typehints/typed_pipeline_test.py
@@ -19,8 +19,6 @@
 
 from __future__ import absolute_import
 
-import os
-import sys
 import typing
 import unittest
 
@@ -104,10 +102,6 @@ class MainInputTest(unittest.TestCase):
       [1, 2, 3] | (beam.ParDo(my_do_fn) | 'again' >> beam.ParDo(my_do_fn))
 
 
-@unittest.skipIf(sys.hexversion < 0x030503F0 and
-                 os.environ.get('RUN_SKIPPED_PY3_TESTS') != '1',
-                 'Tuple typehinting depends on typing changes in Python '
-                 '3.5.3')
 class NativeTypesTest(unittest.TestCase):
 
   def test_good_main_input(self):


Mime
View raw message