Skip to content

More pyi_generator tweaks#6319

Merged
masenf merged 6 commits intomainfrom
masenf/typing-callable
Apr 10, 2026
Merged

More pyi_generator tweaks#6319
masenf merged 6 commits intomainfrom
masenf/typing-callable

Conversation

@masenf
Copy link
Copy Markdown
Collaborator

@masenf masenf commented Apr 10, 2026

  • use collections.abc instead of typing
  • replace typing imports with collections.abc where relevant (generated stubs standardize on collections.abc)
  • parse post-prop docstrings

masenf added 5 commits April 10, 2026 11:27
Where possible, use new collections.abc imports instead of importing from typing.

If the same-named typing imports are present, they will be removed.
Include plain names that are present in the DEFAULT_IMPORTS
This allows old names to be automatically translated to new names in the stub
files.

For example, this allows the actual module to write `typing.Callable` and have
it turn into `collections.abc.Callable`
@codspeed-hq
Copy link
Copy Markdown

codspeed-hq bot commented Apr 10, 2026

Merging this PR will not alter performance

✅ 9 untouched benchmarks


Comparing masenf/typing-callable (d69e528) with main (194711a)

Open in CodSpeed

adhami3310
adhami3310 previously approved these changes Apr 10, 2026
@greptile-apps
Copy link
Copy Markdown
Contributor

greptile-apps bot commented Apr 10, 2026

Greptile Summary

