dataclass tries to generate `__init__()` even though the class already defines one
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:
When running this code:
from dataclasses import dataclass
@dataclass
class A:
a: int = 1
@dataclass
class B(A):
b: int
def __init__(self, *args, **kwargs): ...
I got TypeError: non-default argument 'b' follows default argument
However, if I set init=False for the second dataclass, like this:
from dataclasses import dataclass
@dataclass
class A:
a: int = 1
@dataclass(init=False)
class B(A):
b: int
def __init__(self, *args, **kwargs): ...
There error disappeared.
I think the error comes from the process where dataclass tries to generate the default __init__() and checks the fields. But, if I understand correctly, this should not happen when the class already defines its own __init__().
Although I'm not sure whether the behavior should be defined as a bug, I do find the results inconsistant with the docs, which says
If the class already defines
__init__(), this parameter (init) is ignored.
so, intuitively, adding init=False shouldn't have changed the result.
CPython versions tested on:
3.12, 3.13, 3.14
Operating systems tested on:
Windows
Linked PRs
- gh-151184
- gh-153601
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
Tái hiện hai ví dụ bằng cách sử dụng dataclasses.dataclass, sau đó kiểm tra công việc được liên kết trong gh-151184 và gh-153601 trước khi quyết định liệu hành vi hoặc tài liệu có không nhất quán hay không. Được xem là hoàn tất khi cách xử lý dự kiến đối với một init hiện có đã được xác lập và được bao phủ bằng bằng chứng hồi quy phù hợp.
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
- backend
- Loại issue
- Lỗi
- Độ 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
- 25/100