python / python/cpython

tokenize: `e.offset` on `IndentationError` is not set correctly

Đang mở
#153,837 3 bình luận 0 reaction 0 người được giao Xem trên GitHub

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

3.14 3.15 3.16 interpreter-core topic-parser type-bug
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ả

Bug report

Bug description:
import io, tokenize

input_s = """
def foo():
    x = 1
   x = "some_very_long_expression"
"""
try:
    list(tokenize.generate_tokens(io.StringIO(input_s).readline))
except IndentationError as e:
    print(f"{e.offset = }")

The line x = "some_very_long_expression" is indented with 3 spaces instead of 4, so we get an error.
We would expect the offset to be around 3, since that is where the issue is.
On python 3.12+, the offset is instead 35, equal to the length of the line + 1.

This appears to be caused by the refactor of tokenize done in 3.12 for PEP 701
Potentially related to https://github.com/python/cpython/issues/84515
Funnily enough, the exact same thing seems to have happened around 3.8 https://github.com/python/cpython/issues/75676

Tested versions:

Version e.offset
3.11.15 3
3.12.13 35
3.13.13 35
3.14.4 35
3.15.0b1 35

Testing command:
nix-shell -p python311 --run "python --version && python tmp.py"
(and replace 311 by 312, 313, etc)

CPython versions tested on:

3.15

Operating systems tested on:

Linux

Linked PRs
  • gh-154281

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

Bắt đầu với tokenize.generate_tokens và tái hiện ví dụ IndentationError đã cung cấp trên các phiên bản Python được liệt kê. Theo dõi cách tokenizer tạo IndentationError và so sánh offset với hành vi của Python 3.11. Được xem là hoàn tất khi dòng bị thụt lề sai báo cáo offset gần 3, với kiểm thử hồi quy cho ví dụ này.

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
Lỗi
Độ khó
3/5
Thời gian dự kiến
1-2 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.