Hello, I have a simple question but cannot find ea...
# hamilton-help
p
Hello, I have a simple question but cannot find easily the clean way to do it in the doc. How to rename a node to not have the function node. In particular, I'd like to have a space in the node name. Code would be like
Copy code
@rename("My Feature")
def my_feature(...) -> ...:
    return this_feature
And then node is named My Feature in the DAG, and I can further access it with
source("My Feature")
if needed.
s
Check out this example. https://github.com/apache/hamilton/blob/343291bb99b56ab9093783f99940b9750d7788b9/examples/pandas/split-apply-combine/my_functions.py#L114
Copy code
# We use @inject decorator here because we have spaces in the names of columns.
# If column names are valid python variable names we wouldn't need this.
@inject(income=source("Income"), tax_rate=source("Tax Rate"), tax_credit=source("Tax Credit"))
# Step 4: 'Tax Formula' is calculated from 'Income', 'Tax Rate' and 'Tax Credit' series
def tax_formula(income: Series, tax_rate: Series, tax_credit: Series) -> Series:
👍 1
s
@Paul D you'd have to do it via
extract_fields
I think --> so return a dictionary and then pull that key out... I don't see a reason to not add
@rename
-- mind opening an issue / considering contributing it? The only challenge with this is that you're forced to use
@source
downstream because it ceases to be a valid python variable name.
If this issue is retaining spaces, I would consider a transformation on the final output of Hamilton?
t
@Stefan Krawczyk I don't have specific example in mind, but I believe having nodes be valid identifiers is a strong assumptions made in several places in the code. For example, when using
@pipe_output
we namespace things
{entrypoint_node}.with_{step_node}
. Similar things happen in
@subdag
It creates a lot of edge cases if you can do
Copy code
@rename("; DROP TABLE bar")  # SQL injection
def foo(...) -> str: ...

@rename("<b><i>bingo<</b>")  # valid or invalid HTML
def baz(...) -> int: ...
also: • break graphviz notation for visualization • Node names are used as keys in dictionaries,
HamiltonGraph
• node names are passed to SQL engines in the caching backends and the Hamilton UI (would need further sanitization) • could even break exceptions and logging message if it includes
{
A practical solution would be to have lifecycle like: 1. user node is @renamed
"My Node!"
2. pre-graph construction hook sanitizes nodes and maps
My Node!
to
my_node
3. error if we have name collisions (e.g., the user code has both
My Node!
and
My Node
and
my_node
already) 4. User can call
.execute()
using
"My Node!"
which internally maps to
my_node
5.
.execute()
returns a dictionary
{"My Node!": ...}
## Edge cases / questions • what
node_name
do lifecycle hooks see? (e.g., caching, Hamilton UI, MLFlow plugin) • we need strong assumptions and sanitation for anything internal
s
rename could be restrictive
but yeah internally it is a string / string key -- and maybe graphviz would break.
p
I'd be happy to help if possible. I can open an issue where this can be further discussed
👍 1
e
@rename
should be easy
Problem is spaces in node name. I'd recommend keeping them as identifiers cause it makes things easier. Then add
@tag(name="My Exciting Name!")
and do the accounting yourself.