Skip to content

Commit 43cdbf3

Browse files
authored
Merge pull request #12 from OpenHCSDev/perf/prepared-declaration-source-20261004
Admit canonical parameter values before deriving dataclass help
2 parents 430fbbe + 2ab8140 commit 43cdbf3

8 files changed

Lines changed: 369 additions & 323 deletions

‎README.md‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,19 @@ changes are checked by the repository's [documentation
5858
workflow](https://github.com/OpenHCSDev/python-introspect/actions/workflows/docs.yml);
5959
the local warnings-as-errors build command is documented in
6060
[`development.rst`](docs/source/development.rst).
61+
62+
### Parameter declarations in 0.2
63+
64+
`UnifiedParameterAnalyzer` now returns the original `ParameterInfo` declarations
65+
from `SignatureAnalyzer`. `UnifiedParameterInfo`, its `source_type` tag, and
66+
`analyze_nested` were removed. Use `analyze` and the declared `param_type` for
67+
parameter topology.
68+
69+
`ParameterInfo` retains its five public fields, constructor and `_replace`
70+
operation, and is now a frozen declaration rather than a tuple. Tuple unpacking
71+
and NamedTuple helpers are no longer supported. Dataclass descriptions are
72+
derived on first help access; value/default overlays preserve that deferred
73+
source, and renaming preserves the original field's help. Callable descriptions
74+
still participate eagerly in inferred parameter types. A presentation extraction
75+
error yields absent help without discarding valid types/defaults. Factory errors
76+
remain uncached and retryable.

‎pyproject.toml‎

Lines changed: 1 addition & 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.1.16"
7+
version = "0.2.0"
88
description = "Pure Python introspection toolkit for function signatures, dataclasses, and type hints"
99
readme = "README.md"
1010
requires-python = ">=3.10"

‎src/python_introspect/__init__.py‎

Lines changed: 1 addition & 3 deletions
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.1.16"
12+
__version__ = "0.2.0"
1313

1414
from .signature_analyzer import (
1515
SignatureAnalyzer,
@@ -24,7 +24,6 @@
2424
)
2525
from .unified_parameter_analyzer import (
2626
UnifiedParameterAnalyzer,
27-
UnifiedParameterInfo,
2827
add_parameter_exclusions,
2928
set_parameter_exclusions,
3029
parameter_exclusions,
@@ -89,7 +88,6 @@
8988
"signature_analysis_target",
9089
# Unified analysis
9190
"UnifiedParameterAnalyzer",
92-
"UnifiedParameterInfo",
9391
"add_parameter_exclusions",
9492
"set_parameter_exclusions",
9593
"parameter_exclusions",

‎src/python_introspect/signature_analyzer.py‎

Lines changed: 243 additions & 116 deletions
Large diffs are not rendered by default.

‎src/python_introspect/unified_parameter_analyzer.py‎

Lines changed: 24 additions & 123 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
import dataclasses
1414
from abc import ABC, abstractmethod
1515
from typing import Dict, Union, Callable, Type, Any, Optional, ClassVar
16-
from dataclasses import dataclass
1716
from weakref import WeakKeyDictionary
1817

1918
from metaclass_registry import AutoRegisterMeta
@@ -59,29 +58,6 @@ def add_parameter_exclusions(
5958
set_parameter_exclusions(target, (*parameter_exclusions(target), *normalized))
6059

6160

62-
@dataclass
63-
class UnifiedParameterInfo:
64-
"""Unified parameter information that works for all parameter sources."""
65-
name: str
66-
param_type: Type
67-
default_value: Any
68-
is_required: bool
69-
description: Optional[str] = None
70-
source_type: str = "unknown" # "function", "dataclass", "nested"
71-
72-
@classmethod
73-
def from_parameter_info(cls, param_info: ParameterInfo, source_type: str = "function") -> "UnifiedParameterInfo":
74-
"""Convert from existing ParameterInfo to unified format."""
75-
return cls(
76-
name=param_info.name,
77-
param_type=param_info.param_type,
78-
default_value=param_info.default_value,
79-
is_required=param_info.is_required,
80-
description=param_info.description,
81-
source_type=source_type
82-
)
83-
84-
8561
class UnifiedParameterTargetAnalyzer(ABC, metaclass=AutoRegisterMeta):
8662
"""Nominal target-kind family for unified parameter analysis."""
8763

@@ -91,7 +67,7 @@ class UnifiedParameterTargetAnalyzer(ABC, metaclass=AutoRegisterMeta):
9167
target_kind: ClassVar[Optional[str]] = None
9268

9369
@classmethod
94-
def analyze_target(cls, target: Union[Callable, Type, object]) -> Dict[str, UnifiedParameterInfo]:
70+
def analyze_target(cls, target: Union[Callable, Type, object]) -> Dict[str, ParameterInfo]:
9571
"""Analyze a target using the first registered target-kind analyzer."""
9672
for analyzer_cls in cls.__registry__.values():
9773
analyzer = analyzer_cls()
@@ -104,7 +80,7 @@ def matches(self, target: Union[Callable, Type, object]) -> bool:
10480
"""Return whether this analyzer owns the target."""
10581

10682
@abstractmethod
107-
def analyze(self, target: Union[Callable, Type, object]) -> Dict[str, UnifiedParameterInfo]:
83+
def analyze(self, target: Union[Callable, Type, object]) -> Dict[str, ParameterInfo]:
10884
"""Analyze the target."""
10985

11086

@@ -116,7 +92,7 @@ class CallableTargetAnalyzer(UnifiedParameterTargetAnalyzer):
11692
def matches(self, target: Union[Callable, Type, object]) -> bool:
11793
return callable(target) and not inspect.isclass(target)
11894

119-
def analyze(self, target: Union[Callable, Type, object]) -> Dict[str, UnifiedParameterInfo]:
95+
def analyze(self, target: Union[Callable, Type, object]) -> Dict[str, ParameterInfo]:
12096
return UnifiedParameterAnalyzer._analyze_callable(target)
12197

12298

@@ -128,7 +104,7 @@ class DataclassTypeTargetAnalyzer(UnifiedParameterTargetAnalyzer):
128104
def matches(self, target: Union[Callable, Type, object]) -> bool:
129105
return inspect.isclass(target) and dataclasses.is_dataclass(target)
130106

131-
def analyze(self, target: Union[Callable, Type, object]) -> Dict[str, UnifiedParameterInfo]:
107+
def analyze(self, target: Union[Callable, Type, object]) -> Dict[str, ParameterInfo]:
132108
return UnifiedParameterAnalyzer._analyze_dataclass_type(target)
133109

134110

@@ -140,7 +116,7 @@ class ClassTargetAnalyzer(UnifiedParameterTargetAnalyzer):
140116
def matches(self, target: Union[Callable, Type, object]) -> bool:
141117
return inspect.isclass(target)
142118

143-
def analyze(self, target: Union[Callable, Type, object]) -> Dict[str, UnifiedParameterInfo]:
119+
def analyze(self, target: Union[Callable, Type, object]) -> Dict[str, ParameterInfo]:
144120
return UnifiedParameterAnalyzer._analyze_callable(target.__init__)
145121

146122

@@ -152,7 +128,7 @@ class DataclassInstanceTargetAnalyzer(UnifiedParameterTargetAnalyzer):
152128
def matches(self, target: Union[Callable, Type, object]) -> bool:
153129
return dataclasses.is_dataclass(target)
154130

155-
def analyze(self, target: Union[Callable, Type, object]) -> Dict[str, UnifiedParameterInfo]:
131+
def analyze(self, target: Union[Callable, Type, object]) -> Dict[str, ParameterInfo]:
156132
return UnifiedParameterAnalyzer._analyze_dataclass_instance(target)
157133

158134

@@ -164,7 +140,7 @@ class ObjectInstanceTargetAnalyzer(UnifiedParameterTargetAnalyzer):
164140
def matches(self, target: Union[Callable, Type, object]) -> bool:
165141
return True
166142

167-
def analyze(self, target: Union[Callable, Type, object]) -> Dict[str, UnifiedParameterInfo]:
143+
def analyze(self, target: Union[Callable, Type, object]) -> Dict[str, ParameterInfo]:
168144
return UnifiedParameterAnalyzer._analyze_object_instance(target)
169145

170146

@@ -177,15 +153,15 @@ class UnifiedParameterAnalyzer:
177153
"""
178154

179155
@staticmethod
180-
def analyze(target: Union[Callable, Type, object], exclude_params: Optional[list] = None) -> Dict[str, UnifiedParameterInfo]:
156+
def analyze(target: Union[Callable, Type, object], exclude_params: Optional[list] = None) -> Dict[str, ParameterInfo]:
181157
"""Analyze parameters from any source.
182158
183159
Args:
184160
target: Function, method, dataclass type, or instance to analyze
185161
exclude_params: Optional list of parameter names to exclude from analysis
186162
187163
Returns:
188-
Dictionary mapping parameter names to UnifiedParameterInfo objects
164+
Dictionary mapping parameter names to ParameterInfo objects
189165
190166
Examples:
191167
# Function analysis
@@ -229,40 +205,17 @@ def _excluded_parameter_names(
229205
return frozenset(names)
230206

231207
@staticmethod
232-
def _analyze_callable(callable_obj: Callable) -> Dict[str, UnifiedParameterInfo]:
208+
def _analyze_callable(callable_obj: Callable) -> Dict[str, ParameterInfo]:
233209
"""Analyze a callable (function, method, etc.)."""
234-
# Use existing SignatureAnalyzer for callables
235-
param_info_dict = SignatureAnalyzer.analyze(callable_obj)
236-
237-
# Convert to unified format
238-
unified_params = {}
239-
for name, param_info in param_info_dict.items():
240-
unified_params[name] = UnifiedParameterInfo.from_parameter_info(
241-
param_info,
242-
source_type="function"
243-
)
244-
245-
return unified_params
246-
210+
return SignatureAnalyzer.analyze(callable_obj)
211+
247212
@staticmethod
248-
def _analyze_dataclass_type(dataclass_type: Type) -> Dict[str, UnifiedParameterInfo]:
249-
"""Analyze a dataclass type using existing SignatureAnalyzer infrastructure."""
250-
# CRITICAL FIX: Use existing SignatureAnalyzer._analyze_dataclass method
251-
# which already handles all the docstring extraction properly
252-
param_info_dict = SignatureAnalyzer._analyze_dataclass(dataclass_type)
253-
254-
# Convert to unified format
255-
unified_params = {}
256-
for name, param_info in param_info_dict.items():
257-
unified_params[name] = UnifiedParameterInfo.from_parameter_info(
258-
param_info,
259-
source_type="dataclass"
260-
)
261-
262-
return unified_params
213+
def _analyze_dataclass_type(dataclass_type: Type) -> Dict[str, ParameterInfo]:
214+
"""Analyze a dataclass through its authoritative field declarations."""
215+
return SignatureAnalyzer._analyze_dataclass(dataclass_type)
263216

264217
@staticmethod
265-
def _analyze_object_instance(instance: object) -> Dict[str, UnifiedParameterInfo]:
218+
def _analyze_object_instance(instance: object) -> Dict[str, ParameterInfo]:
266219
"""Analyze a regular object instance by examining its full inheritance hierarchy.
267220
268221
For dynamic containers like SimpleNamespace (which use **kwargs in __init__),
@@ -295,8 +248,9 @@ def _analyze_object_instance(instance: object) -> Dict[str, UnifiedParameterInfo
295248
class_params = UnifiedParameterAnalyzer._analyze_callable(cls.__init__)
296249

297250
# Remove 'self' parameter
298-
if 'self' in class_params:
299-
del class_params['self']
251+
class_params = {
252+
name: info for name, info in class_params.items() if name != 'self'
253+
}
300254

301255
_logger.debug(f"🔧 _analyze_object_instance: cls={cls.__name__}, class_params after removing self={list(class_params.keys())}")
302256

@@ -312,14 +266,7 @@ def _analyze_object_instance(instance: object) -> Dict[str, UnifiedParameterInfo
312266
# Add parameters that haven't been seen yet (most specific wins)
313267
for param_name, param_info in class_params.items():
314268
if param_name not in all_params and param_name != 'kwargs':
315-
all_params[param_name] = UnifiedParameterInfo(
316-
name=param_name,
317-
param_type=param_info.param_type,
318-
default_value=param_info.default_value,
319-
is_required=param_info.is_required,
320-
description=param_info.description,
321-
source_type="object_instance"
322-
)
269+
all_params[param_name] = param_info
323270

324271
except Exception:
325272
# Skip classes that can't be analyzed - this is legitimate since some classes
@@ -337,67 +284,21 @@ def _analyze_object_instance(instance: object) -> Dict[str, UnifiedParameterInfo
337284
continue
338285
# Infer type from value
339286
attr_type = type(attr_value) if attr_value is not None else type(None)
340-
all_params[attr_name] = UnifiedParameterInfo(
287+
all_params[attr_name] = ParameterInfo(
341288
name=attr_name,
342289
param_type=attr_type,
343290
default_value=attr_value,
344291
is_required=False,
345-
description=None,
346-
source_type="dynamic_attr"
292+
description=None
347293
)
348294
_logger.debug(f"🔧 _analyze_object_instance: after fallback, all_params={list(all_params.keys())}")
349295

350296
return all_params
351297

352298
@staticmethod
353-
def _analyze_dataclass_instance(instance: object) -> Dict[str, UnifiedParameterInfo]:
299+
def _analyze_dataclass_instance(instance: object) -> Dict[str, ParameterInfo]:
354300
"""Analyze a dataclass instance.
355301
356302
Uses current instance values as defaults.
357303
"""
358-
param_info_dict = SignatureAnalyzer.analyze(instance)
359-
return {
360-
name: UnifiedParameterInfo.from_parameter_info(
361-
param_info,
362-
source_type="dataclass_instance",
363-
)
364-
for name, param_info in param_info_dict.items()
365-
}
366-
367-
@staticmethod
368-
def analyze_nested(
369-
target: Union[Callable, Type, object],
370-
parent_info: Dict[str, UnifiedParameterInfo] = None,
371-
) -> Dict[str, UnifiedParameterInfo]:
372-
"""Analyze parameters with nested dataclass support.
373-
374-
This method provides enhanced analysis that can handle nested dataclasses
375-
and maintain parent context information.
376-
377-
Args:
378-
target: The target to analyze
379-
parent_info: Optional parent parameter information for context
380-
381-
Returns:
382-
Dictionary of unified parameter information with nested support
383-
"""
384-
base_params = UnifiedParameterAnalyzer.analyze(target)
385-
386-
# For each parameter, check if it's a nested dataclass
387-
enhanced_params = {}
388-
for name, param_info in base_params.items():
389-
enhanced_params[name] = param_info
390-
391-
# If this parameter is a dataclass, mark it as having nested structure
392-
if dataclasses.is_dataclass(param_info.param_type):
393-
# Update source type to indicate nesting capability
394-
enhanced_params[name] = UnifiedParameterInfo(
395-
name=param_info.name,
396-
param_type=param_info.param_type,
397-
default_value=param_info.default_value,
398-
is_required=param_info.is_required,
399-
description=param_info.description,
400-
source_type=f"{param_info.source_type}_nested"
401-
)
402-
403-
return enhanced_params
304+
return SignatureAnalyzer.analyze(instance)

0 commit comments

Comments
 (0)