Skip to content

Commit 4bb7d13

Browse files
authored
Merge pull request #19 from OpenHCSDev/fix/class-source-py313
Find class source without inspect._ClassFinder (0.2.5)
2 parents b56ea76 + 5e0a891 commit 4bb7d13

5 files changed

Lines changed: 36 additions & 17 deletions

File tree

‎.github/workflows/ci.yml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ jobs:
2222
fail-fast: false
2323
matrix:
2424
os: [ubuntu-latest, windows-latest, macos-latest]
25-
python-version: ["3.10", "3.11", "3.12"]
25+
python-version: ["3.10", "3.11", "3.12", "3.13"]
2626

2727
steps:
2828
- name: Checkout code

‎CHANGELOG.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
0.2.2 is the first 0.2.x release published to PyPI. The 0.2.0 and 0.2.1 tags exist in git, but their publish runs were cancelled, so upgrading from PyPI goes straight from 0.1.16 to 0.2.2. The notes below cover every change in that jump.
44

5+
## 0.2.5
6+
7+
Fixed:
8+
- Inline field documentation (a string literal under a dataclass field) is extracted again on Python 3.13 and later. Class source lookup used `inspect._ClassFinder`, a private helper that Python 3.13 removed, so every dataclass came back with no field help. Classes are now located by qualified name from the module AST directly. CI now tests Python 3.13.
9+
510
## 0.2.4
611

712
Fixed:

‎pyproject.toml‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
44

55
[project]
66
name = "python-introspect"
7-
version = "0.2.4"
7+
version = "0.2.5"
88
description = "Pure Python introspection toolkit for function signatures, dataclasses, and type hints"
99
readme = "README.md"
1010
requires-python = ">=3.10"
@@ -21,6 +21,7 @@ classifiers = [
2121
"Programming Language :: Python :: 3.10",
2222
"Programming Language :: Python :: 3.11",
2323
"Programming Language :: Python :: 3.12",
24+
"Programming Language :: Python :: 3.13",
2425
"Topic :: Software Development :: Libraries :: Python Modules",
2526
"Topic :: Utilities",
2627
]

‎src/python_introspect/__init__.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
type resolution for framework-specific types (lazy configs, proxies, etc.)
1010
"""
1111

12-
__version__ = "0.2.4"
12+
__version__ = "0.2.5"
1313

1414
from .signature_analyzer import (
1515
SignatureAnalyzer,

‎src/python_introspect/signature_analyzer.py‎

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -771,6 +771,26 @@ def type_hints(self) -> Dict[str, Any]:
771771
)
772772

773773

774+
def _class_node_for_qualname(tree: ast.AST, qualname: str) -> Optional[ast.ClassDef]:
775+
"""Return the class statement whose qualified name is ``qualname``.
776+
777+
Function bodies contribute ``<locals>`` exactly as ``__qualname__`` does.
778+
"""
779+
pending = [(node, ()) for node in reversed(getattr(tree, 'body', ()))]
780+
while pending:
781+
node, scope = pending.pop()
782+
if isinstance(node, ast.ClassDef):
783+
node_scope = (*scope, node.name)
784+
if '.'.join(node_scope) == qualname:
785+
return node
786+
elif isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef)):
787+
node_scope = (*scope, node.name, '<locals>')
788+
else:
789+
node_scope = scope
790+
pending.extend((child, node_scope) for child in reversed(list(ast.iter_child_nodes(node))))
791+
return None
792+
793+
774794
class ClassSourceDeclaration(NamedTuple):
775795
"""Immutable located class source and its source-only documentation views."""
776796

@@ -1196,20 +1216,13 @@ def _module_source_tree(source: str) -> ast.AST:
11961216
def _qualified_class_source(source: str, qualname: str) -> Optional[ClassSourceDeclaration]:
11971217
"""Use Python's qualified class/decorator locator on current source content."""
11981218
tree = SignatureAnalyzer._module_source_tree(source)
1199-
finder = inspect._ClassFinder(qualname)
1200-
try:
1201-
finder.visit(tree)
1202-
except inspect.ClassFoundException as found:
1203-
lines = source.splitlines(keepends=True)
1204-
first_line = found.args[0] + 1
1205-
block = ''.join(inspect.getblock(lines[found.args[0]:]))
1206-
class_node = next(
1207-
node for node in ast.walk(tree)
1208-
if isinstance(node, ast.ClassDef)
1209-
and min([node.lineno, *(decorator.lineno for decorator in node.decorator_list)]) == first_line
1210-
)
1211-
return ClassSourceDeclaration.from_block(block, class_node, first_line)
1212-
return None
1219+
class_node = _class_node_for_qualname(tree, qualname)
1220+
if class_node is None:
1221+
return None
1222+
first_line = min([class_node.lineno, *(decorator.lineno for decorator in class_node.decorator_list)])
1223+
lines = source.splitlines(keepends=True)
1224+
block = ''.join(inspect.getblock(lines[first_line - 1:]))
1225+
return ClassSourceDeclaration.from_block(block, class_node, first_line)
12131226

12141227
@staticmethod
12151228
def _extract_inline_field_docs(dataclass_type: type) -> Dict[str, str]:

0 commit comments

Comments
 (0)