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:
- Writing the docstring as a raw string (
r"""..."""), or
- 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
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:on Python 3.12+ (this will become a
SyntaxErrorin a future Python version).Repro
TypeSpec source (excerpt) —
specification/compute/resource-manager/Microsoft.Compute/Compute/Compute/models.tspinAzure/azure-rest-api-specs: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:Importing the module on Python 3.12 produces (issue Azure/azure-sdk-for-python#47011):
Expected
When emitting a Python docstring, the Python emitter should ensure the resulting Python string literal is valid — either by:
r"""..."""), or\→\\) before writing it into the Python source.This should be done unconditionally for all generated docstrings (class docstrings, attribute
:ivar/:vartype/:keywordlines, operation parameter docs, etc.), because users cannot reasonably be expected to pre-escape every@docin 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")inclient.tspto override the Python doc text with manually double-escaped backslashes. Example PR: Azure/azure-rest-api-specs#43481This is fragile — it requires every service team to discover the warning, then duplicate and hand-escape long doc strings just for Python.
Environment
azure-mgmt-compute==38.0.0(generated)