Short circuit assignability checks on identity
This is a performance-motivated change.
@@ -341,6 +341,8 @@ class TensorType(Type, metaclass=_Intern):
tensor_utils.same_shape(self._shape, other.shape)))
def is_assignable_from(self, source_type: 'Type') -> bool:
+ if self is source_type:
+ return True
if (not isinstance(source_type, TensorType) or
self.dtype != source_type.dtype):
return False
@@ -456,6 +458,8 @@ class StructType(structure.Struct, Type, metaclass=_Intern):
structure.Struct.__eq__(self, other))
def is_assignable_from(self, source_type: 'Type') -> bool:
+ if self is source_type:
+ return True
if not isinstance(source_type, StructType):
return False
target_elements = structure.to_elements(self)
@@ -552,6 +556,8 @@ class SequenceType(Type, metaclass=_Intern):
self._element == other.element))
def is_assignable_from(self, source_type: 'Type') -> bool:
+ if self is source_type:
+ return True
return ((isinstance(source_type, SequenceType) and
self.element.is_assignable_from(source_type.element)))
@@ -605,6 +611,8 @@ class FunctionType(Type, metaclass=_Intern):
self._result == other.result))
def is_assignable_from(self, source_type: 'Type') -> bool:
+ if self is source_type:
+ return True
if not isinstance(source_type, FunctionType):
return False
if (self.parameter is None) != (source_type.parameter is None):
@@ -690,6 +698,8 @@ class PlacementType(Type, metaclass=_Intern):
return (self is other) or isinstance(other, PlacementType)
def is_assignable_from(self, source_type: 'Type') -> bool:
+ if self is source_type:
+ return True
return isinstance(source_type, PlacementType)
@@ -764,6 +774,8 @@ class FederatedType(Type, metaclass=_Intern):
self._all_equal == other.all_equal))
def is_assignable_from(self, source_type: 'Type') -> bool:
+ if self is source_type:
+ return True
return (isinstance(source_type, FederatedType) and
self.member.is_assignable_from(source_type.member) and
(not self.all_equal or source_type.all_equal) and
