跳转到主要内容

一个flake8插件,帮助您编写更好的列表/集合/字典推导式。

项目描述

https://img.shields.io/github/actions/workflow/status/adamchainz/flake8-comprehensions/main.yml.svg?branch=main&style=for-the-badge https://img.shields.io/pypi/v/flake8-comprehensions.svg?style=for-the-badge https://img.shields.io/badge/code%20style-black-000000.svg?style=for-the-badge pre-commit

一个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 不必要的列表推导 - 重写为字典推导。

在调用 setdict 时使用列表推导是不必要的,因为这些类型有等效的推导。例如

  • 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> 文字 - 重写为字典文字。

在调用 setdict 时使用列表或元组文字是不必要的。例如

  • 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> 文字

在调用 listtuple 时使用列表或元组文字是不必要的,因为这些类型有文字语法。例如

  • 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 是内置函数且你的函数已经命名时,性能很好。但如果 funclambda 表达式,使用生成器表达式或推导式会更快,因为它避免了函数调用的开销。例如

  • 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)

项目详情


下载文件

下载适合您平台的文件。如果您不确定选择哪个,请了解有关安装包的更多信息。

源代码分发

flake8_comprehensions-3.15.0.tar.gz (13.1 kB 查看哈希值)

上传时间 源代码

构建分发

flake8_comprehensions-3.15.0-py3-none-any.whl (8.2 kB 查看哈希值)

上传时间 Python 3

支持单位: