python / python/cpython

Improving various error messages

Đang mở
#135,975 4 bình luận 1 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

interpreter-core topic-parser type-feature
Ngôn ngữ chính
Python
Star
77.2k
Fork
35.9k
Chỉ số merge pull request
Chỉ số pull request đang chờ

Mô tả

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

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Hướng nghiên cứu

Issue không nêu tên tệp hay bài kiểm thử nào; hãy bắt đầu bằng cách tái hiện các ví dụ cú pháp được liệt kê trong Python 3.13.5 và lần theo nơi các thông báo SyntaxError của chúng được tạo ra. Hoàn thành có nghĩa là thống nhất về các trường hợp còn bỏ ngỏ và cập nhật nhất quán các thông báo được đề xuất trong các kịch bản starred-expression và import được liệt kê.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
python
Lĩnh vực
compilers
Loại issue
Tính năng
Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức độ hoạt động
Đình trệ
Độ rõ ràng
Khá rõ ràng
Mức phù hợp với người mới
35/100

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.