python 绳 : How to Find all missing imports and errors in all sub modules refactoring

标签 python refactoring automated-tests

我正在尝试为每个模块及其子模块查找所有缺失的导入语句和错误。

有专门的工具可以满足我的需求吗?

我写的代码,但看起来真的很糟糕,也许这样的东西已经存在了?:

import os
def find_missing_imports(walk):
    for items in walk:
        d = items[0]
        f_list = items[1]
        for f in f_list:
            module = f[:-3]
            # posix_path
            module_path = d.lstrip('.').replace('/','.').lstrip('.')
            try:
                __import__(module_path, fromlist=[module])
            except IndentationError, e:
                #print(f,e)
                pass
            except NameError, e:
                print(d,f,e)
                pass
            except Exception, e:
                print(f,e)
                pass

walk = [[root,files] for root,dirs,files in os.walk('.') for fn in files if  fn.endswith('.py')]
find_missing_imports(walk)

输出:

.[snip]
('./Sky_Group_Inventory_Scanner-wxpython/display_image/Dialogs', 'ImageSelectionFrame.py', NameError("name 'wx' is not defined",))
('./Sky_Group_Inventory_Scanner-wxpython/display_image/Dialogs', 'ItemSpecificsDialog.py', NameError("name 'wx' is not defined",))
('./Sky_Group_Inventory_Scanner-wxpython/display_image/Dialogs', 'ReturnCorrectWatchTitle.py', NameError("name 'wx' is not defined",))
.[snip]

重构前我的项目一团糟但有点用,现在重构后它坏了。

根据我在 codereview 上的初始帖子中的建议阅读“实用程序员”后:

我一直在挖掘以下源代码:

/usr/local/lib/python2.7/dist-packages/rope

ROPE 的文档似乎有点稀疏。我也一直在使用 Ninja-IDE,但未能找到解决我面临的问题的方法。

总的来说,我认为我错过了重构的全部内容。

可以看到当前的父目录布局here.

enter image description here

与原来相比before.

before

如能提供任何帮助,无论是关于填补缺失的术语,还是对于我所要问的问题,都将是极好的。

解决方案:

pylint -E/path/to/module

最佳答案

pip install pylint

将 pylint 指向有问题的文件夹/模块:

pylint/path/to/module > pylint_output

/path/to/module 是您感兴趣的 python 模块的位置。

例如:

my_project
   |____  moduleA
             |____  __init__.py
             |____  A.py
   |____  moduleB
             |____  __init__.py
             |____  B.py
  • ./my_project/moduleA
  • ./my_project/moduleB

这将创建一个包含全局评估的文件:

  • undefined-variable <-- 这是我在每个文件中寻找的内容。
  • 无效的名字
  • 行太长
  • 多余的括号
  • 坏空格
  • 属性定义外初始化
  • 缺少文档字符串
  • 缩进错误
  • 糟糕的延续
  • 广泛除外
  • 未使用的参数
  • 未使用的导入
  • 未使用的变量
  • 不得自用
  • 非成员(member)
  • 修复我
  • 不必要的通过
  • 多语句
  • 太多语句
  • 重复代码
  • 本地人太多
  • 缺少最后的换行符
  • 实例属性太多
  • 分支太多
  • 重新定义内置
  • 太多公共(public)方法
  • 语法错误
  • 相对导入
  • 可能没有成员
  • 导入错误
  • super 老同学
  • 除此之外
  • 未定义循环变量
  • 返回语句过多
  • 争论太多
  • 公共(public)方法太少
  • 星形参数
  • 重新导入
  • 索引异常
  • 无法访问
  • 行数过多
  • 重新定义外部名称
  • 旧类属性
  • 无意义的字符串语句
  • 毫无意义的陈述
  • 旧式类
  • 模块中没有名称
  • 全局变量未定义
  • 表达式未赋值
  • 坏订单除外
  • 无分配

有趣的是,我的问题的直接答案是在 pylint 结果中会有具有这种布局的行:

************* Module module_name.sub_module.class_name.method_name
R: line_no, column: Issue description 'some_name' (issue-type)
C: line_no, column: Issue description 'some_name' (issue-type)
W: line_no, column: Issue description 'some_name' (issue-type)
E: line_no, column: Issue description 'some_name' (issue-type)
F: line_no, column: Issue description 'some_name' (issue-type)
************* Module module_name.sub_module.class_name.method_name
R: line_no, column: Issue description 'some_name' (issue-type)
C: line_no, column: Issue description 'some_name' (issue-type)
W: line_no, column: Issue description 'some_name' (issue-type)
E: line_no, column: Issue description 'some_name' (issue-type)
F: line_no, column: Issue description 'some_name' (issue-type)    
  • [R]违反“良好做法”指标的因素
  • [C]违反编码标准的公约
  • [W]关于文体问题或小程序问题的警告
  • [E]重要编程问题的错误(即很可能是错误)
  • [F]阻止进一步处理的错误统计

因此,在我的大多数情况下,问题类型 (undefined-variable) 表示尚未导入的模块。 pylint -E/path/to/module 将仅返回 undefined variable 错误。

关于 python 绳 : How to Find all missing imports and errors in all sub modules refactoring,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23644805/

相关文章:

python - 从图像中提取补丁的更快方法?

支持头文件重构的C++ IDE

javascript - 如何通过消除双重等待和 DRY 代码进行重构?

unit-testing - `go test` 自动化测试的文件修改监控

windows - 针对特定软件的自动化工具建议(我认为是 C#)

java - Maven 从包中运行所有测试

python - 在子集上使用 dropna 进行清理

python - 如何在 Python/Numpy 中以最快的方式获取 "n"矩阵的最大值?

python - Django 平面页面 : sort list

c++ - 以 API 向后兼容的方式添加新的命名空间