Any way to control input or output types such that...
# hamilton-help
m
Any way to control input or output types such that I can use ABCs such as Mapping? I'd like to not collapse my ChainMap into a dict, but right now the Builder can't link up one node that returns a Mapping (on top of ChainMap) into another node that requests a Mapping. I realize the automatic discovery may be intractable or ambiguous. Any ideas in this direction?
s
Do you have a code example to reproduce this? That would help me understand.
m
Not working:
Copy code
cfg: collections.abc.Mapping[str, str] = [...]
result = driver.execute(
     inputs={
        "study_config": cfg,

[...]
def config(study_config: dict[str, str]) -> Config:
    [...]
Fix: (collapse Mapping into dict]
Copy code
"study_config": dict(cfg),
Basically, types have to match exactly right now. Not a problem with pd.DataFrames for example since the types are internal, but how can we coax connections that would be legal with direct Python calls, such as ABC, Interfaces, Protocols.
e
@Mattias Fornander US so it does something smarter than that, but it’s not perfect. New type-checking cases that we haven’t evaluated. You can override it with this hook https://hamilton.dagworks.io/en/latest/reference/lifecycle-hooks/EdgeConnectionHook/
But if you have any good logic that you’d want to contribute back, we can always make the default more permissive
And do open a ticket with examples of specific things that don’t work!
s
@Mattias Fornander US we just need to modify check_input_type (since it appears to be an issue with validating the object against the type, and not with constructing the graph) and therefore add to the test case here. Actually @Mattias Fornander US I think that is expected behavior, no? Mapping is a superset of dict. Every
dict
is a
Mapping
, but not every
Mapping
is a
dict
. My suggestion here would be to change the annotation on
study_config
...
🙏 1