unionai-oss / pandera

A light-weight, flexible, and expressive statistical data testing library

Home Page:https://www.union.ai/pandera

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Unescaped description gives error converting Model -> Schema -> Script

nathanjmcdougall opened this issue · comments

Describe the bug
When converting a DataFrameModel to a schema and then to a script, the docstring of the DataFrameModel is not escaped properly.

  • I have checked that this issue has not already been reported.
  • I have confirmed this bug exists on the latest version of pandera.
  • (optional) I have confirmed this bug exists on the master branch of pandera.

Note: Please read this guide detailing how to provide the necessary information for us to reproduce your bug.

Code Sample, a copy-pastable example

from pandera import DataFrameModel

class A(DataFrameModel):
    """print('gotcha')"""

print(A.to_schema().to_script()) 

Here is the printed code:

from pandera import DataFrameSchema, Column, Check, Index, MultiIndex

schema = DataFrameSchema(
    columns={},
    checks=None,
    index=None,
    dtype=None,
    coerce=False,
    strict=False,
    name="A",
    ordered=False,
    unique=None,
    report_duplicates="all",
    unique_column_names=False,
    add_missing_columns=False,
    title=None,
    description=print('gotcha'), # Uh oh!
)

This also gives an error if invalid Python is provided in the docstring, etc.

Expected behaviour

The description argument in the generated code should be the docstring from the class enclosed in a string.

I think, although I haven't checked, that the description generated by to_schema needs to escape the docstring.