| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 883559f commit ef8c086
2 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -497,7 +497,9 @@ def assert_shallow_structure(shallow_tree, input_tree, check_types=True): | |||
| 497 | 497 | shallow_tree: an arbitrarily nested structure. | |
| 498 | 498 | input_tree: an arbitrarily nested structure. | |
| 499 | 499 | 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. | ||
| 501 | 503 | ||
| 502 | 504 | Raises: | |
| 503 | 505 | 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): | |||
| 513 | 515 | "Input has type: %s." % type(input_tree)) | |
| 514 | 516 | ||
| 515 | 517 | 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))) | ||
| 520 | 533 | ||
| 521 | 534 | if len(input_tree) != len(shallow_tree): | |
| 522 | 535 | raise ValueError( | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -429,6 +429,15 @@ def testAssertShallowStructure(self): | |||
| 429 | 429 | inp_ba = collections.OrderedDict([("b", (2, 3)), ("a", 1)]) | |
| 430 | 430 | nest.assert_shallow_structure(inp_ab, inp_ba) | |
| 431 | 431 | ||
| 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 | + | ||
| 432 | 441 | def testFlattenUpTo(self): | |
| 433 | 442 | # Shallow tree ends at scalar. | |
| 434 | 443 | input_tree = [[[2, 2], [3, 3]], [[4, 9], [5, 5]]] | |
| Back | FazBrowse Home | New Git URL |
0 commit comments