一个flake8插件,帮助您编写更好的列表/集合/字典推导式。
项目描述
一个flake8插件,帮助您编写更好的列表/集合/字典推导式。
正在审查Django项目? 查看我关于Flake8和其他许多代码质量工具的书籍Boost Your Django DX。
要求
Python 3.8到3.12支持。
安装
首先,使用pip安装
python -m pip install flake8-comprehensions
其次,如果您定义了Flake8的select设置,请将其添加到C4前缀。否则,插件应该默认激活。
规则
C400-402:不必要的生成器 - 重写为 推导式。
规则
C400 不必要的生成器 - 重写为列表推导式。
C401 不必要的生成器 - 重写为集合推导式。
C402 不必要的生成器 - 重写为字典推导式。
在生成器表达式周围使用 ,
将
- 重写为 <[f(x) for x in foo]>
将 set(f(x) for x in foo) 重写为 {f(x) for x in foo}
将 dict((x, f(x)) for x in foo) 重写为 {x: f(x) for x in foo}
C403-404: 不必要的列表推导 - 重写为 <set/dict> 推导。
规则
C403 不必要的列表推导 - 重写为集合推导。
C404 不必要的列表推导 - 重写为字典推导。
在调用 set 或 dict 时使用列表推导是不必要的,因为这些类型有等效的推导。例如
将 set([f(x) for x in foo]) 重写为 {f(x) for x in foo}
将 dict([(x, f(x)) for x in foo]) 重写为 {x: f(x) for x in foo}
C405-406: 不必要的 <list/tuple> 文字 - 重写为 <set/dict> 文字。
C405 不必要的 <list/tuple> 文字 - 重写为集合文字。
C406 不必要的 <list/tuple> 文字 - 重写为字典文字。
在调用 set 或 dict 时使用列表或元组文字是不必要的。例如
将 set([1, 2]) 重写为 {1, 2}
将 set((1, 2)) 重写为 {1, 2}
将 set([]) 重写为 set()
将 dict([(1, 2)]) 重写为 {1: 2}
将 dict(((1, 2),)) 重写为 {1: 2}
将 dict([]) 重写为 {}
C407: 不必要的 <dict/list> 推导 - <builtin> 可以接受一个生成器
该规则在版本 3.4.0 中被删除,因为它促进了惰性的增加,这可能导致错误。
C408: 不必要的 <dict/list/tuple> 调用 - 重写为文字。
调用例如 dict() 比使用空文字要慢,因为必须在全局作用域中查找名称 dict,以防它已被重新绑定。其他两种基本类型也是如此。例如
将 dict() 重写为 {}
将 dict(a=1, b=2) 重写为 {"a": 1, "b": 2}
将 list() 重写为 []
将 tuple() 重写为 ()
C409-410: 不必要的 <list/tuple> 传递给 <list/tuple>() - <advice>。
规则
C409 不必要的 <list/tuple> 传递给 tuple() - <advice>。
C410 不必要的列表传递给 list() - <advice>。
其中 <advice> 是以下之一
移除外部的 <list/tuple>() 调用
重写为 <list/tuple> 文字
在调用 list 或 tuple 时使用列表或元组文字是不必要的,因为这些类型有文字语法。例如
将 tuple([1, 2]) 重写为 (1, 2)
将 tuple((1, 2)) 重写为 (1, 2)
将 tuple([]) 重写为 ()
将 list([1, 2]) 重写为 [1, 2]
将 list((1, 2)) 重写为 [1, 2]
将 list([]) 重写为 []
C411: 不必要的列表调用 - 移除对 list() 的外部调用。
在列表推导式周围使用列表是不必要的,因为它没有它也是等价的。例如
将 list([f(x) for x in foo]) 重写为 [f(x) for x in foo]
C412: 不必要的 <dict/list/set> 推导式 - ‘in’ 可以接受一个生成器。
该规则在版本 3.4.0 中被删除,因为它促进了惰性的增加,这可能导致错误。
C413: 在 sorted() 附近不必要的 <list/reversed> 调用。
没有必要在 sorted() 附近使用 list(),因为它已经返回一个列表。同样,没有必要在 sorted() 附近使用 reversed(),因为后者有 reverse 参数。例如
将 list(sorted([2, 3, 1])) 重写为 sorted([2, 3, 1])
将 reversed(sorted([2, 3, 1])) 重写为 sorted([2, 3, 1], reverse=True)
将 reversed(sorted([2, 3, 1], reverse=True)) 重写为 sorted([2, 3, 1])
C414: 在 <list/set/sorted/tuple> 内不必要的 <list/reversed/set/sorted/tuple> 调用。
没有必要通过将列出的函数包装在 list/set/sorted/tuple 中来双重转换或双重处理可迭代对象。例如
将 list(list(iterable)) 重写为 list(iterable)
将 list(tuple(iterable)) 重写为 list(iterable)
将 tuple(list(iterable)) 重写为 tuple(iterable)
将 tuple(tuple(iterable)) 重写为 tuple(iterable)
将 set(set(iterable)) 重写为 set(iterable)
将 set(list(iterable)) 重写为 set(iterable)
将 set(tuple(iterable)) 重写为 set(iterable)
将 set(sorted(iterable)) 重写为 set(iterable)
将 set(reversed(iterable)) 重写为 set(iterable)
将 sorted(list(iterable)) 重写为 sorted(iterable)
将 sorted(tuple(iterable)) 重写为 sorted(iterable)
将 sorted(sorted(iterable)) 重写为 sorted(iterable)
将 sorted(reversed(iterable)) 重写为 sorted(iterable)
C415: 在 <reversed/set/sorted> 内不必要的可迭代对象下标反转。
在传递给列出的函数之一时,没有必要反转可迭代对象的顺序,因为该函数会再次更改顺序。例如
将 set(iterable[::-1]) 重写为 set(iterable)
将 sorted(iterable)[::-1] 重写为 sorted(iterable, reverse=True)
将 reversed(iterable[::-1]) 重写为 iterable
C416: 不必要的 <dict/list/set> 推导式 - 使用 <dict/list/set> 重写。
如果元素没有改变,则无需使用字典/列表/集合推导式来构建数据结构。请用 dict()、list() 或 set() 包装可迭代对象。例如
将 {a: b for a, b in iterable} 修改为 dict(iterable)
将 [x for x in iterable] 修改为 list(iterable)
将 {x for x in iterable} 修改为 set(iterable)
C417:不必要的 map 使用 - 使用生成器表达式/推导式重写。
map(func, iterable) 在 func 是内置函数且你的函数已经命名时,性能很好。但如果 func 是 lambda 表达式,使用生成器表达式或推导式会更快,因为它避免了函数调用的开销。例如
将 map(lambda x: x + 1, iterable) 修改为 (x + 1 for x in iterable)
将 map(lambda item: get_id(item), items) 修改为 (get_id(item) for item in items)
将 list(map(lambda num: num * 2, nums)) 修改为 [num * 2 for num in nums]
将 set(map(lambda num: num % 2 == 0, nums)) 修改为 {num % 2 == 0 for num in nums}
将 dict(map(lambda v: (v, v ** 2), values)) 修改为 {v : v ** 2 for v in values}
C418:不必要的 <dict/dict comprehension> 传递给 dict() - 删除对 dict() 的外部调用
在字典字面量或字典推导式周围使用字典是不必要的,因为这两种语法已经构建了一个字典。例如
将 dict({}) 修改为 {}
将 dict({"a": 1}) 修改为 {"a": 1}
C419:在 <any/all>() 中不必要的列表推导式阻止了短路 - 用生成器重写。
在 any()/all() 调用中使用列表推导式阻止了短路,当找到一个 True / False 值时。在调用 any()/all() 之前将构建整个列表,可能浪费了部分工作。请将其重写为生成器表达式,它可以在中途停止。例如
将 all([condition(x) for x in iterable]) 修改为 all(condition(x) for x in iterable)
将 any([condition(x) for x in iterable]) 修改为 any(condition(x) for x in iterable)
C420:不必要的字典推导式 - 使用 dict.fromkeys() 重写。
使用字典推导式来构建所有值都设置为相同常量的字典是不必要的。请使用 dict.fromkeys() 代替,它更快。例如
将 {x: 1 for x in iterable} 修改为 dict.fromkeys(iterable, 1)
将 {x: None for x in iterable} 修改为 dict.fromkeys(iterable)
项目详情
下载文件
下载适合您平台的文件。如果您不确定选择哪个,请了解有关安装包的更多信息。
源代码分发
构建分发
哈希值 for flake8_comprehensions-3.15.0-py3-none-any.whl
算法 | 哈希摘要 | |
---|---|---|
SHA256 | b7e027bbb52be2ceb779ee12484cdeef52b0ad3c1fcb8846292bdb86d3034681 |
|
MD5 | 55fb1af0ed0627a4d6f2852869c49864 |
|
BLAKE2b-256 | 3aaa93667d6f398749d1a9dd37d646e092f9f1baade7cbac948331b50a1d513c |