Skip to content

Python emitter: generated docstrings can contain invalid Python escape sequences (e.g. \W) causing SyntaxWarning on 3.12+ #10784

@msyyc

Description

@msyyc

Bug

The TypeSpec Python emitter (@azure-tools/typespec-python / @typespec/http-client-python) writes model/property doc strings verbatim into the generated Python source as the body of a docstring. When the original @doc(...) contains a backslash followed by a character that is not a recognized Python string escape (e.g. \W, \d, \s, etc.), the resulting Python source triggers:

SyntaxWarning: invalid escape sequence ''\W''

on Python 3.12+ (this will become a SyntaxError in a future Python version).

Repro

TypeSpec source (excerpt) — specification/compute/resource-manager/Microsoft.Compute/Compute/Compute/models.tsp in Azure/azure-rest-api-specs:

model OSProfile {
  @doc("Specifies the password ... Has a special character (Regex match [\\W_]) ...")
  @secret
  adminPassword?: string;
}

The TypeSpec string value here is the literal ... (Regex match [\W_]) ... (one backslash + W), which is correct for the swagger/REST description.

The generated Python (azure-mgmt-compute==38.0.0, azure/mgmt/compute/models/_models.py) contains:

class OSProfile(_Model):
    """...
    :ivar admin_password: ... Has a special character (Regex match [\W_]) ...
    ...
    """

Importing the module on Python 3.12 produces (issue Azure/azure-sdk-for-python#47011):

SyntaxWarning: invalid escape sequence ''\W''
  <br>Has upper characters <br> Has a digit <br> Has a special character (Regex match [\W_])

Expected

When emitting a Python docstring, the Python emitter should ensure the resulting Python string literal is valid — either by:

  1. Writing the docstring as a raw string (r"""..."""), or
  2. Escaping every backslash in the doc text (\\\) before writing it into the Python source.

This should be done unconditionally for all generated docstrings (class docstrings, attribute :ivar/:vartype/:keyword lines, operation parameter docs, etc.), because users cannot reasonably be expected to pre-escape every @doc in TypeSpec just for the Python emitter — and doing so would corrupt the REST API description / other-language SDKs.

Current workaround

Use @@clientDoc(..., DocumentationMode.replace, "python") in client.tsp to override the Python doc text with manually double-escaped backslashes. Example PR: Azure/azure-rest-api-specs#43481

This is fragile — it requires every service team to discover the warning, then duplicate and hand-escape long doc strings just for Python.

Environment

Metadata

Metadata

Assignees

Labels

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

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions