why would this fail? ```@inject( pq_df = sourc...
# hamilton-help
v
why would this fail?
Copy code
@inject(
    pq_df = source("filter_pq_df"),
    filter_maxpages = value("filter_maxpages"),
    filter_maxqueries = value("filter_maxqueries"),
    filter_minqueryweight = value("filter_minqueryweight")
)
def dimension_filters(
    pq_df: pd.DataFrame,
    important_urls_multiline: str,
    exclude_urls_multiline: str,
    exclude_url_regexes: str,
    important_queries_multiline: str,
    filter_maxpages = int,
    filter_maxqueries = int,
    filter_minqueryweight = int
) -> dict:
    ...
I'm getting the error
Copy code
KeyError: 'filter_maxpages'

File /opt/conda/envs/pythondev/lib/python3.11/site-packages/IPython/core/interactiveshell.py:2541, in InteractiveShell.run_cell_magic(self, magic_name, line, cell)
   2539 with self.builtin_trap:
   2540     args = (magic_arg_s, cell)
-> 2541     result = fn(*args, **kwargs)
   2543 # The code below prevents the output from being displayed
   2544 # when using magics with decorator @output_can_be_silenced
   2545 # when the last Python token in the expression is a ';'.
   2546 if getattr(fn, magic.MAGIC_OUTPUT_CAN_BE_SILENCED, False):

File /opt/conda/envs/pythondev/lib/python3.11/site-packages/hamilton/plugins/jupyter_magic.py:287, in HamiltonMagics.cell_to_module(self, line, cell)
    271 """Turn a notebook cell into a Hamilton module definition. This allows you to define
    272 and execute a dataflow from a single cell.
    273 
   (...)
    282
283 """ 284 # shell.ex() is equivalent to exec(), but in the user namespace (i.e. notebook context). 285 # This allows imports and functions defined in the magic cell %%cell_to_module to be 286 # directly accessed from the notebook --> 287 self.shell.ex(cell) 289 args, unknown_args = parse_known_argstring( 290 self.cell_to_module, line 291 ) # specify how to parse by passing method 292 self.resolve_unknown_args_cell_to_module(unknown_args) File /opt/conda/envs/pythondev/lib/python3.11/site-packages/IPython/core/interactiveshell.py:2878, in InteractiveShell.ex(self, cmd) 2876 """Execute a normal python statement in user namespace.""" 2877 with self.builtin_trap: -> 2878 exec(cmd, self.user_global_ns, self.user_ns) File <string>:14 File /opt/conda/envs/pythondev/lib/python3.11/site-packages/hamilton/function_modifiers/base.py:60, in track_decorator_usage.<locals>.replace__call__(self, fn) 58 else: 59 DECORATOR_COUNTER["custom_decorator"] = DECORATOR_COUNTER["custom_decorator"] + 1 ---> 60 return call_fn(self, fn) File /opt/conda/envs/pythondev/lib/python3.11/site-packages/hamilton/function_modifiers/base.py:102, in NodeTransformLifecycle.__call__(self, fn) 94 @track_decorator_usage 95 def __call__(self, fn: Callable): 96 """Calls the decorator by adding attributes using the get_lifecycle_name string. 97 These attributes are the pointer to the decorator object itself, and used later in resolve_nodes below. 98 99 :param fn: Function to decorate 100 return The function again, with the desired properties. 101 """ --> 102 self.validate(fn) 103 lifecycle_name = self.class.get_lifecycle_name() 104 if hasattr(fn, self.get_lifecycle_name()): File /opt/conda/envs/pythondev/lib/python3.11/site-packages/hamilton/function_modifiers/expanders.py:320, in parameterize.validate(self, fn) 318 mapping = mapping[0] 319 for param, replacement_value in mapping.items(): --> 320 param_annotation = type_hints[param] 321 if typing_inspect.is_optional_type(param_annotation): 322 param_annotation = typing_inspect.get_args(param_annotation)[0] KeyError: 'filter_maxpages'```
s
value()
means literal value. So you’re passing a string here. and the type annotations on the function seems off.
Copy code
@inject(
    pq_df = source("filter_pq_df"),
    filter_maxpages = value("filter_maxpages"),   # <--- this is a string value. did you mean source?
    filter_maxqueries = value("filter_maxqueries"),
    filter_minqueryweight = value("filter_minqueryweight")
)
def dimension_filters(
    pq_df: pd.DataFrame,
    important_urls_multiline: str,
    exclude_urls_multiline: str,
    exclude_url_regexes: str,
    important_queries_multiline: str,
    filter_maxpages = int, # <--- type annotations are off here.
    filter_maxqueries = int,
    filter_minqueryweight = int
) -> dict:
    ...
v
true, I had to go back and revisit the definitions, to correct my understanding. The issue still persists. This is the entire cell code:
Copy code
%%cell_to_module rendition_facilities
from hamilton.function_modifiers import inject, value, source
import pandas as pd

from rendition_facing import make_dimension_filters
    
@inject(
    pq_df = source("filter_pq_df"),
    filter_maxpages = source("filter_maxpages"),
    filter_maxqueries = source("filter_maxqueries"),
    filter_minqueryweight = source("filter_minqueryweight")
)
def dimension_filters(
    pq_df: pd.DataFrame,
    important_urls_multiline: str,
    exclude_urls_multiline: str,
    exclude_url_regexes: str,
    important_queries_multiline: str,
    filter_maxpages = int,
    filter_maxqueries = int,
    filter_minqueryweight = int
) -> dict:
    """determine, which queries and pages in the DataFrame fit the criteria"""
    
    from rendition_side import make_dimension_filters
    
    return make_dimension_filters(
        pq_df_source = pq_df,
        maxpages = filter_maxpages, 
        maxqueries = filter_maxqueries, 
        minqueryweight = filter_minqueryweight, 
        includepages = important_urls_multiline,
        excludepages = exclude_urls_multiline, 
        regex_excludepages = exclude_url_regexes, 
        includequeries = important_queries_multiline
    )
s
@Viktor it’s the type annotations. Try:
Copy code
def dimension_filters(
    pq_df: pd.DataFrame,
    important_urls_multiline: str,
    exclude_urls_multiline: str,
    exclude_url_regexes: str,
    important_queries_multiline: str,
    filter_maxpages: int,
    filter_maxqueries: int,
    filter_minqueryweight: int
) -> dict:
we should error better here and say the function doesn’t have correct type annotations
v
oof, thank you. Copy-paste error Sometimes one is blind to one's own doing.
s
all good 🙂