Yield ``(attr, type_name)`` for every ``self.attr = ...`` in a method body (#2860). ``type_name`` is the annotation (``self.attr: T = ...``) or the bare class of ``self.attr = Ctor(...)``, and None for any other value so the caller can poison the field; ``self.attr = None`` binds nothing
(body, source: bytes, local_types: dict[str, str | None] | None = None)
| 4120 | return _read_text(attr, source) |
| 4121 | |
| 4122 | def _python_self_attr_bindings(body, source: bytes, local_types: dict[str, str | None] | None = None): |
| 4123 | """Yield ``(attr, type_name)`` for every ``self.attr = ...`` in a method body |
| 4124 | (#2860). ``type_name`` is the annotation (``self.attr: T = ...``) or the bare |
| 4125 | class of ``self.attr = Ctor(...)``, and None for any other value so the |
| 4126 | caller can poison the field; ``self.attr = None`` binds nothing. An unchanged |
| 4127 | typed parameter/local also carries its class through ``self.attr = value``.""" |
| 4128 | local_types = local_types or {} |
| 4129 | stack = list(body.named_children) |
| 4130 | while stack: |
| 4131 | node = stack.pop() |
| 4132 | if node.type in ("function_definition", "class_definition", "lambda"): |
| 4133 | continue |
| 4134 | stack.extend(node.named_children) |
| 4135 | left = node.child_by_field_name("left") if node.type == "assignment" else None |
| 4136 | if left is None: |
| 4137 | continue |
| 4138 | value, right = node.child_by_field_name("type"), node.child_by_field_name("right") |
| 4139 | bound_type = None |
| 4140 | if value is None and right is not None: |
| 4141 | if right.type == "none": |
| 4142 | continue |
| 4143 | if right.type == "identifier": |
| 4144 | bound_type = local_types.get(_read_text(right, source)) |
| 4145 | value = right.child_by_field_name("function") if right.type == "call" else None |
| 4146 | name = _read_text(value, source) if value is not None else (bound_type or "") |
| 4147 | typed = name.isidentifier() and name[:1].isupper() |
| 4148 | for target in (left.named_children if left.type == "pattern_list" else [left]): |
| 4149 | attr = _python_self_attr(target, source) |
| 4150 | if attr: |
| 4151 | yield attr, (name if typed and target is left else None) |
| 4152 | |
| 4153 | _JS_SCOPE_BOUNDARY = frozenset({ |
| 4154 | "function_declaration", "function_expression", "function", "arrow_function", |
no test coverage detected