ast.parse creates only Constant(value=0) nodes for a `case +0:` pattern, which is inconsistent.
Chưa có ai nhận issue này.
- 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:
I found the following asymmetry in the generated ast while I was working with the new cpython 3.15.
c="""
match 0:
case +0:
pass
case -0:
pass
+0
-0
"""
import ast
print(ast.dump(ast.parse(c),indent=2))
output (Python 3.15.0b3+dev):
Module(
body=[
Match(
subject=Constant(value=0),
cases=[
match_case(
pattern=MatchValue(
value=Constant(value=0)),
body=[
Pass()]),
match_case(
pattern=MatchValue(
value=UnaryOp(
op=USub(),
operand=Constant(value=0))),
body=[
Pass()])]),
Expr(
value=UnaryOp(
op=UAdd(),
operand=Constant(value=0))),
Expr(
value=UnaryOp(
op=USub(),
operand=Constant(value=0)))])
+0 is parsed to Constant(value=0) instead of UnaryOp(op=UAdd(),operand=Constant(value=0)) when it is used as a match-expression.
This was really unexpected for me and can maybe produce some problems with ast.parse/unparse roundtripps or code which works with the ast and require workarounds (like in my case)
Is it possible to implement this in a way that it generates UnaryOp(...)?
@johnslavik you implemented this #148566, maybe you can help me here.
CPython versions tested on:
3.15
Operating systems tested on:
No response
Linked PRs
- gh-152712
Hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- 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.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Hướng nghiên cứu
Bắt đầu với ví dụ ast.parse trong issue và so sánh các node được tạo ra cho case +0:, case -0:, +0 và -0. Truy vết cách match-expression được biểu diễn, sau đó làm cho pattern +0 tạo ra dạng UnaryOp như mong đợi và xác minh đầu ra AST cũng như round trip ast.parse/unparse.
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