flake8-bugbear是一个用于查找程序中可能存在的 bug 和设计问题的插件,错误码以B开头
B001: Do not use bare except:, it also catches unexpected events like memory errors, interrupts, system exit, and so on. Prefer except Exception:. If you’re sure what you’re doing, be explicit and write except BaseException:. Disable E722 to avoid duplicate warnings.
B001: 不要使用 bare except: ,它还可以捕获内存错误、中断、系统出口等意外事件。除了例外: 。如果你确定你正在做什么,那么明确的写除了 BaseException: 。禁用 e722以避免重复警告。
B002: Python does not support the unary prefix increment. Writing ++n is equivalent to +(+(n)), which equals n. You meant n += 1.
B002: Python 不支持一元前缀递增。写 + + n 等于 + (+ (n)) ,它等于 n。你的意思是 n + = 1。
B003: Assigning to os.environ doesn’t clear the environment. Subprocesses are going to see outdated variables, in disagreement with the current process. Use os.environ.clear() or the env= argument to Popen.
B003: 将任务分配给 os.environ 并不能清除环境。子流程将看到过时的变量,与当前流程不一致。对 Popen 使用 os.environment.clear ()或 env = 参数。
B004: Using hasattr(x, ‘call’) to test if x is callable is unreliable. If x implements custom getattr or its call is itself not callable, you might get misleading results. Use callable(x) for consistent results.
B004: 使用 hasattr (x,’_ call _’)来测试 x 是否可调用是不可靠的。如果 x 实现了自定义 getattr 或者它的调用本身不可调用,你可能会得到错误的结果。使用可调用(x)以获得一致的结果。
B005: Using .strip() with multi-character strings is misleading the reader. It looks like stripping a substring. Move your character set to a constant if this is deliberate. Use .replace() or regular expressions to remove string fragments.
B005: 使用。带有多个字符串的 strip ()会误导读者。它看起来像剥离子字符串。如果是故意的,则将角色设置为常量。使用。Replace ()或正则表达式来删除字符串片段。
B006: Do not use mutable data structures for argument defaults. They are created during function definition time. All calls to the function reuse this one instance of that data structure, persisting changes between them.
B006: 不要对参数默认值使用可变数据结构。它们是在函数定义时创建的。对该函数的所有调用都重用该数据结构的这个实例,并在它们之间持久化更改。
B007: Loop control variable not used within the loop body. If this is intended, start the name with an underscore.
B007: 循环控制变量不在循环体中使用,如果是这样的话,用下划线开始命名。
B008: Do not perform function calls in argument defaults. The call is performed only once at function definition time. All calls to your function will reuse the result of that definition-time function call. If this is intended, assign the function call to a module-level variable and use that variable as a default value.
B008: 在参数默认情况下不执行函数调用。在函数定义时只执行一次调用。对函数的所有调用都将重用定义时函数调用的结果。如果这是有意为之,那么将函数调用分配给模块级变量,并将该变量用作默认值。
B009: Do not call getattr(x, ‘attr’), instead use normal property access: x.attr. Missing a default to getattr will cause an AttributeError to be raised for non-existent properties. There is no additional safety in using getattr if you know the attribute name ahead of time.
B009: 不要调用 getattr (x,‘ attr’) ,而是使用 normal property access: x.attr。缺少 getattr 的默认值将导致为不存在的属性引发 AttributeError。如果您事先知道属性名称,那么使用 getattr 就没有额外的安全性。
B010: Do not call setattr(x, ‘attr’, val), instead use normal property access: x.attr = val. There is no additional safety in using setattr if you know the attribute name ahead of time.
B010: 不要调用 setattr (x,‘ attr’,val) ,而是使用 normal property access: x.attr = val。如果您事先知道属性名称,那么使用 setattr 就没有额外的安全性。
B011: Do not call assert False since python -O removes these calls. Instead callers should raise AssertionError().
B011: 不要调用 assert False,因为 python-o 会删除这些调用。相反,调用者应该引发 AssertionError ()。
B012: Use of break, continue or return inside finally blocks will silence exceptions or override return values from the try or except blocks. To silence an exception, do it explicitly in the except block. To properly use a break, continue or return refactor your code so these statements are not in the finally block.
B012: 使用 break、 continue 或 return inside finally block will silence exceptions or override return value from the try or except blocks。为了使异常无声,请在 except 块中显式执行此操作。若要正确使用中断,请继续或返回重构代码,使这些语句不在 finally 块中。
B013: A length-one tuple literal is redundant. Write except SomeError: instead of except (SomeError,):.
B013: length-one tuple literal 是冗余的。
B014: Redundant exception types in except (Exception, TypeError):. Write except Exception:, which catches exactly the same exceptions.
B014: Redundant Exception types in except (Exception,TypeError) : . Write except: ,它捕获完全相同的异常。
B015: Pointless comparison. This comparison does nothing but waste CPU instructions. Either prepend assert or remove it.
B015: 无意义的比较。这种比较只会浪费 CPU 指令。要么补充断言,要么删除它。
B016: Cannot raise a literal. Did you intend to return it or raise an Exception?
不能提升一个字面值。你是打算返回它还是提升一个例外值?
B017: self.assertRaises(Exception): should be considered evil. It can lead to your test passing even if the code being tested is never executed due to a typo. Either assert for a more specific exception (builtin or custom), use assertRaisesRegex, or use the context manager form of assertRaises (with self.assertRaises(Exception) as ex:) with an assertion against the data available in ex.
B017: self.assertRaises (例外) : 应被视为邪恶。它可以导致您的测试通过,即使正在测试的代码由于输入错误而从未执行。要么为更特定的异常(内建的或定制的)断言,使用 assertRaisesRegex,要么使用 assertRaises 的上下文管理器形式(使用 self.assertRaises (Exception)作为 ex:) ,对 ex 中可用的数据进行断言。
B018: Found useless expression. Either assign it to a variable or remove it.
B018: 找到无用的表达式。要么将其赋值给变量,要么将其删除。
B019: Use of functools.lru_cache or functools.cache on methods can lead to memory leaks. The cache may retain instance references, preventing garbage collection.
B019: 在方法上使用 functools.lru 缓存或 functools.cache 会导致内存泄漏。缓存可能保留实例引用,防止垃圾回收。
B020: Loop control variable overrides iterable it iterates
020: 循环/控制变量/代码重写迭代
B021: f-string used as docstring. This will be interpreted by python as a joined string rather than a docstring.
B021: f-string 用作文档字符串。这将被 python 解释为一个连接字符串,而不是文档字符串。
B022: No arguments passed to contextlib.suppress. No exceptions will be suppressed and therefore this context manager is redundant. N.B. this rule currently does not flag suppress calls to avoid potential false positives due to similarly named user-defined functions.
B022: 没有传递给 contextlib.suppress 的参数。不会抑制任何异常,因此此上下文管理器是多余的。注意,此规则目前并不标记抑制调用,以避免由类似命名的用户定义函数引起的潜在误报。
The following warnings are disabled by default because they are controversial. They may or may not apply to you, enable them explicitly in your configuration if you find them useful. Read below on how to enable.
下列警告默认情况下禁用,因为它们具有争议性。它们可能适用于您,也可能不适用于您,如果您认为它们有用,请在您的配置中显式启用它们。阅读下面关于如何启用。
B901: Using return x in a generator function used to be syntactically invalid in Python 2. In Python 3 return x can be used in a generator as a return value in conjunction with yield from. Users coming from Python 2 may expect the old behavior which might lead to bugs. Use native async def coroutines or mark intentional return x usage with # noqa on the same line.
B901: 在生成器函数中使用 return x 在 Python 2中曾经是语法无效的。在 Python 3中,可以在生成器中使用 x 作为结合 yield 的返回值。来自 python2的用户可能会期望旧的行为可能导致 bug。使用本地异步定义协同程序或者在同一行标记故意返回 x 用法和 # noqa。
B902: Invalid first argument used for method. Use self for instance methods, and cls for class methods (which includes new and init_subclass) or instance methods of metaclasses (detected as classes directly inheriting from type).
B902: 方法使用的第一个参数无效。对实例方法使用 self,对类方法(包括 _ new _ 和 _ init _ subclass _)或元类的实例方法(作为直接继承自类型的类检测)使用 cls。
B903: Use collections.namedtuple (or typing.NamedTuple) for data classes that only set attributes in an init method, and do nothing else. If the attributes should be mutable, define the attributes in slots to save per-instance memory and to prevent accidentally creating additional attributes on instances.
B903: 使用 collections.namedtuple (或者打字)。对于只在 _ init _ method 中设置属性,而不做其他任何事情的数据类,可以使用 NamedTuple)。如果属性应该是可变的,那么在 _ slots _ _ 中定义属性,以便保存每个实例的内存,并防止在实例上意外地创建其他属性。
B904: Within an except clause, raise exceptions with raise … from err or raise … from None to distinguish them from errors in exception handling. See the exception chaining tutorial for details.
B904: 在 except 子句中,用 raise… from err 或 raise… from None 提出异常,以区分异常处理中的错误。详细信息请参阅异常链教程。
B950: Line too long. This is a pragmatic equivalent of pycodestyle’s E501: it considers “max-line-length” but only triggers when the value has been exceeded by more than 10%. You will no longer be forced to reformat code due to the closing parenthesis being one character too far to satisfy the linter. At the same time, if you do significantly violate the line length, you will receive a message that states what the actual limit is. This is inspired by Raymond Hettinger’s “Beyond PEP 8” talk and highway patrol not stopping you if you drive < 5mph too fast. Disable E501 to avoid duplicate warnings. Like E501, this error ignores long shebangs on the first line and urls or paths that are on their own line:
B950: 队伍太长。这是一个实用的 pycodestyle 的 e501等价物: 它考虑“ max-line-length”,但只在值超过10% 时触发。您将不再被迫重新格式化代码,因为结束括号是一个字符太远,以满足临时。同时,如果您确实严重违反了行的长度,那么您将收到一条消息,说明实际的限制是什么。这个灵感来自于雷蒙德 · 赫廷格的“超越 PEP 8”演讲和高速公路巡警不会阻止你,如果你开车速度超过每小时5英里。禁用 e501以避免重复警告。像 e501一样,这个错误忽略了第一行上的 shebangs,以及它们自己行上的 url 或路径:
#! long shebang ignored
# https://some-super-long-domain-name.com/with/some/very/long/paths
url = (
"https://some-super-long-domain-name.com/with/some/very/long/paths"
)
To enable Bugbear’s opinionated checks (B9xx), specify an --extend-select command-line option or extend-select= option in your config file (requires flake8 >=4.0):
要启用 Bugbear 的 opinionated checks (B9xx) ,请在配置文件中指定 – extend-select 命令行选项或 extend-select = option (需要 flake8 > = 4.0) :
[flake8]
max-line-length = 80
max-complexity = 12
...
extend-ignore = E501
extend-select = B950