This PR migrates Callable, Mapping, and Sequence from typing to collections.abc in generated stubs, and adds support for parsing post-prop triple-quoted docstrings in _get_class_prop_comments. The _get_visible_type_name function is also updated to recognize types from DEFAULT_IMPORTS/EXCLUDED_IMPORTS by module, removing the need for them to be present in type_hint_globals.

  • The new docstring-end detection uses stripped.partition('\"\"\"')[0] or stripped.partition(\"'''\")[0], which incorrectly appends literal triple-quote characters to the docstring when the closing \"\"\" appears alone on a line.
  • The def function-boundary check runs before the in_docstring guard, so a prop docstring containing def in its prose prematurely stops parsing and silently drops subsequent props' comments.

Confidence Score: 4/5

Safe to merge after fixing the closing triple-quote or-expression bug; the loop-ordering issue is minor but worth addressing.

One P1 bug (incorrect end_text when closing triple-quote is on its own line) and one P2 ordering issue (def-boundary check before in_docstring guard) in the new docstring parser. The existing tests don't cover the closing-quote-on-its-own-line case, so the bug would be silently shipped. All other changes look correct.

packages/reflex-base/src/reflex_base/utils/pyi_generator.py — specifically the in_docstring end-detection block (lines 329-342) and the loop guard ordering (lines 320-326).

Important Files Changed

Filename Overview
packages/reflex-base/src/reflex_base/utils/pyi_generator.py Core stub generator: adds collections.abc imports, post-prop docstring parsing, and fixes _get_visible_type_name — two logic bugs in the docstring end-detection and loop-guard ordering need fixing.
tests/units/reflex_base/utils/pyi_generator/dataset/simple_component.py Adds inline triple-quoted docstring test cases (single-line and multi-line) to the dataset for the new post-prop docstring feature.
tests/units/reflex_base/utils/pyi_generator/dataset/var_types.py Updates import to use collections.abc.Callable, matching the new default import convention for generated stubs.
tests/units/reflex_base/utils/pyi_generator/golden/simple_component.pyi Updated golden file reflecting post-prop docstring extraction in the generated create() docstring; Mapping/Sequence now imported from collections.abc.
tests/units/reflex_base/utils/pyi_generator/golden/var_types.pyi Golden stub updated to import Callable, Mapping, Sequence from collections.abc instead of typing.
pyi_hashes.json Regenerated hashes for all affected .pyi stubs following the collections.abc import migration.

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart TD
    A[Next source line] --> B{in_docstring?}
    B -- Yes --> C{closing triple-quote found?}
    C -- Yes --> D[Extract end_text via partition OR-chain]
    D --> E[Save to props_comments]
    E --> F[Reset in_docstring and last_prop]
    C -- No --> G[Append line to docstring_lines]
    B -- No --> H{re.search def in line?}
    H -- Yes --> I[Break loop]
    H -- No --> J{empty line?}
    J -- Yes --> K[Clear comments and last_prop]
    J -- No --> L{line starts with hash?}
    L -- Yes --> M[Append to comments]
    L -- No --> N{regex matches word-colon?}
    N -- No --> O[Clear last_prop]
    N -- Yes --> P[Extract prop name]
    P --> Q[Save comments to props_comments]
    Q --> R[Set last_prop equals prop]
    R --> S{last_prop set and line starts with triple-quote?}
    S -- Yes --> T{closing quote on same line?}
    T -- Yes --> U[Single-line docstring stored]
    T -- No --> V[Set in_docstring = True]

    style D fill:#f99,stroke:#c00
    style H fill:#f99,stroke:#c00
Loading

Reviews (1): Last reviewed commit: "pyi_generator: Recognize post-prop docst..." | Re-trigger Greptile

Comment on lines +330 to +337
if '"""' in stripped or "'''" in stripped:
# End of multi-line docstring.
end_text = stripped.partition('"""')[0] or stripped.partition("'''")[0]
if end_text:
docstring_lines.append(end_text.strip())
if last_prop and docstring_lines:
props_comments[last_prop] = tuple(docstring_lines)
in_docstring = False
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Triple-quote on its own line incorrectly appends quotes to docstring content

When a multi-line docstring ends with the closing triple-quote alone on a line (e.g. """), stripped.partition('"""')[0] returns "" (empty, falsy), so the or falls through to stripped.partition("'''")[0], which returns '"""' (the whole string, since ''' is absent). That truthy value gets appended to docstring_lines, poisoning the stored comment with literal triple-quote characters.

# stripped = '"""' (closing quote on its own line)
stripped.partition('"""')[0]   # → '' (falsy)
stripped.partition("'''")[0]   # → '"""' (truthy) ← appended to docstring_lines by mistake

Fix by selecting the partition based on which quote type is actually present:

Suggested change
if '"""' in stripped or "'''" in stripped:
# End of multi-line docstring.
end_text = stripped.partition('"""')[0] or stripped.partition("'''")[0]
if end_text:
docstring_lines.append(end_text.strip())
if last_prop and docstring_lines:
props_comments[last_prop] = tuple(docstring_lines)
in_docstring = False
if in_docstring:
if '"""' in stripped or "'''" in stripped:
# End of multi-line docstring.
if '"""' in stripped:
end_text = stripped.partition('"""')[0].strip()
else:
end_text = stripped.partition("'''")[0].strip()
if end_text:
docstring_lines.append(end_text)
if last_prop and docstring_lines:
props_comments[last_prop] = tuple(docstring_lines)
in_docstring = False
docstring_lines = []
last_prop = ""

Comment on lines 320 to +326
for line in _get_source(clz).splitlines():
reached_functions = re.search(r"def ", line)
if reached_functions:
# We've reached the functions, so stop.
break

stripped = line.strip()
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 reached_functions check fires inside an open docstring

The def search runs on every line before the in_docstring guard is consulted. A prop docstring that mentions def in its prose (e.g., "Defines the callback spec.") will prematurely break the loop, silently dropping all subsequent prop comments. The in_docstring block should be evaluated first so that docstring content is never mistaken for a function boundary.

Suggested change
for line in _get_source(clz).splitlines():
reached_functions = re.search(r"def ", line)
if reached_functions:
# We've reached the functions, so stop.
break
stripped = line.strip()
for line in _get_source(clz).splitlines():
stripped = line.strip()
# Handle triple-quoted docstrings after prop definitions.
if in_docstring:
if '"""' in stripped or "'''" in stripped:
# End of multi-line docstring.
end_text = stripped.partition('"""')[0] or stripped.partition("'''")[0]
if end_text:
docstring_lines.append(end_text.strip())
if last_prop and docstring_lines:
props_comments[last_prop] = tuple(docstring_lines)
in_docstring = False
docstring_lines = []
last_prop = ""
else:
docstring_lines.append(stripped)
continue
reached_functions = re.search(r"def ", line)
if reached_functions:
# We've reached the functions, so stop.
break

@masenf masenf merged commit ecccd05 into main Apr 10, 2026
40 checks passed
@masenf masenf deleted the masenf/typing-callable branch April 10, 2026 19:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants