Repository navigation
Description
Summary
graphify.export.to_html() raises TypeError when any node has a source_file
(or label) attribute whose value is None. Nodes that aren't tied to a single
source file (synthetic / aggregate nodes) legitimately carry source_file=None,
so a single such node makes the entire HTML visualization fail to render.
Version
graphifyy==0.1.14(installed), source @91f4d120b630ee35c79bf3c75ccd186870a808f9- Python 3.12
Reproduction
import networkx as nx
from graphify.export import to_html
G = nx.Graph()
G.add_node("n1", label="Foo", source_file=None) # source_file present but null
to_html(G, communities={0: ["n1"]}, output_path="graphify-out/graph.html")
# TypeError: expected string or bytes-like object, got 'NoneType'
# security.py:163 _CONTROL_CHAR_RE.sub("", text) <- text is None
# via export.py:337 sanitize_label(data.get("source_file", ""))Root cause
The node loop passes attribute values straight into sanitize_label, relying on
dict.get(key, default) to supply a string default:
export.py:323label = sanitize_label(data.get("label", node_id))export.py:337"source_file": sanitize_label(data.get("source_file", ""))
dict.get(key, default) returns default only when the key is absent. When
the key is present with value None (e.g. a node created with source_file=None,
or loaded from JSON where "source_file": null), .get() returns None, not "".
sanitize_label then runs a regex substitution on that value with no guard:
security.py:156def sanitize_label(text: str) -> str:security.py:163text = _CONTROL_CHAR_RE.sub("", text)->TypeErroronNone
Impact
- One node with a null
source_file(orlabel) abortsto_html()entirely; no
HTML output is produced. - Null
source_fileis normal for aggregate/synthetic nodes, so this is easy to hit
on real graphs.
Workaround (currently required)
Coerce None string fields to "" on every node before calling to_html().
Suggested fix (backward-compatible)
Guard at the function boundary so all call sites are protected (labels, source_file,
titles all flow through sanitize_label):
def sanitize_label(text: str | None) -> str:
if text is None:
text = ""
text = _CONTROL_CHAR_RE.sub("", text)
...Alternatively coerce at the call sites with data.get("source_file") or "", but the
function-level guard covers every caller at once.
Activity
test: guard to_html/sanitize_label against null source_file/label (#1775
safishamsi commentedon Jul 10, 2026
Already fixed in current releases (you're on 0.1.14; current is 0.9.12). On v8
sanitize_labelcoerces None (if text is None: return "") and theto_htmlcall site guardssource_filewithstr(data.get('source_file') or ''), so a node withsource_file=Noneorlabel=Nonerenders fine. Verified all three cases (null source_file, null label, both) produce HTML with no TypeError. Added regression tests (unit + to_html integration) to keep it that way. Pleaseuv tool install --upgrade graphifyy— 0.1.14 predates the fix by a lot. Thanks for the precise root-cause.test: guard to_html/sanitize_label against null source_file/label (Gr…