Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optionally preserve sub-indentation while parsing? #51

Open
ntjess opened this issue Aug 2, 2021 · 4 comments · May be fixed by #52
Open

Optionally preserve sub-indentation while parsing? #51

ntjess opened this issue Aug 2, 2021 · 4 comments · May be fixed by #52

Comments

@ntjess
Copy link

ntjess commented Aug 2, 2021

Maybe this is unsupportable, but I figured it was worth asking. In my library, metadata for function arguments is stored in the docstring, looking something like this:

def a(x=5):
  """
  :param x: The x parameter
    limits: [0, 5, 10, 15]
  """

Under the hood, I can turn this into a configuration using ConfigParser. However, docstring_parser removes all indentation underneath a parameter. This is a problem for me, since ConfigParser requires multiline values to be indented. The below example doesn't work, since all sub-indentation underneath x is obliterated:

def a(x=5):
  """
  :param x: The x parameter
    limits: [
        0, 5, 10, 15
      ]
  """

docstring_parser.parse() turns x's description into:

The x parameter
limits: [
0, 5, 10, 15
]
^^^ Notice the lack of indentation

Is there some way to optionally use textwrap.dedent instead of strip() during the parsing logic for these cases? Thanks for your consideration.

@ntjess ntjess changed the title Optionally preserve spaces while parsing? Optionally preserve sub-indentation while parsing? Aug 2, 2021
@rr-
Copy link
Owner

rr- commented Aug 2, 2021

Thank you for the detailed report. Currently this feature isn't there and would need to be implemented.

@ntjess
Copy link
Author

ntjess commented Aug 2, 2021

Thanks for the reply. If this was to be a PR, what would you suggest for exposing it? Would enough people need the feature to add a keyword to parse? Would you make it a module parameter? Etc. I might be interested in creating such a feature, so I would love the feedback.

@rr-
Copy link
Owner

rr- commented Aug 2, 2021

I wouldn't add a new flag and just always keep the original indentation – it's easy to emulate old behavior with "\n".join(map(str.strip, docstring.splitlines())).

@ntjess ntjess linked a pull request Aug 2, 2021 that will close this issue
1 task
@abergou
Copy link
Contributor

abergou commented Aug 3, 2021

You might be able to do something simple manually using textwrap.dedent in place of inspect.getdoc.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants