Shadowing and unshadowing a builtin makes pyflakes emit a F821

未关闭
#548 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
3/5
预计耗时
1-2 天
新手友好度
45/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
停滞
技术栈
python
领域
tooling

调研方向

首先使用 pyflakes 或 flake8 运行提供的 shadowing 和 unshadowing 示例,然后将其与 linkcheck 示例进行比较。跟踪 del 附近对 undefined names 的处理,并添加一个回归测试,证明删除后对有效的 builtin 或 gettext 的使用不会发出 F821;该测试仍应捕获真正未定义的名称。

由索引模型根据 Issue 内容生成。

描述

Consider this silly code:

int = 42
del int

x = int('42')
print(x)

This is perfectly valid Python: if you run it, it'll print 42.

flake8 emits a F821: undefined name 'int' on the penultimate line. I think it's because of the del int.

For a real-world example of this situation, consider a project that uses gettext and installs _ in builtins, but doesn't do that early enough to be available at import time of every module. Here's how linkchecker copes with that situation:

_ = lambda x: x
Fields = dict(
    realurl=_("Real URL"),
    cachekey=_("Cache key"),
...
    level=_("Level"),
    modified=_("Modified"),
)
del _

and this then causes flake8 to complain about _() being used elsewhere in the file:

./linkcheck/logger/__init__.py:278:19: F821 undefined name '_'
./linkcheck/logger/__init__.py:340:16: F821 undefined name '_'
./linkcheck/logger/__init__.py:377:22: F821 undefined name '_'
./linkcheck/logger/__init__.py:380:22: F821 undefined name '_'
./linkcheck/logger/__init__.py:382:22: F821 undefined name '_'
./linkcheck/logger/__init__.py:390:22: F821 undefined name '_'

flake8 --version reports

3.8.2 (flake8_2020: 1.6.0, mccabe: 0.6.1, pycodestyle: 2.6.0, pyflakes: 2.2.0) CPython 3.8.2 on Linux

主要语言
Python
星标
1.5k
派生
205
平均合并
1 天 2 小时
30 天内合并 PR
5

环境准备

这个项目没有提供开发容器、Dockerfile 或贡献指南,环境需要你自己搭建:先看它的 README,通用步骤见我们的新手贡献指南。

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

PyCQA/pyflakes 的其他 Issue

查看 PyCQA/pyflakes 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。