MCPcopy Index your code
hub / github.com/python/mypy / _attributes_from_assignment

Function _attributes_from_assignment

mypy/plugins/attrs.py:539–574  ·  view source on GitHub ↗

Return Attribute objects that are created by this assignment. The assignments can look like this: x = attr.ib() x = y = attr.ib() x, y = attr.ib(), attr.ib() or if auto_attribs is enabled also like this: x: type x: type = default_value x: type

(
    ctx: mypy.plugin.ClassDefContext, stmt: AssignmentStmt, auto_attribs: bool, class_kw_only: bool
)

Source from the content-addressed store, hash-verified

537
538
539def _attributes_from_assignment(
540 ctx: mypy.plugin.ClassDefContext, stmt: AssignmentStmt, auto_attribs: bool, class_kw_only: bool
541) -> Iterable[Attribute]:
542 """Return Attribute objects that are created by this assignment.
543
544 The assignments can look like this:
545 x = attr.ib()
546 x = y = attr.ib()
547 x, y = attr.ib(), attr.ib()
548 or if auto_attribs is enabled also like this:
549 x: type
550 x: type = default_value
551 x: type = attr.ib(...)
552 """
553 for lvalue in stmt.lvalues:
554 lvalues, rvalues = _parse_assignments(lvalue, stmt)
555
556 if len(lvalues) != len(rvalues):
557 # This means we have some assignment that isn't 1 to 1.
558 # It can't be an attrib.
559 continue
560
561 for lhs, rvalue in zip(lvalues, rvalues):
562 # Check if the right hand side is a call to an attribute maker.
563 if (
564 isinstance(rvalue, CallExpr)
565 and isinstance(rvalue.callee, RefExpr)
566 and rvalue.callee.fullname in attr_attrib_makers
567 ):
568 attr = _attribute_from_attrib_maker(
569 ctx, auto_attribs, class_kw_only, lhs, rvalue, stmt
570 )
571 if attr:
572 yield attr
573 elif auto_attribs and stmt.type and stmt.new_syntax and not is_class_var(lhs):
574 yield _attribute_from_auto_attrib(ctx, class_kw_only, lhs, rvalue, stmt)
575
576
577def _cleanup_decorator(stmt: Decorator, attr_map: dict[str, Attribute]) -> None:

Callers 1

_analyze_classFunction · 0.85

Calls 7

is_class_varFunction · 0.90
_parse_assignmentsFunction · 0.85
lenFunction · 0.85
zipFunction · 0.85
isinstanceFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…