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

[CodeStyle][isort] introduce isort (part5) #48404

Merged
merged 8 commits into from
Nov 28, 2022

Conversation

SigureMo
Copy link
Member

@SigureMo SigureMo commented Nov 25, 2022

PR types

Others

PR changes

Others

Describe

引入 isort 第五部分,格式化 python/paddle/tensor 目录及 python/paddle/common_ops_import.py

Note

为避免冲突,本 PR 不修改配置,将在主要 PR merge 后统一修改

Related links

@paddle-bot
Copy link

paddle-bot bot commented Nov 25, 2022

你的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 SigureMo changed the title [CodeStyle][isort] introduce isort (part5) [WIP][CodeStyle][isort] introduce isort (part5) Nov 25, 2022
@paddle-bot-old paddle-bot-old bot added the contributor External developers label Nov 25, 2022
@SigureMo SigureMo changed the title [WIP][CodeStyle][isort] introduce isort (part5) [CodeStyle][isort] introduce isort (part5) Nov 25, 2022
@@ -37,5 +37,7 @@
convert_dtype,
)
from paddle.fluid.layers import fill_constant, utils, scale # noqa: F401
from paddle.tensor.layer_function_generator import templatedoc # noqa: F401
from paddle.fluid.layers.layer_function_generator import ( # noqa: F401
Copy link
Member Author

@SigureMo SigureMo Nov 25, 2022

Choose a reason for hiding this comment

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

因为格式化后变化有点多,这里的实际 diff 如下:

# python/paddle/common_ops_import.py
- from paddle.tensor.layer_function_generator import templatedoc  # noqa: F401
+ from paddle.fluid.layers.layer_function_generator import (  # noqa: F401
+     templatedoc,
+ )

# python/paddle/tensor/math.py
- from paddle.common_ops_import import templatedoc
+ from .layer_function_generator import templatedoc

因为按照默认排序在 import paddle 时就会报如下错误

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/home/aistudio/Projects/Paddle/build/python/paddle/__init__.py", line 30, in <module>
    monkey_patch_variable()
  File "/home/aistudio/Projects/Paddle/build/python/paddle/fluid/layers/math_op_patch.py", line 600, in monkey_patch_variable
    import paddle.tensor
  File "/home/aistudio/Projects/Paddle/build/python/paddle/tensor/__init__.py", line 15, in <module>
    from .attribute import is_complex  # noqa: F401
  File "/home/aistudio/Projects/Paddle/build/python/paddle/tensor/attribute.py", line 24, in <module>
    from ..static import Variable
  File "/home/aistudio/Projects/Paddle/build/python/paddle/static/__init__.py", line 18, in <module>
    from . import nn  # noqa: F401
  File "/home/aistudio/Projects/Paddle/build/python/paddle/static/nn/__init__.py", line 15, in <module>
    from .common import fc  # noqa: F401
  File "/home/aistudio/Projects/Paddle/build/python/paddle/static/nn/common.py", line 20, in <module>
    from paddle.common_ops_import import (
  File "/home/aistudio/Projects/Paddle/build/python/paddle/common_ops_import.py", line 40, in <module>
    from paddle.tensor.layer_function_generator import templatedoc  # noqa: F401
  File "/home/aistudio/Projects/Paddle/build/python/paddle/tensor/layer_function_generator.py", line 32, in <module>
    from ..static import Variable

貌似是因为有循环依赖 tensor -> static -> common_ops_import -> tensor,因为这里 common_ops_import 包含一个从 paddle.tensor import 的函数 templatedoclayer_function_generator 里的函数)

这就导致任何 import common_ops_import 的模块都会去 import tensor,很容易产生循环依赖,因此这里将这个 templatedoc 改为从 fluid.layers 里直接 import 了(tensor 下的 templatedoc 是从 fluid.layers copy 过来的,内容完全一致)

修改后 common_ops_import 里的全是 fluid 下的 API,可以保证非 fluid 模块引用时不会产生循环依赖

这里用图说明下改动:

Before After
circular-import-before drawio circular-import-after drawio

之后 common_ops_import 里的 templatedoc 应该会在清理 fluid 的时候就被清理掉了

@luotao1 luotao1 self-assigned this Nov 28, 2022
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.

2 participants