The single class a Python annotation or constructor callee names, else None. ``Request``, ``httpx.Request``, ``"Request"``, ``Optional[Request]`` and ``Request | None`` all name ``Request``. A container (``list[Request]``), a union of two classes, or a lowercase callee (``make_client``,
(node, source: bytes)
| 8463 | return _read_text(recv, source) |
| 8464 | |
| 8465 | def _python_class_ref(node, source: bytes) -> str | None: |
| 8466 | """The single class a Python annotation or constructor callee names, else None. |
| 8467 | |
| 8468 | ``Request``, ``httpx.Request``, ``"Request"``, ``Optional[Request]`` and |
| 8469 | ``Request | None`` all name ``Request``. A container (``list[Request]``), a |
| 8470 | union of two classes, or a lowercase callee (``make_client``, |
| 8471 | ``Client.from_url``) names no single class, so it stays untyped. |
| 8472 | """ |
| 8473 | if node is None: |
| 8474 | return None |
| 8475 | kind = node.type |
| 8476 | if kind == "type": |
| 8477 | named = [c for c in node.children if c.is_named] |
| 8478 | return _python_class_ref(named[0], source) if len(named) == 1 else None |
| 8479 | if kind == "identifier": |
| 8480 | name = _read_text(node, source) |
| 8481 | return name if name[:1].isupper() else None |
| 8482 | if kind == "attribute": |
| 8483 | return _python_class_ref(node.child_by_field_name("attribute"), source) |
| 8484 | if kind == "string": |
| 8485 | name = _read_text(node, source).strip("\"'").split(".")[-1].strip() |
| 8486 | return name if name.isidentifier() and name[:1].isupper() else None |
| 8487 | if kind == "subscript": # typing.Optional[X] |
| 8488 | value = node.child_by_field_name("value") |
| 8489 | subs = node.children_by_field_name("subscript") |
| 8490 | if value is not None and _read_text(value, source).split(".")[-1] == "Optional" and len(subs) == 1: |
| 8491 | return _python_class_ref(subs[0], source) |
| 8492 | return None |
| 8493 | if kind == "generic_type": # Optional[X] |
| 8494 | named = [c for c in node.children if c.is_named] |
| 8495 | if ( |
| 8496 | len(named) == 2 |
| 8497 | and named[0].type == "identifier" |
| 8498 | and _read_text(named[0], source) == "Optional" |
| 8499 | and named[1].type == "type_parameter" |
| 8500 | ): |
| 8501 | inner = [c for c in named[1].children if c.is_named] |
| 8502 | return _python_class_ref(inner[0], source) if len(inner) == 1 else None |
| 8503 | return None |
| 8504 | if kind == "binary_operator": |
| 8505 | left, right = node.child_by_field_name("left"), node.child_by_field_name("right") |
| 8506 | if right is not None and right.type == "none": |
| 8507 | return _python_class_ref(left, source) |
| 8508 | if left is not None and left.type == "none": |
| 8509 | return _python_class_ref(right, source) |
| 8510 | return None |
| 8511 | |
| 8512 | def _python_local_class_bindings( |
| 8513 | body_node, source: bytes, *, factory_bindings: dict[str, list[int]] | None = None, |
no test coverage detected