Map ``name -> ClassName`` for the parameters and locals of one Python function. Typed from a class annotation on a parameter or local (``request: Request``, ``x: Client = ...``), a constructor call (``client = Client(...)``) or a context manager (``with Client(...) as client``). Same 10
(
body_node, source: bytes, *, factory_bindings: dict[str, list[int]] | None = None,
)
| 8510 | return None |
| 8511 | |
| 8512 | def _python_local_class_bindings( |
| 8513 | body_node, source: bytes, *, factory_bindings: dict[str, list[int]] | None = None, |
| 8514 | ) -> dict[str, str | None]: |
| 8515 | """Map ``name -> ClassName`` for the parameters and locals of one Python function. |
| 8516 | |
| 8517 | Typed from a class annotation on a parameter or local (``request: Request``, |
| 8518 | ``x: Client = ...``), a constructor call (``client = Client(...)``) or a |
| 8519 | context manager (``with Client(...) as client``). Same 100%-confidence |
| 8520 | contract as the Ruby table: the table is flow-insensitive, so a name bound |
| 8521 | to anything that is not one class -- an untyped parameter, a factory call, a |
| 8522 | loop target, a second class, tuple unpacking, a lambda parameter, a ``match`` |
| 8523 | capture, an import -- maps to ``None`` for the whole function and is never |
| 8524 | resolved. Nested functions are their own caller and are not entered. Calls |
| 8525 | inside a lambda or a nested class body are attributed to this function, so |
| 8526 | every name those bind is poisoned rather than typed. |
| 8527 | """ |
| 8528 | # A member-call result is not a known class in this file. Keep only its exact |
| 8529 | # AST call span for the corpus resolver; the same poison rules apply to it. |
| 8530 | bindings: dict[str, str | tuple[int, int] | None] = {} |
| 8531 | poison_only = False |
| 8532 | |
| 8533 | def bind(name: str, cls: str | tuple[int, int] | None) -> None: |
| 8534 | if poison_only or cls is None or bindings.get(name, cls) != cls: |
| 8535 | bindings[name] = None |
| 8536 | elif name not in bindings: |
| 8537 | bindings[name] = cls |
| 8538 | |
| 8539 | def poison(target) -> None: |
| 8540 | names: set[str] = set() |
| 8541 | _python_collect_assignment_targets(target, source, names) |
| 8542 | for name in names: |
| 8543 | bindings[name] = None |
| 8544 | |
| 8545 | def poison_identifiers(node) -> None: |
| 8546 | stack = [node] |
| 8547 | while stack: |
| 8548 | cur = stack.pop() |
| 8549 | if cur.type == "identifier": |
| 8550 | bindings[_read_text(cur, source)] = None |
| 8551 | stack.extend(cur.children) |
| 8552 | |
| 8553 | func = body_node.parent |
| 8554 | params = func.child_by_field_name("parameters") if func is not None and func.type == "function_definition" else None |
| 8555 | for name in _python_param_names(params, source): |
| 8556 | bindings[name] = None # an untyped parameter: unknown, and a later assignment must not type it |
| 8557 | if params is not None: |
| 8558 | for child in params.children: |
| 8559 | if child.type in ("typed_parameter", "typed_default_parameter"): |
| 8560 | name_n = child.child_by_field_name("name") or next( |
| 8561 | (c for c in child.children if c.type == "identifier"), None |
| 8562 | ) |
| 8563 | if name_n is not None: |
| 8564 | name = _read_text(name_n, source) |
| 8565 | bindings.pop(name, None) |
| 8566 | bind(name, _python_class_ref(child.child_by_field_name("type"), source)) |
| 8567 | |
| 8568 | def visit(n) -> None: |
| 8569 | nonlocal poison_only |
no test coverage detected