Skip to content

[http-client-python][ICM] Escape quotes and backslashes in StringType.get_declaration #12405

Closed

Description

@g2vinay

Package

@typespec/http-client-python

Describe the bug

In packages/http-client-python/generator/pygen/codegen/models/primitive_types.py (lines 335–336), StringType.get_declaration formats string literal values as:

def get_declaration(self, value) -> str:
    return f"'{value}'" if value == '"' else f'"{value}"'

This only special-cases when the entire string is a single " character. If a string literal in TypeSpec (such as a parameter/property default value, constant, enum value, or discriminator value) contains embedded double quotes ("), backslashes (\), or newlines, the emitted Python string literal in _models.py / _enums.py is unescaped and produces invalid Python syntax or unintended escape sequences.

Similarly, model_type.py (lines 174–175) and model_serializer.py (line 425) interpolate discriminator_value and wire_name directly into quoted strings without escaping.

Expected behavior

String literals emitted into generated Python source files should properly escape embedded backslashes, quotes, and control characters so that any valid TypeSpec string literal produces valid, round-trippable Python source code.

Proposed Fix

Update StringType.get_declaration in primitive_types.py (and string literal formatting in model_type.py / model_serializer.py) to escape backslashes and quotes (e.g., via json.dumps(str(value))) and add unit tests covering string literals with embedded quotes and backslashes.

Activity

  1. added
    bugSomething isn't working
    emitter:client:pythonIssue for the Python client emitter: @typespec/http-client-python
    on Oct 26, 2026
  2. github-actions commented on Oct 26, 2026

    @github-actions
    Contributor

    🎯 Issue Triage
    The Python client emitter does not safely encode TypeSpec string values before embedding them in generated Python source, which can produce invalid syntax or alter backslash/control-character semantics.
    Category: Bug
    Area: emitter:client:python

    ⚠️ No reproduction provided. A minimal repro would help diagnose this issue.

    🔍 Analysis

    The affected paths are Python source generation for literal/default/enum values and model discriminator or wire-name metadata. The apparent cause is direct interpolation into quoted Python literals without a shared escaping routine. Related reports #10784 and #11881 concern escaping in generated docstrings, but they use different rendering paths and are not duplicates of this value-serialization defect.

    📝 Suggested next steps
    • Add a minimal TypeSpec model or operation covering embedded quotes, backslashes, and a control character.
    • Centralize Python string-literal serialization and use it at each identified interpolation site.
    • Add generated-source syntax and round-trip regression tests for defaults, enum values, discriminators, and wire names.

    Generated by Agentic Triage

  3. changed the title [-][http-client-python] Escape quotes and backslashes in StringType.get_declaration[/-] [+][http-client-python][ICM] Escape quotes and backslashes in StringType.get_declaration[/+] on Oct 29, 2026
  4. msyyc commented on Nov 5, 2026

    @msyyc
    Contributor

    fixed in #12342

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

bugSomething isn't workingemitter:client:pythonIssue for the Python client emitter: @typespec/http-client-python

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions