Skip to content

Multi-line strings in fields produce issues when writing comments #333

@Doomerdinger

Description

@Doomerdinger

If you stringify or write to disk the model with add_comments=True (or with "fields-only") and fields have multi-line comments (with config use_attribute_docstrings=True) or multi-line field descriptions (description="My cool string.\nWith a newline") the output yaml will be malfomed.

from typing import Annotated

from pydantic import BaseModel, Field

class MyModel(BaseModel):
    """My custom model."""  # This will become the header!

    c: Annotated[float, Field(description="See three?\nPO")] = 3  # description will become a comment

will produce

# My custom model.
c: 3.14  # See three?
PO

Likewise something like

c: float = 3
"""
Comment line one
Comment line two
""" 

with attribute docstrings will have the same issue, where Comment line two will be on a newline, and not commented out.

It would appear multi-line model comments work fine.

Version used: 1.6.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workinggood first issueGood for newcomerspythonPull requests that update Python code

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions