Skip to content

to_html() raises TypeError when a node's source_file (or label) is null #1775

Closed

Description

@happypotato0227-star

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:323 label = 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:156 def sanitize_label(text: str) -> str:
  • security.py:163 text = _CONTROL_CHAR_RE.sub("", text) -> TypeError on None

Impact

  • One node with a null source_file (or label) aborts to_html() entirely; no
    HTML output is produced.
  • Null source_file is 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

  1. safishamsi commented on Jul 10, 2026

    @safishamsi
    Member

    Already fixed in current releases (you're on 0.1.14; current is 0.9.12). On v8 sanitize_label coerces None (if text is None: return "") and the to_html call site guards source_file with str(data.get('source_file') or ''), so a node with source_file=None or label=None renders 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. Please uv tool install --upgrade graphifyy — 0.1.14 predates the fix by a lot. Thanks for the precise root-cause.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions