Repository navigation
Description
Labels: bug, node-identity, unicode
Version: 0.9.38 (commit 10ad921)
Summary
graphify/ids.py exists specifically to stop node-ID drift — its module docstring names
#811, #550, #1033 and #1104 as the bug class it was created to prevent, and
normalize_id()'s own docstring states:
Idempotent:
normalize_id(normalize_id(s)) == normalize_id(s).
That invariant does not hold for any identifier containing U+0130 LATIN CAPITAL LETTER I
WITH DOT ABOVE (İ) — an extremely common character in Turkish source code
(İslemYap, İçerik, İstanbul).
Steps to reproduce
from graphify.ids import normalize_id
for s in ["İ", "İstanbul", "fileİname", "naİve_Handler"]:
a = normalize_id(s)
b = normalize_id(a)
print("OK " if a == b else "FAIL", ascii(s), "->", ascii(a), "->", ascii(b))FAIL 'İ' -> 'i̇' -> 'i'
FAIL 'İstanbul' -> 'i̇stanbul' -> 'i_stanbul'
FAIL 'fileİname' -> 'filei̇name' -> 'filei_name'
FAIL 'naİve_Handler' -> 'nai̇ve_handler' -> 'nai_ve_handler'
Actual behavior
normalize_id()is not idempotent: a second application changes the result.- The output of the first application contains U+0307 COMBINING DOT ABOVE, which is
categoryMnand is not matched by\w. Sonormalize_id()violates its own
postcondition that the result contains only word characters and underscores.
Expected behavior
normalize_id(normalize_id(s)) == normalize_id(s) for every input, and the result contains
only \w characters and _.
Root cause
graphify/ids.py:32-40:
def normalize_id(s: str) -> str:
s = unicodedata.normalize("NFKC", s)
s = re.sub(r"[^\w]+", "_", s, flags=re.UNICODE)
s = re.sub(r"_+", "_", s)
return s.strip("_").casefold() # <-- casefold runs AFTER the \w filtercasefold() is the last step, but it can introduce non-\w characters:
"İ".casefold() is the two-character sequence "i̇". Because the [^\w]+
substitution already ran, that combining mark is never filtered — it survives into the
returned ID. On a second pass it is filtered, to _, producing a different string.
Impact
build.py re-normalizes IDs that make_id() already produced, at several points:
build.py:1008—norm_to_id = {_normalize_id(nid): nid for nid in node_set}build.py:1102-1104— edge endpoints resolved through_normalize_id(src)build.py:1057-1058—if _normalize_id(nid).startswith(new_stem)wherenew_stemcomes
frommake_id()and is not re-normalized
That last one is the concrete drift: for a Turkish file/symbol, new_stem still carries the
raw i̇ form while _normalize_id(nid) has collapsed it to i_, so the startswith
test fails and the re-key silently does not happen — the exact ghost-node outcome
ids.py was introduced to prevent.
It also creates a collision channel: a symbol genuinely named i_slemYap and a symbol named
İslemYap both normalize to islem_i_slemyap, so norm_to_id can rewrite an edge endpoint
onto the wrong node.
Suggested fix
Casefold before the character filter, and re-run NFKC afterwards so the whole recipe is a
fixed point:
def normalize_id(s: str) -> str:
s = unicodedata.normalize("NFKC", s)
s = s.casefold() # may expand: İ -> i + U+0307
s = unicodedata.normalize("NFKC", s) # recompose what casefold produced
s = re.sub(r"[^\w]+", "_", s, flags=re.UNICODE) # now nothing non-\w can survive
s = re.sub(r"_+", "_", s)
return s.strip("_")Worth adding as a property test, since this class keeps recurring:
@given(st.text())
def test_normalize_id_is_idempotent(s):
assert normalize_id(normalize_id(s)) == normalize_id(s)Note that any fix changes IDs for affected corpora, so it needs the same legacy-form
migration treatment as #1504 / #1917.
Related
- Chinese/Unicode identifiers cause silent node deduplication — only 8 of 202 functions captured #811 (Chinese/Unicode identifiers silently deduplicated) — same bug class, different
character range. - Path-based IDs still collide after #1504 fix:
normalize_idcollapses non-word sequences to_#1522 (normalize_idcollapses non-word sequences to_) — adjacent, but about
collisions rather than idempotency.
Activity
chore: bump to 0.9.40
safishamsi commentedon Aug 11, 2026
Fixed in v0.9.40 (
graphifyy==0.9.40on PyPI).normalize_id()now casefolds before the non-word filter, so it is idempotent for U+0130 (Turkishİ) and similar codepoints. Verified no ASCII identifier id changes. Thanks @rajarshidattapy (PR #2616).fix(ids): make normalize_id caseless-stable via a casefold+NFKC fixpo…
fix(csharp): emit member nodes for properties only, not fields