python / python/cpython

Improving various error messages

未关闭
#135,975 4 条评论 1 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

interpreter-core topic-parser type-feature
主要语言
Python
星标
77.2k
派生
35.9k
PR 合并指标
PR 指标待抓取

描述

Feature or enhancement

Proposal:

I propose improving the error messages as follows:

Starred expression

In the return type type hint

In Python 3.13.5:

>>> def func() -> *list:
  File "<python-input-0>", line 1
    def func() -> *list:
               ^^
SyntaxError: expected ':'

Proposed:

>>> def func() -> *list:
  File "<python-input-0>", line 1
    def func() -> *list:
                  ^^^^^
SyntaxError: can't use starred expression in the return type type hint
When unpacking to a set

In Python 3.13.5:

>>> {*x, y} = 1, 2
  File "<python-input-0>", line 1
    {*x, y} = 1, 2
    ^^^^^^^
SyntaxError: cannot assign to set display here. Maybe you meant '==' instead of '='?

Proposed:

>>> {*x, y} = 1, 2
  File "<python-input-0>", line 1
    {*x, y} = 1, 2
    ^^^^^^^
SyntaxError: starred assignment target must be in a list or tuple, not a set. Maybe you meant '==' instead of '='?
In type statement

In Python 3.13.5:

>>> type *Z = int
  File "<python-input-0>", line 1
    type *Z = int
    ^^^^^^^
SyntaxError: cannot assign to expression here. Maybe you meant '==' instead of '='?
>>> type Z = *int
  File "<python-input-1>", line 1
    type Z = *int
             ^
SyntaxError: invalid syntax

Proposed:

>>> type *Z = int
  File "<python-input-0>", line 1
    type *Z = int
         ^^
SyntaxError: cannot use starred expression in a type statement
>>> type Z = *int
  File "<python-input-1>", line 1
    type Z = *int
             ^^^^
SyntaxError: cannot use starred expression in a type statement
Assign to None/True/False

In Python 3.13.5:

>>> *None, _ = (1, 2)
  File "<python-input-1>", line 1
    *None, _ = (1, 2)
           ^^^^^^^^^^
SyntaxError: invalid syntax. Maybe you meant '==' or ':=' instead of '='?

Proposed:

>>> *None, _ = (1, 2)
  File "<python-input-1>", line 1
    *None, _ = (1, 2)
    ^^^^^
SyntaxError: cannot assign to None

(Same for True/False)

import statement

In Python 3.13.5:

>>> from typing import *, Any
  File "<python-input-0>", line 1
    from typing import *, Any
                        ^
SyntaxError: invalid syntax

Proposed:

>>> from typing import *, Any
  File "<python-input-0>", line 1
    from typing import *, Any
                       ^^^^^^
SyntaxError: can't use wildcard and identifier at the same time

Open questions:

"You don't need to use a starred expression here"?

In Python 3.13.5:

>>> lst = [1, 2, 3]
>>> a, b, c = *lst
  File "<python-input-1>", line 1
    a, b, c = *lst
              ^^^^
SyntaxError: can't use starred expression here

This is a tricky one. In my experience, this exists in many beginner codes, but I don't know how we can improve the error message here. The word "here" is very unclear IMHO.

Inconsistency of error messages

In Python 3.13.5:

>>> for *x in [[0]]: print(x)
  File "<python-input-0>", line 1
    for *x in [[0]]: print(x)
        ^^
SyntaxError: starred assignment target must be in a list or tuple
>>> for (*x) in [[0]]: print(x)
  File "<python-input-1>", line 1
    for (*x) in [[0]]: print(x)
         ^^
SyntaxError: cannot use starred expression here

I find the above inconsistency a little bit surprising, but I don't have a strong feeling about this.

Has this already been discussed elsewhere?

This is a minor feature, which does not need previous discussion elsewhere

Links to previous discussion of this feature:

No response

贡献指南

打开贡献指南

从这里开始

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

调研方向

该 issue 未指定任何文件或测试;首先在 Python 3.13.5 中复现列出的语法示例,并跟踪它们的 SyntaxError 消息在哪里生成。完成的标准是就开放问题中的案例达成一致,并在列出的带星号表达式和导入场景中一致地更新拟议的消息。

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

评估

技术栈
python
领域
compilers
Issue 类型
功能
难度
4/5
预计耗时
3-5 天
活跃度
停滞
描述清晰度
基本清楚
新手友好度
35/100

把新 issue 发到你的邮箱

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