MCPcopy Create free account
hub / github.com/aboutcode-org/scancode-toolkit / _transform_attrs

Function _transform_attrs

src/licensedcode/_vendor/attr/_make.py:512–618  ·  view source on GitHub ↗

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
)

Source from the content-addressed store, hash-verified

510
511
512def _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 ),

Callers 1

__init__Method · 0.85

Calls 15

_get_annotationsFunction · 0.85
_is_class_varFunction · 0.85
attribFunction · 0.85
_collect_base_attrsFunction · 0.85
_make_attr_tuple_classFunction · 0.85
addMethod · 0.80
from_counting_attrMethod · 0.80
evolveMethod · 0.80
iteritemsFunction · 0.70
getMethod · 0.65

Tested by

no test coverage detected