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

[Fix]fix cleandoc with a first blank line #55052

Merged
merged 4 commits into from
Jul 3, 2023

Conversation

megemini
Copy link
Contributor

@megemini megemini commented Jul 1, 2023

PR types

Bug fixes

PR changes

Others

Description

修复 extract_code_blocks_from_docstr 里面的 inspect.cleandoc 不能正确处理 code-block 第一行的缩进问题。

参考 PEP 257 – Docstring Conventions

inspect.cleandoc 这个方法本来不是为 code 准备的,而是为 docstring 使用的。

而,docstring 中,默认第一行的缩进是无意义的:

Docstring processing tools will strip a uniform amount of indentation from the second and further lines of the docstring, equal to the minimum indentation of all non-blank lines after the first line. Any indentation in the first line of the docstring (i.e., up to the first newline) is insignificant and removed. Relative indentation of later lines in the docstring is retained.

也就是说,这个方法是按照第二行的缩进进行删减的,从而导致,如下的代码块可以正常解析:

In [80]: f
Out[80]: 
['            import paddle',
 '            with paddle.static.program_guard(main_program, startup_program):',
 "                data = paddle.static.data(name='image', shape=[None, 784, 784], dtype='float32')"]

In [81]: print(inspect.cleandoc('\n'.join(f)))
import paddle
with paddle.static.program_guard(main_program, startup_program):
    data = paddle.static.data(name='image', shape=[None, 784, 784], dtype='float32')

而,如下的代码块缩进会出问题:

In [82]: e
Out[82]: 
['            with paddle.static.program_guard(main_program, startup_program):',
 "                data = paddle.static.data(name='image', shape=[None, 784, 784], dtype='float32')"]

In [83]: print(inspect.cleandoc('\n'.join(e)))
with paddle.static.program_guard(main_program, startup_program):
data = paddle.static.data(name='image', shape=[None, 784, 784], dtype='float32')

修改后,默认添加一个空行,由此可以正常解析:

In [106]: print(inspect.cleandoc('\n' + '\n'.join(e)))
with paddle.static.program_guard(main_program, startup_program):
    data = paddle.static.data(name='image', shape=[None, 784, 784], dtype='float32')

关联 PR :

单测在 docs 那边的 PR 里面,这里就不放了~

@SigureMo 请评审!

@paddle-bot
Copy link

paddle-bot bot commented Jul 1, 2023

你的PR提交成功,感谢你对开源项目的贡献!
请关注后续CI自动化测试结果,详情请参考Paddle-CI手册
Your PR has been submitted. Thanks for your contribution!
Please wait for the result of CI firstly. See Paddle CI Manual for details.

@SigureMo
Copy link
Member

SigureMo commented Jul 1, 2023

单测在 docs 那边的 PR 里面,这里就不放了~

我的意思是在这个 PR 里加上 #54879 python/paddle/metric/metrics.py 这个文件的修改,如果这个 PR CI 通过了,那么就说明改动是没问题的,docs 那边不用加单测的 😂,这边改一下吧

@SigureMo
Copy link
Member

SigureMo commented Jul 1, 2023

如果这边通过了,会同步 merge docs 下的相应 PR

@megemini
Copy link
Contributor Author

megemini commented Jul 1, 2023

好了, 看看行不~

.. code-block:: python
:name: code-standalone-example
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

好了, 看看行不~

嗯嗯,没什么问题,不过这些代码块最好和上面一样统一修改成四缩进吧~

@megemini
Copy link
Contributor Author

megemini commented Jul 1, 2023

已修改缩进,请评审!

@RedContritio @SigureMo

Copy link
Member

@SigureMo SigureMo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

相关流水线已过 🎉

@SigureMo
Copy link
Member

SigureMo commented Jul 2, 2023

merge 一下 develop 吧,PR-CI-Windows-Inference 问题 develop 已解决

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

Successfully merging this pull request may close these issues.

3 participants