|
4 | 4 | including workarounds for type checker limitations and utilities for runtime type inspection. |
5 | 5 | """ |
6 | 6 |
|
7 | | -import inspect |
8 | 7 | from dataclasses import dataclass |
9 | 8 | from typing import Any, Generic, cast, get_args, get_origin |
10 | 9 |
|
|
17 | 16 | class TypeExpression(Generic[T]): |
18 | 17 | """A workaround for type checker limitations when using complex type expressions. |
19 | 18 |
|
20 | | - This class serves as a wrapper for types that cannot normally be used in positions |
| 19 | + This class serves as a wrapper for types that cannot normally be used in positions |
21 | 20 | requiring `type[T]`, such as `Any`, `Union[...]`, or `Literal[...]`. It provides a |
22 | | - way to pass these complex type expressions to functions expecting concrete types. |
| 21 | + way to pass these complex type expressions to functions expecting concrete types. |
23 | 22 |
|
24 | | - Example: |
25 | | - Instead of `output_type=Union[str, int]` (which may cause type errors), |
26 | | - use `output_type=TypeExpression[Union[str, int]]`. |
| 23 | + Example: |
| 24 | + Instead of `output_type=Union[str, int]` (which may cause type errors), |
| 25 | + use `output_type=TypeExpression[Union[str, int]]`. |
27 | 26 |
|
28 | | - Note: |
29 | | - This is a workaround for the lack of TypeForm in the Python type system. |
| 27 | + Note: |
| 28 | + This is a workaround for the lack of TypeForm in the Python type system. |
30 | 29 | """ |
31 | 30 |
|
32 | 31 | pass |
@@ -89,44 +88,3 @@ def get_callable_name(callable_: Any) -> str: |
89 | 88 | The callable's __name__ attribute if available, otherwise its string representation. |
90 | 89 | """ |
91 | 90 | return getattr(callable_, '__name__', str(callable_)) |
92 | | - |
93 | | - |
94 | | -def infer_name(obj: Any, *, depth: int) -> str | None: |
95 | | - """Infer the variable name of an object from the calling frame's scope. |
96 | | -
|
97 | | - This function examines the call stack to find what variable name was used |
98 | | - for the given object in the calling scope. This is useful for automatic |
99 | | - naming of objects based on their variable names. |
100 | | -
|
101 | | - Args: |
102 | | - obj: The object whose variable name to infer. |
103 | | - depth: Number of stack frames to traverse upward from the current frame. |
104 | | -
|
105 | | - Returns: |
106 | | - The inferred variable name if found, None otherwise. |
107 | | -
|
108 | | - Example: |
109 | | - Usage should generally look like `infer_name(self, depth=2)` or similar. |
110 | | -
|
111 | | - Note: |
112 | | - TODO(P3): Use this or lose it |
113 | | - """ |
114 | | - target_frame = inspect.currentframe() |
115 | | - if target_frame is None: |
116 | | - return None # pragma: no cover |
117 | | - for _ in range(depth): |
118 | | - target_frame = target_frame.f_back |
119 | | - if target_frame is None: |
120 | | - return None |
121 | | - |
122 | | - for name, item in target_frame.f_locals.items(): |
123 | | - if item is obj: |
124 | | - return name |
125 | | - |
126 | | - if target_frame.f_locals != target_frame.f_globals: # pragma: no branch |
127 | | - # if we couldn't find the agent in locals and globals are a different dict, try globals |
128 | | - for name, item in target_frame.f_globals.items(): |
129 | | - if item is obj: |
130 | | - return name |
131 | | - |
132 | | - return None |
0 commit comments