Skip to content

ids.normalize_id() is not idempotent for U+0130 (Turkish İ), breaking its documented invariant and emitting non-\w characters into node IDs #2614

Closed

Description

@rajarshidattapy

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
    category Mn and is not matched by \w. So normalize_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 filter

casefold() 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) where new_stem comes
    from make_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

Activity

  1. added a commit that references this issue on Aug 11, 2026
  2. safishamsi commented on Aug 11, 2026

    @safishamsi
    Member

    Fixed in v0.9.40 (graphifyy==0.9.40 on 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).

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