检查是否将python内置代码用作变量或参数。

flake8-builtins的Python项目详细描述


https://travis-ci.org/gforcada/flake8-builtins.svg?branch=masterhttps://coveralls.io/repos/gforcada/flake8-builtins/badge.svg?branch=master&service=github

Flake8内置插件

检查是否将python内置代码用作变量或参数

想象一下这样的代码:

def max_values(list, list2):
    max = list[0]
    for x in list:
        if x > 0:
            max = x

    all_values = list()
    all_values.append(max)

    max = list2[0]
    for x in list2:
        if x > 0:
            max = x
    all_values.append(max)

    return all_values

max_values([3, 4, 5, ], [5, 6, 7])

最后一条语句没有像预期的那样返回[5, 7], 而是引发此异常:

Traceback (most recent call last):
  File "test.py", line 17, in <module>
    max_values([3,4,5], [4,5,6])
  File "bla.py", line 6, in max_values
    all_values = list()
TypeError: 'list' object is not callable

为什么?因为max_value函数的第一个参数是list一个python内置参数。 python允许重写它们,但在某些特定的用例中可能很有用, 一般的方法是not这样做,因为代码可能会突然中断,而没有清晰的跟踪。

示例

给定以下代码:

def my_method(object, list, dict):
    max = 5
    min = 3
    zip = (4, 3)

显示以下警告(通过flake8):

test.py:1:15: A002 "object" is used as an argument and thus shadows a python builtin, consider renaming the argument
test.py:1:23: A002 "list" is used as an argument and thus shadows a python builtin, consider renaming the argument
test.py:1:29: A002 "dict" is used as an argument and thus shadows a python builtin, consider renaming the argument
test.py:2:5: A001 "max" is a python builtin and is being shadowed, consider renaming the variable
test.py:3:5: A001 "min" is a python builtin and is being shadowed, consider renaming the variable
test.py:4:5: A001 "zip" is a python builtin and is being shadowed, consider renaming the variable

安装

使用pip安装:

$ pip install flake8-builtins

要求

  • Python2.7、3.5、3.6
  • 薄片8

许可证

GPL 2.0

变更日志

1.4.1(2018-05-11)

  • 修复1.4.0中的回归和与Flake8 Bugbear的交互。 [德克·托马斯]

1.4.0(2018-05-03)

  • 通过不假设节点是^{tt4},使代码更加健壮$ 但实际上是在检查。 [gforcada]
  • 也处理ast.Starred(即a, *int = range(4)) [gforcada]
  • 也处理列表,即[a, int] = 3, 4 [gforcada]

1.3.1(2018-04-30)

1.3.0(2018-04-13)

1.2.3(2018-04-10)

1.2.2(2018-04-03)

1.2.1(2018-04-01)

  • 从主分支重新发布1.2。 [gforcada]

1.2(2018-04-01)

  • 修复for循环中的错误消息。 [格沃卡达]

  • 检查以下地方是否有可能被遮蔽的建筑:

    • 打开('/tmp/bla.txt')为int
    • 除了值错误为int
    • [int表示范围(4)内的int]
    • 从zope.component导入提供为int
    • 将zope.component导入为int
    • 类int(对象)
    • 定义整数()
    • 异步定义int()
    • 范围(4)中的int异步
    • 将open('/tmp/bla.txt')作为int异步

    [gforcada]

1.1.1(2018-03-20)

  • 在for循环中分配的变量不仅可以是元组,还可以是元组内的元组。 [多普勒频移]

1.1.0(2018-03-17)

  • 更新更多的trove分类器。 [gforcada]
  • 检查在for循环中分配的变量。 感谢索博列夫的报道! [gforcada]

1.0.post0(2017-12-02)

  • 更新自述文件。 [Dmytrolitvinov]
  • 更新trove分类器。 [dirn]

1.0(2017-08-19)

0.4(2017-05-29)

  • 对类属性使用不同的代码。 [卡拉马诺列夫]

0.3.1.post0(2017-05-27)

  • 释放通用轮,而不仅仅是python 2轮。 [gforcada]
  • 更新trove分类器。 [gforcada]

0.3.1(2017-05-27)

  • 修复stdin处理。 [Sangiovanni]

0.3(2017-05-15)

  • 处理stdin,这是flake8集成到编辑器中的方式。 [gforcada]
  • 测试Python2.7、3.5、3.6和pypy。 [gforcada]

0.2(2016-03-30)

  • 白名单一些内置项。 [gforcada]

0.1(2016-03-04)

  • 初次发行 [gforcada]
  • 加上建筑和其他东西。 [gforcada]
  • 添加实际代码。 [gforcada]
  • 放弃对Python3.3的支持,只测试Python2.7和Python3.4。 [gforcada]

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java使用jain api上的编解码器信息自动创建sdp消息   显示素数误报的java程序   java如何从存储x、y值和整数索引之间的时间变化的两个数组列表创建动画?   java CompletableFuture:转换与组合   具有多个in运算符的java CrudePository findBy方法签名?   java:扩展小程序进程超时   java如何做mvn scm插件匿名pserver cvs访问   java如何在选中项目时将项目从一个活动发送到另一个活动   java存储子网的最佳数据结构,用于将Ip地址与子网匹配   java Jackson序列化以报告无法序列化的字段列表   如何将java mysql连接器导入IntelliJ?   如果java只在构造函数中编写,它是线程安全的集合吗?   java正则表达式捕获组在OR运算符后返回null   java使用Jackson在运行时将实体动态序列化为其ID或完整表示形式