Dataclass constructors defaults don't solve type parameters

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

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

Đánh giá

Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức phù hợp với người mới
57/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
python, rust

Hướng nghiên cứu

Start by reading the generated dataclass constructor logic and the generic argument solving path in Pyrefly; the issue does not name specific source files or tests. Use the Python example in the issue as a regression case: Generated() should reveal Generated[int], while Generated(5) should continue to reveal Generated[int]. Add or update a focused type-checker test for those results.

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

Mô tả

typechecking
Describe the Bug

Consider this code:

from dataclasses import dataclass
from typing import reveal_type

class Explicit[T]:
    def __init__(self, x: T = 5) -> None:
        self.x = x

@dataclass
class Generated[T]:
    x: T = 5  # pyrefly: ignore[bad-assignment]

reveal_type(Explicit())    # Explicit[int]
reveal_type(Generated())   # Generated[@_] — expected Generated[int]
reveal_type(Generated(5))  # Generated[int]

There's a gap in our generated dataclass constructor logic. The default value for x is not actually affecting how calls solve. It's clear from Explicit that we intend for default constructor arguments to solve generic tparams when suitable, but in Generated we ignore the fact that the default of 5 means T ought to solve to int.

Note that it's possible to make this example work with T = int, a tparam-level default. But that's not a good solution - as a rule we don't want to use tparam-level defaults (which are an advanced feature and have some probably-undesirable side effects, like making a bare Generated mean Generated[int] rather than Generated[Any]) for a case where they aren't necessary

Sandbox Link

https://pyrefly.org/sandbox/?project=v2.pVUxEoIwEPzKzaQACk1FQ-UTLOiQcSJkkBkFRqKT0kf4Ql_iJSGSqKSxhOxtLrndTTAyQ7afksFP1Y8osn4bTm3ViiIvlz0nM8jxgOmi3SSuyt8poLOa4Y3OO1g6AALTlDIwN1wcWL3CMvw6807oYHD6jm27cZIoJjL3j-pCtIt972zAxGllsy_heX8AlwMOmdfOUognVUQujwJ_S43A1vyAaWoYsqjR5opFqL31riOY4hyOQgxjRqkt7y8N5R2t-2qkXgUF5Sp2Y61-IHw66LU1RqT9R-ORGUAUUnikBI6IoGhf

(Only applicable for extension issues) IDE Information

No response

Ngôn ngữ chính
Rust
Star
7.2k
Fork
578
Chỉ số merge pull request
Không có pull request nào được merge trong 30 ngày

Chuẩn bị môi trường

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.

Issue khác của facebook/pyrefly

Tất cả issue của facebook/pyrefly

Issue tương tự

Thêm issue về Rust

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.