More compatibility fixes for typing.Generic:

* types.new_class is required in some distributions
 * avoid calling isinstance on some function objects in python 3.6
Required for #40132.

PiperOrigin-RevId: 316195553
Change-Id: I59600054e7a7c35036875ade2f3da6ee5f7e0809
This commit is contained in:
Dan Moldovan 2020-06-12 16:08:22 -07:00 committed by TensorFlower Gardener
parent f60370876c
commit eb7d642789
2 changed files with 15 additions and 5 deletions

View File

@ -732,6 +732,8 @@ def assert_no_new_tensors(f):
"""Finds existing Tensors, runs the test, checks for new Tensors."""
def _is_tensorflow_object(obj):
if not hasattr(obj, "__class__"):
return False
try:
return isinstance(obj,
(ops.Tensor, variables.Variable,

View File

@ -21,15 +21,12 @@ import copy
import sys
import textwrap
import traceback
import six # pylint: disable=unused-import
import types
from tensorflow.python.eager import context
from tensorflow.python.framework import ops
from tensorflow.python.platform import tf_logging
from tensorflow.python.util import tf_decorator
# pylint: enable=g-bad-import-order,g-import-not-at-top
class _TFShouldUseHelper(object):
@ -154,7 +151,18 @@ def _get_wrapper(x, tf_should_use_helper):
tx = copy.deepcopy(type_x)
# Prefer using __orig_bases__, which preserve generic type arguments.
bases = getattr(tx, '__orig_bases__', tx.__bases__)
copy_tx = type(tx.__name__, bases, dict(tx.__dict__))
# Use types.new_class when available, which is preferred over plain type in
# some distributions.
if sys.version_info >= (3, 5):
def set_body(ns):
ns.update(tx.__dict__)
return ns
copy_tx = types.new_class(tx.__name__, bases, exec_body=set_body)
else:
copy_tx = type(tx.__name__, bases, dict(tx.__dict__))
copy_tx.__init__ = _new__init__
copy_tx.__getattribute__ = _new__getattribute__
copy_tx.mark_used = _new_mark_used