You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@beam.apache.org by cc...@apache.org on 2019/02/20 23:19:25 UTC

[beam] branch release-2.11.0 updated: Clean up Python version casework in native_type_compatibilty.py

This is an automated email from the ASF dual-hosted git repository.

ccy 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 bde620a  Clean up Python version casework in native_type_compatibilty.py
     new 54520e1  Merge pull request #7909 from charlesccychen/cp-7902
bde620a is described below

commit bde620aac8facd3d855346b0ada17f484bdc16bd
Author: Charles Chen <cc...@google.com>
AuthorDate: Tue Feb 19 23:34:58 2019 -0800

    Clean up Python version casework in native_type_compatibilty.py
---
 .../typehints/native_type_compatibility.py         | 39 +++++++++++-----------
 1 file changed, 20 insertions(+), 19 deletions(-)

diff --git a/sdks/python/apache_beam/typehints/native_type_compatibility.py b/sdks/python/apache_beam/typehints/native_type_compatibility.py
index 05c7393..d08049a 100644
--- a/sdks/python/apache_beam/typehints/native_type_compatibility.py
+++ b/sdks/python/apache_beam/typehints/native_type_compatibility.py
@@ -36,18 +36,27 @@ _TypeMapEntry = collections.namedtuple(
     '_TypeMapEntry', ['match', 'arity', 'beam_type'])
 
 
+def _get_compatible_args(typ):
+  # 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__ argument respectively.
+  if (3, 0, 0) <= sys.version_info[0:3] < (3, 5, 3):
+    if getattr(typ, '__tuple_params__', None) is not None:
+      return typ.__tuple_params__
+    elif getattr(typ, '__union_params__', None) is not None:
+      return typ.__union_params__
+  return None
+
+
 def _get_arg(typ, index):
   """Returns the index-th argument to the given type."""
   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]
+    compatible_args = _get_compatible_args(typ)
+    if compatible_args is None:
+      raise
+    return compatible_args[index]
 
 
 def _len_arg(typ):
@@ -55,18 +64,10 @@ 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
+    compatible_args = _get_compatible_args(typ)
+    if compatible_args is None:
+      return 0
+    return len(compatible_args)
 
 
 def _safe_issubclass(derived, parent):