FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Make namedtuples with identical name and field names to be considered… · codeplaysoftware/tensorflow@ef8c086 · GitHub

Repository navigation

Commit ef8c086

Browse files
Make namedtuples with identical name and field names to be considered as the same shallow structure in assert_shallow_structure
PiperOrigin-RevId: 184687609
1 parent 883559f commit ef8c086

2 files changed

Lines changed: 27 additions & 5 deletions

File tree

‎tensorflow/python/util/nest.py‎

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,9 @@ def assert_shallow_structure(shallow_tree, input_tree, check_types=True):
497497
shallow_tree: an arbitrarily nested structure.
498498
input_tree: an arbitrarily nested structure.
499499
check_types: if `True` (default) the sequence types of `shallow_tree` and
500-
`input_tree` have to be the same.
500+
`input_tree` have to be the same. Note that even with check_types==True,
501+
this function will consider two different namedtuple classes with the same
502+
name and _fields attribute to be the same class.
501503
502504
Raises:
503505
TypeError: If `shallow_tree` is a sequence but `input_tree` is not.
@@ -513,10 +515,21 @@ def assert_shallow_structure(shallow_tree, input_tree, check_types=True):
513515
"Input has type: %s." % type(input_tree))
514516

515517
if check_types and not isinstance(input_tree, type(shallow_tree)):
516-
raise TypeError(
517-
"The two structures don't have the same sequence type. Input "
518-
"structure has type %s, while shallow structure has type %s."
519-
% (type(input_tree), type(shallow_tree)))
518+
# Duck-typing means that nest should be fine with two different
519+
# namedtuples with identical name and fields.
520+
shallow_is_namedtuple = _is_namedtuple(shallow_tree, False)
521+
input_is_namedtuple = _is_namedtuple(input_tree, False)
522+
if shallow_is_namedtuple and input_is_namedtuple:
523+
if not _same_namedtuples(shallow_tree, input_tree):
524+
raise TypeError(
525+
"The two namedtuples don't have the same sequence type. Input "
526+
"structure has type %s, while shallow structure has type %s."
527+
% (type(input_tree), type(shallow_tree)))
528+
else:
529+
raise TypeError(
530+
"The two structures don't have the same sequence type. Input "
531+
"structure has type %s, while shallow structure has type %s."
532+
% (type(input_tree), type(shallow_tree)))
520533

521534
if len(input_tree) != len(shallow_tree):
522535
raise ValueError(

‎tensorflow/python/util/nest_test.py‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,15 @@ def testAssertShallowStructure(self):
429429
inp_ba = collections.OrderedDict([("b", (2, 3)), ("a", 1)])
430430
nest.assert_shallow_structure(inp_ab, inp_ba)
431431

432+
# This assertion is expected to pass: two namedtuples with the same
433+
# name and field names are considered to be identical.
434+
same_name_type_0 = collections.namedtuple("same_name", ("a", "b"))
435+
same_name_type_1 = collections.namedtuple("same_name", ("a", "b"))
436+
inp_shallow = same_name_type_0(1, 2)
437+
inp_deep = same_name_type_1(1, [1, 2, 3])
438+
nest.assert_shallow_structure(inp_shallow, inp_deep, check_types=False)
439+
nest.assert_shallow_structure(inp_shallow, inp_deep, check_types=True)
440+
432441
def testFlattenUpTo(self):
433442
# Shallow tree ends at scalar.
434443
input_tree = [[[2, 2], [3, 3]], [[4, 9], [5, 5]]]

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL