Skip to content

Support for f-strings in Python->Laurel translation + bind variables introduced by exception handlers#595

Open
ssomayyajula wants to merge 21 commits intomainfrom
feat-python-formatted-value
Open

Support for f-strings in Python->Laurel translation + bind variables introduced by exception handlers#595
ssomayyajula wants to merge 21 commits intomainfrom
feat-python-formatted-value

Conversation

@ssomayyajula
Copy link
Contributor

@ssomayyajula ssomayyajula commented Mar 17, 2026

Description of changes:

Handle f-strings in Python->Laurel translation

  • FormattedValue: convert inner expr to string via to_string_any
  • JoinedStr: concatenate all parts using PAdd (same as string +)
  • Interpolation/TemplateStr (Python 3.14+ t-strings): fall through to Hole
  • inferExprType: return Str for JoinedStr and FormattedValue
  • Add f-string test cases to test_strings.py
  • Bind exception variables from except ... as e handlers as LocalVariable declarations in Laurel, so they are in
    scope when referenced in f-strings or other expressions within the handler body.
    Fixed in PR 609
  • test_missing_models expected file updated

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

Translate Python with statements to __enter__/__exit__ instance calls.
For each withitem, a manager variable is declared from the context
expression, __enter__ is called (binding the result if 'as VAR' is
present), the body is translated, then __exit__ is called.

Also fix translateType to recognize user-defined composite types and
return UserDefined instead of falling through to AnyTy. This was a
pre-existing gap that caused type unification errors when composite-typed
variables flowed through translateType.

Add test covering with...as, with (no as), and multiple context managers.
The direct-to-Core path (pyAnalyze) does not support with statements,
so skip this test for the non-laurel SARIF runner.
- FormattedValue: convert inner expr to string via to_string_any
- JoinedStr: concatenate all parts using PAdd (same as string +)
- Interpolation/TemplateStr (Python 3.14+ t-strings): fall through to Hole
- inferExprType: return Str for JoinedStr and FormattedValue
- Add f-string test cases to test_strings.py
@ssomayyajula ssomayyajula requested a review from a team March 17, 2026 19:27
- Revert test_strings.py to original (core-compatible) content
- Add test_fstrings.py with f-string test cases
- Add empty expected_laurel/test_fstrings.expected (to be populated from CI)
- Declare 'as' variable (e.g. 'except Exception as e') as a
  LocalVariable in the handler so it is in scope for Laurel-to-Core
- Translate the exception type annotation for the variable's type
- Update test_missing_models expected output (new call_print_arg
  assertions from f-string translation of print arguments)
@ssomayyajula ssomayyajula changed the title Support for f-strings in Python->Laurel translation Support for f-strings in Python->Laurel translation + bind variables introduced by exception handlers Mar 18, 2026
@aqjune-aws
Copy link
Contributor

Could you resolve the conflict?

F-string parts are always strings, so use well-typed str.concat via
StrConcat operator instead of generic PAdd which generates massive
type-dispatch branches in SMT.
…lify exception handler

FormattedValue now packs composite-typed values (e.g. PythonError) into
from_ClassInstance with their fields as DictStrAny, enabling to_string_any
to accept them. Also removes duplicate LocalVariable declaration in
ExceptHandler since hoisting already handles exception variable binding.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants