Skip to content

Add test_annotationlib to v3.14.2 and fix related bugs#7001

Merged
youknowone merged 2 commits intoRustPython:mainfrom
youknowone:annotationlib
Feb 5, 2026
Merged

Add test_annotationlib to v3.14.2 and fix related bugs#7001
youknowone merged 2 commits intoRustPython:mainfrom
youknowone:annotationlib

Conversation

@youknowone
Copy link
Member

@youknowone youknowone commented Feb 5, 2026

Summary by CodeRabbit

  • Bug Fixes
    • Preserve whitespace after opening brace for interpolations so formatted strings keep intended spacing.
    • More reliable extraction of interpolated expression source, improving accuracy for f-strings and similar templates.
    • Graceful fallback when source ranges are imprecise; no changes to public APIs or behavior outside string formatting.

@github-actions
Copy link
Contributor

github-actions bot commented Feb 5, 2026

Code has been automatically formatted

The code in this PR has been formatted using:

  • cargo fmt --all
    Please pull the latest changes before pushing again:
git pull origin annotationlib

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Feb 5, 2026

📝 Walkthrough

Walkthrough

Adjusts interpolation source extraction to skip the opening brace using a TextSize-based offset (with fallback for dummy ranges), and preserves a leading whitespace after "{" when unparsing formatted expressions.

Changes

Cohort / File(s) Summary
Interpolation Source Extraction
crates/codegen/src/compile.rs
Added TextSize import. When extracting interpolation expression source, compute after_brace = interp.range.start() + TextSize::new(1) and slice from after_brace to preserve text inside braces; fall back to interp.expression.range() for dummy or invalid ranges.
Whitespace Preservation in Formatting
crates/codegen/src/unparse.rs
In unparse_formatted, detect ASCII whitespace immediately before the expression start and emit "{ " (with a space) instead of "{" when the buffered content doesn't already begin with {, preserving leading whitespace between { and the expression.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

🐰 I nudge the brace and peek inside,
A tiny space I carefully hide,
Interpolations keep their gentle grace,
Ranges nudged to find the perfect place,
Hooray — the f-strings wear a softer face.

🚥 Pre-merge checks | ✅ 1 | ❌ 2
❌ Failed checks (2 warnings)
Check name Status Explanation Resolution
Title check ⚠️ Warning The PR title 'Add test_annotationlib to v3.14.2 and fix related bugs' does not match the actual changes shown in the raw_summary, which focus on modifications to interpolation source extraction in f-strings and whitespace handling in unparsing within Rust code generation and unparsing modules. Update the PR title to accurately reflect the actual changes, such as 'Fix f-string interpolation source extraction and whitespace handling in codegen' or similar, to match the implementation changes in compile.rs and unparse.rs.
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (1 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Warning

Tools execution failed with the following error:

Failed to run tools: 13 INTERNAL: Received RST_STREAM with code 2 (Internal server error)


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@youknowone youknowone marked this pull request as ready for review February 5, 2026 12:44
@youknowone youknowone changed the title Update annotationlib to v3.14.2 and fix related bugs Add test_annotationlib to v3.14.2 and fix related bugs Feb 5, 2026
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Fix all issues with AI agents
In `@crates/codegen/src/compile.rs`:
- Around line 8369-8379: The current logic uses after_brace =
interp.range.start() + TextSize::new(1) without verifying interp.range is valid,
which causes slices to drop the first character when interp.range is a dummy
(0..0); fix by first checking that interp.range actually encloses the expression
(e.g., interp.range.start() < interp.expression.range().start() and
interp.range.end() >= interp.expression.range().end()) before computing
after_brace and using self.source_file.slice(TextRange::new(after_brace,
expr_end)); if that containment check fails, fall back to slicing
interp.expression.range() as you already do in the else branch, ensuring
expr_source is only sliced with after_brace when the interpolation range is real
and encloses the expression.
🧹 Nitpick comments (1)
crates/codegen/src/unparse.rs (1)

564-576: Safe bounds checking, but consider defensive handling for edge cases.

The bounds checking with start > 0 and .get(start - 1) is correct and prevents out-of-bounds access. The logic correctly identifies leading whitespace between { and the expression.

However, this assumes val.range() always represents a valid position in the source text. If the AST was constructed with synthetic/dummy ranges (as mentioned in compile.rs context), the check at start - 1 could inspect unrelated source text.

Consider adding a sanity check that verifies the position is within the f-string context, or at minimum that the byte at start - 1 is either { or whitespace:

🔧 Suggested defensive check
             // Preserve leading whitespace between '{' and the expression
             let source_text = self.source.source_text();
             let start = val.range().start().to_usize();
             if start > 0
-                && source_text
+                && start <= source_text.len()
+                && source_text
                     .as_bytes()
                     .get(start - 1)
                     .is_some_and(|b| b.is_ascii_whitespace())

@github-actions
Copy link
Contributor

github-actions bot commented Feb 5, 2026

📦 Library Dependencies

The following Lib/ modules were modified. Here are their dependencies:

[x] lib: cpython/Lib/annotationlib.py
[x] test: cpython/Lib/test/test_annotationlib.py

dependencies:

  • annotationlib

dependent tests: (43 tests)

  • annotationlib: test_annotationlib test_functools test_inspect test_reprlib test_typing
    • inspect: test_abc test_argparse test_asyncgen test_builtin test_code test_collections test_coroutines test_decimal test_enum test_generators test_grammar test_ntpath test_operator test_patma test_posixpath test_signal test_traceback test_types test_unittest test_yield_from test_zipimport
      • asyncio: test_asyncio test_contextlib_async test_logging test_os test_sys_settrace test_unittest
      • bdb: test_bdb
      • dataclasses: test__colorize test_genericalias test_pprint test_regrtest test_zoneinfo
      • importlib.metadata: test_importlib
      • rlcompleter: test_rlcompleter
      • trace: test_trace
      • xmlrpc.server: test_docxmlrpc test_xmlrpc

[ ] test: cpython/Lib/test/test_list.py (TODO: 5)
[ ] test: cpython/Lib/test/test_listcomps.py (TODO: 1)
[ ] test: cpython/Lib/test/test_userlist.py

dependencies:

dependent tests: (no tests depend on list)

[x] lib: cpython/Lib/warnings.py
[x] lib: cpython/Lib/_py_warnings.py
[ ] test: cpython/Lib/test/test_warnings (TODO: 40)

dependencies:

  • warnings

dependent tests: (48 tests)

  • warnings: test_argparse test_asyncio test_buffer test_builtin test_codecs test_codeop test_coroutines test_ctypes test_decimal test_descr test_fnmatch test_fstring test_genericpath test_glob test_global test_grammar test_gzip test_hashlib test_hmac test_importlib test_inspect test_io test_itertools test_logging test_ntpath test_os test_pickle test_posix test_pty test_pyclbr test_random test_re test_runpy test_set test_socket test_sqlite3 test_str test_string_literals test_support test_sys test_tempfile test_threading test_typing test_unicode_file_functions test_unittest test_unparse test_xml_etree test_zipimport

Legend:

  • [+] path exists in CPython
  • [x] up-to-date, [ ] outdated

@youknowone youknowone merged commit 6de6a92 into RustPython:main Feb 5, 2026
13 of 14 checks passed
@youknowone youknowone deleted the annotationlib branch February 5, 2026 14:39
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.

1 participant