palantir / conjure-python

Conjure generator for Python clients
Apache License 2.0
19 stars 18 forks source link

Wrap indent docstring in newlines for UnionSnippet (#777) #779

Closed LorenzoMartini closed 9 months ago

LorenzoMartini commented 9 months ago

Backport of https://github.com/palantir/conjure-python/pull/777

Before this PR

The UnionSnippet.emit(PythonPoetWriter poetWriter) method parses conjure class definitions into python code bindings. As part of the process, it extracts docs components from the apis and wraps them in the python docstring format, which is the triple-quote """.

If a docstring terminates with a " (which it shouldn't), that would cause a set of 4 consecutives ", which is invalid python and that will cause errors when reading from these files. For example, if I define my conjure api like

FaultyType:
  docs: this will say "ERROR"
  union:
    something: integer
    somethingElse: string

this will cause the following error when reading the generated conjure python bindings:

File "tmp.py", line 10
      """this will say "ERROR""""
                                                  ^
  SyntaxError: EOL while scanning string literal

After this PR

Fix up UnionSnippet to have newlines wrapping the content and extract the doc-writing into a separate method being reused across all cases.

==COMMIT_MSG== Wrap indent docstring in newlines for UnionSnippet ==COMMIT_MSG==

Possible downsides?

The pythonic way of doing docstrings has them in the format

"""Docstring
"""

but we currently do

"""
Docstring
"""

I will change that in a separate PR, this is a more time-sensitive fix.