Conversation
cc66ec4 to
27614a7
Compare
src/instana/util/span_utils.py
Outdated
| return True | ||
|
|
||
|
|
||
| def resolve_nested_key(data: Dict[str, Any], key_parts: List[str]) -> Optional[Any]: |
There was a problem hiding this comment.
Starting with Python 3.9 (our current lower bound supported runtime version), the usage of the built-in collection types like list, dict, set, and tuple can be used directly as generic types in annotations, eliminating the need to import capitalized aliases from the typing module (line 4).
I suggest you take the opportunity and update this code to use the built-in collections.
| def resolve_nested_key(data: Dict[str, Any], key_parts: List[str]) -> Optional[Any]: | |
| def resolve_nested_key(data: dict[str, Any], key_parts: list[str]) -> Optional[Any]: |
There was a problem hiding this comment.
Done! (only for the parts I touched)
27614a7 to
2ee3887
Compare
Signed-off-by: Cagri Yonca <cagri@ibm.com>
2ee3887 to
1924c28
Compare
|
| return True | ||
|
|
||
|
|
||
| def resolve_nested_key(data: dict[str, Any], key_parts: list[str]) -> Optional[Any]: |
There was a problem hiding this comment.
Optional[Any] is redundant — Any already includes None. The return type is simply Any. The import of Optional can be dropped entirely.
| def resolve_nested_key(data: dict[str, Any], key_parts: list[str]) -> Optional[Any]: | |
| def resolve_nested_key(data: dict[str, Any], key_parts: list[str]) -> Any: |
| remaining = key_parts[i:] | ||
| if not remaining: | ||
| return data[candidate] | ||
| result = resolve_nested_key(data[candidate], remaining) |
There was a problem hiding this comment.
I suggest that you refactor this function to use a while loop instead of a recursive function - a while loop is usually faster with constant memory usage than recursion in Python because Python is optimized for iteration, not recursion.
Every recursive call in Python pushes a new frame onto the call stack — local variables, argument bindings, return address, etc. For a deeply nested structure (say, 50 key parts), that's 50 frames allocated and deallocated. The iterative version reuses a single frame throughout. This is relatively costly in Python.



No description provided.