Transform all `_CountingAttr`s on a class into `Attribute`s. If *these* is passed, use that and don't look for them on the class. *collect_by_mro* is True, collect them in the correct MRO order, otherwise use the old -- incorrect -- order. See #428. Return an `_Attributes`.
(
cls, these, auto_attribs, kw_only, collect_by_mro, field_transformer
)
| 510 | |
| 511 | |
| 512 | def _transform_attrs( |
| 513 | cls, these, auto_attribs, kw_only, collect_by_mro, field_transformer |
| 514 | ): |
| 515 | """ |
| 516 | Transform all `_CountingAttr`s on a class into `Attribute`s. |
| 517 | |
| 518 | If *these* is passed, use that and don't look for them on the class. |
| 519 | |
| 520 | *collect_by_mro* is True, collect them in the correct MRO order, otherwise |
| 521 | use the old -- incorrect -- order. See #428. |
| 522 | |
| 523 | Return an `_Attributes`. |
| 524 | """ |
| 525 | cd = cls.__dict__ |
| 526 | anns = _get_annotations(cls) |
| 527 | |
| 528 | if these is not None: |
| 529 | ca_list = [(name, ca) for name, ca in iteritems(these)] |
| 530 | |
| 531 | if not isinstance(these, ordered_dict): |
| 532 | ca_list.sort(key=_counter_getter) |
| 533 | elif auto_attribs is True: |
| 534 | ca_names = { |
| 535 | name |
| 536 | for name, attr in cd.items() |
| 537 | if isinstance(attr, _CountingAttr) |
| 538 | } |
| 539 | ca_list = [] |
| 540 | annot_names = set() |
| 541 | for attr_name, type in anns.items(): |
| 542 | if _is_class_var(type): |
| 543 | continue |
| 544 | annot_names.add(attr_name) |
| 545 | a = cd.get(attr_name, NOTHING) |
| 546 | |
| 547 | if not isinstance(a, _CountingAttr): |
| 548 | if a is NOTHING: |
| 549 | a = attrib() |
| 550 | else: |
| 551 | a = attrib(default=a) |
| 552 | ca_list.append((attr_name, a)) |
| 553 | |
| 554 | unannotated = ca_names - annot_names |
| 555 | if len(unannotated) > 0: |
| 556 | raise UnannotatedAttributeError( |
| 557 | "The following `attr.ib`s lack a type annotation: " |
| 558 | + ", ".join( |
| 559 | sorted(unannotated, key=lambda n: cd.get(n).counter) |
| 560 | ) |
| 561 | + "." |
| 562 | ) |
| 563 | else: |
| 564 | ca_list = sorted( |
| 565 | ( |
| 566 | (name, attr) |
| 567 | for name, attr in cd.items() |
| 568 | if isinstance(attr, _CountingAttr) |
| 569 | ), |
no test coverage detected