Skip to content

Ignore nodes without standalone render in whereis#1

Open
kafkasl wants to merge 1 commit into
mainfrom
whereis
Open

Ignore nodes without standalone render in whereis#1
kafkasl wants to merge 1 commit into
mainfrom
whereis

Conversation

@kafkasl

@kafkasl kafkasl commented Jul 9, 2026

Copy link
Copy Markdown

I got an exception using whereis is non-trivial code example (exception at the bottom).

The problem is that not every LibCST node can be rendered on its own. Some nodes are deliberately incomplete because they rely on their parent to fill in a detail at render time. The clearest example is Annotation:

  • In def f(x: int), the annotation renders as : int
  • In def f() -> int, the annotation renders as -> int

The Annotation node itself doesn't store whether it's a : or a -> — the parent (Param vs FunctionDef) supplies that "indicator" during codegen. Rendered in isolation, there's a MaybeSentinel.DEFAULT where the indicator should be, and LibCST raises CSTCodegenError rather than guess.

The fix is simply to skip the nodes that raise an exception because they can't rendered. Given that whereis is asking "does this node's text equal/contain the fragment?" A node that has no standalone text can never match and returning false is the right answer.

Traceback (most recent call last):

  File ~/ws/.venv/lib/python3.12/site-packages/IPython/core/interactiveshell.py:3748 in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)

  Cell In[50], line 6
    whereis(src, '5b00de99', contains=True)

  File ~/ws/remold/remold/__init__.py:127 in whereis
    return {frag: [p for p,n in sorted(((p,n) for p,n in _walk(mod) if p and _hit(n, frag)), key=lambda o: -len(o[0]))]

  File ~/ws/remold/remold/__init__.py:127 in <genexpr>
    return {frag: [p for p,n in sorted(((p,n) for p,n in _walk(mod) if p and _hit(n, frag)), key=lambda o: -len(o[0]))]

  File ~/ws/remold/remold/__init__.py:123 in _hit
    c = _render(n).strip()

  File ~/ws/.venv/lib/python3.12/site-packages/libcst/_nodes/module.py:136 in code_for_node
    node._codegen(state)

  File ~/ws/.venv/lib/python3.12/site-packages/libcst/_nodes/base.py:300 in _codegen
    self._codegen_impl(state, **kwargs)

  File ~/ws/.venv/lib/python3.12/site-packages/libcst/_nodes/expression.py:1928 in _codegen_impl
    raise CSTCodegenError(

  File <string>
    
CSTCodegenError: Must specify a concrete default_indicator if default used on indicator.

@kafkasl kafkasl changed the title fix: silence CST render errors in whereis() with graceful fallback Ignore nodes without standalone render in whereis Jul 9, 2026
@kafkasl kafkasl self-assigned this Jul 9, 2026
@kafkasl kafkasl added the enhancement New feature or request label Jul 9, 2026
@kafkasl kafkasl requested a review from jph00 July 9, 2026 15:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant