question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Newline character in doctstring not supported

See original GitHub issue

My docstrings Examples have string arguments containing newline characters. Despite being placed inside a code section, they are broken:

class WikiPage():
  """
  The class for a Gitlab Wiki page. This class is used to handle a local copy of the wiki repository file (i.e., a wiki page gitlab.v4.objects.ProjectWiki or one of gitlab.v4.objects.Project.wikis)

  \```
    {
      'content' = '# My content with newlines\n\n',
      'format' = 'markdown',
  ...
  \```

image

Further, Examples (google-style) are broken as well:

image

here’s my YAML file:

# mkdocs.yml
site_name: Grafana Wiki
theme:
  name: "material"

markdown_extensions:
  - pymdownx.arithmatex

plugins:
  - mkdocstrings:
      handlers:
        python:
          docstring_style: "google"
          docstring_options: "true"
          setup_commands:
            - import sys
            - sys.path.append("..")
      watch:
        - ..
# index.md

::: WikiPage:WikiPage
    handler: "python"

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
pawamoycommented, Sep 23, 2021

In the end I will close this as there’s nothing we can do: it’s how Python works. Consider this:

In [1]: """first line
   ...: second line\nthird line
   ...: fourth line"""
Out[1]: 'first line\nsecond line\nthird line\nfourth line'

To Pyhon, your \n is not any different than a literal new line. \n is, actually, a new line character.

Two workarounds:

  • escape your newline characters:
class WikiPage:
    """
    ```
      {
        'content' = '# My content with newlines\\n\\n',
        'format' = 'markdown',
        ...
    ```
    """
  • mark your docstring as “raw”:
class WikiPage:
    r"""
    ```
      {
        'content' = '# My content with newlines\n\n',
        'format' = 'markdown',
        ...
    ```
    """

Feel free to further comment on this 🙂

1reaction
justinTMcommented, Sep 23, 2021

i’ll go with raw thank you for your help!

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to preserve line breaks when generating python docs ...
I am using Sphinx for generating docs for a python project. The output html is not preserving the line breaks which ...
Read more >
Making a string with new line characters using docstrings?...
Hi! This part of the video confused me; can someone (
Read more >
Black removes newline from a multiline docstring. · Issue #1635
I am expecting black to not remove newlines from strings. ... rule if the the docstring is within three characters of the line-length...
Read more >
Inconsistent Whitespace error occurrence when using Python ...
The docstring might not contain a completely valid Python statement but see that it contains a quoted string with the newline character in ......
Read more >
Built-in Functions — Python 3.11.1 documentation
When compiling a string with multi-line code in 'single' or 'eval' mode, input must be terminated by at least one newline character. This...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found