Skip to content

Commit b0c1b8f

Browse files
committed
Do proper lookup when working with a nameclass
1 parent aea1ae2 commit b0c1b8f

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

func_adl/type_based_replacement.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import inspect
66
import logging
77
import sys
8-
from dataclasses import dataclass, make_dataclass
8+
from dataclasses import dataclass, is_dataclass, make_dataclass
99
from typing import (
1010
Any,
1111
Callable,
@@ -971,6 +971,11 @@ def visit_Attribute(self, node: ast.Attribute) -> Any:
971971
raise ValueError(f"Key {key} not found in dict expression!!")
972972
value = t_node.value.values[key_index[0]]
973973
self._found_types[node] = self.lookup_type(value)
974+
elif ((dc := self.lookup_type(t_node.value)) is not None) and is_dataclass(dc):
975+
dc_types = get_type_hints(dc)
976+
if node.attr not in dc_types:
977+
raise ValueError(f"Key {node.attr} not found in dataclass/dictionary {dc}")
978+
self._found_types[node] = dc_types[node.attr]
974979
return t_node
975980

976981
tt = type_transformer(o_stream)

tests/test_type_based_replacement.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -564,6 +564,20 @@ def test_dictionary_through_Select():
564564
assert j_info.annotation == float
565565

566566

567+
def test_dictionary_through_Select_reference():
568+
"""Make sure the Select statement carries the typing all the way through,
569+
including a later reference"""
570+
571+
s = ast_lambda(
572+
"e.Jets().Select(lambda j: {'pt': j.pt(), 'eta': j.eta()}).Select(lambda info: info.pt)"
573+
)
574+
objs = ObjectStream[Event](ast.Name(id="e", ctx=ast.Load()))
575+
576+
_, _, expr_type = remap_by_types(objs, "e", Event, s)
577+
578+
assert expr_type == Iterable[float]
579+
580+
567581
def test_indexed_tuple():
568582
"Check that we can type-follow through tuples"
569583

0 commit comments

Comments
 (0)