"Local variable (defined in enclosing scope) referenced before assignment" wording is misleading

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

还没有人认领这个 Issue。

评估

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

调研方向

首先定位格式化 F823 诊断信息的代码,然后将其措辞与本 issue 中的示例以及 #315 中的相关示例进行比较。完成的标准是:消息明确说明内部名称遮蔽了外层作用域的名称,而不是说外层变量在赋值前被引用。

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

描述

def f():
    x = None
    def g():
        # Programmer forgot to add `nonlocal` here.
        if x is None:
            x = "hello"

foo.py:5:12: F823 local variable 'x' (defined in enclosing scope on line 2) referenced before assignment

But it's not the variable defined on line 2 which is being referenced before assignment, it's the one defined on line 6. The programmer thinks they're using the one on line 2, and the message only reinforces their mistake.

Suggest rephrase to something like (shadowing name from line 2 in enclosing scope).

(There's a similar example in #315 where the enclosing scope was global.)

主要语言
Python
星标
1.5k
派生
207
平均合并
4 分钟
30 天内合并 PR
1

环境准备

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

从这里开始

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

PyCQA/pyflakes 的其他 Issue

查看 PyCQA/pyflakes 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 发到你的邮箱

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