python / python/mypy

`tuple[*Ts]` subtype with extra type parameter not gradually assignable to itself

Đang mở
#19,908 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.

bug topic-pep-646
Ngôn ngữ chính
Python
Star
20.6k
Fork
3.3k
Chỉ số merge pull request
Chỉ số pull request đang chờ

Mô tả

This demonstrates the issue:

from typing import Any

class Array[DataT, *ShapeTs](tuple[*ShapeTs]): ...

type Float2D = Array[float, *tuple[int, int]]  # or Array[float, int, int]
type FloatND = Array[float, *tuple[Any, ...]]

def from_2d(a: Float2D) -> FloatND:
    return a  # mypy: ✅, pyright: ✅

def into_2d(a: FloatND) -> Float2D:
    return a  # mypy: ❌, pyright: ✅

https://mypy-play.net/?mypy=latest&python=3.13&flags=strict&gist=1b95c702df6de35fa3f61aa7b5dfef16

Without the additional DataT typar, the false positive disappears

from typing import Any

class Array[*ShapeTs](tuple[*ShapeTs]): ...

type Float2D = Array[*tuple[int, int]]  # or Array[int, int]
type FloatND = Array[*tuple[Any, ...]]

def from_2d(a: Float2D) -> FloatND:
    return a  # mypy: ✅, pyright: ✅

def into_2d(a: FloatND) -> Float2D:
    return a  # mypy: ✅, pyright: ✅

https://mypy-play.net/?mypy=latest&python=3.13&flags=strict&gist=be0dfedeb0e7ed14da359cd52d2901e7

The issue also disappears when tuple is no longer a base class

from typing import Any

class Array[float, *ShapeTs]: ...

type Float2D = Array[float, *tuple[int, int]]  # or Array[float, int, int]
type FloatND = Array[float, *tuple[Any, ...]]

def from_2d(a: Float2D) -> FloatND:
    return a  # mypy: ✅, pyright: ✅

def into_2d(a: FloatND) -> Float2D:
    return a  # mypy: ✅, pyright: ✅

https://mypy-play.net/?mypy=latest&python=3.13&flags=strict&gist=a1439e7c3037239bc4663b981534fb17

Potentially related issues:

  • #18665
  • #19106
  • #19109
  • #19110
  • #19860
  • #19858

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 bằng cách chạy hai bản tái hiện Python trong các ví dụ mypy-play được liên kết với mypy 3.13 strict settings, so sánh các phép gán from_2dinto_2d cùng các biến thể không có DataT hoặc không có lớp cơ sở tuple. Xem xét các issue liên quan #18665, #19106, #19109, #19110, #19860 và #19858; được xem là hoàn thành khi phép gán có vấn đề được xử lý nhất quán mà không làm hỏng các trường hợp vốn đã chạy được.

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
devtools
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
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.