python / python/typing

Proposal: Programmatically create types

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

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

topic: feature
Ngôn ngữ chính
Python
Star
1.8k
Fork
302
Merge trung bình
23 giờ
Pull request đã merge (30 ngày)
8

Mô tả

This proposal is aimed at solving two related problem. First, when defining a multi-ary operator on Numpy arrays, e.g., leaky integral, then you ideally want to bake in Numpy type promotion rules. However, even Numpy doesn't use its own type promotion rules in its type annotations.

So, my suggestion is the following:

from typing import SyntheticType

def ResultType(*args: Any) -> SyntheticType[DType]:
  return result_type(*args)  # Something like this, but would have to deal with e.g., numpy.ndarray[typing.Any, numpy.dtype[numpy.floating[typing.Any]]]

# Then...
    @overload
    def __add__(self: T, other: U) -> NDArray[ResultType[T, U]]: ...  # type: ignore[misc]

The type checker could be called with a special argument, like --create_synthetic_stubs. This would

  • Run type checking and collect a list of unique argument tuples to synthetic type functions like ResultType.
  • Start a Python interpreter and call the synthetic type functions using the the arguments (which are Python objects representing types, e.g., T=float and U=numpy.ndarray[typing.Any, numpy.dtype[numpy.floating[typing.Any]]]).
  • It would then write the results to some pyi file in some canonical table-like format, like:
    `ResultType: SyntheticTypeMapping = {(float, float): float, ...}

The table could either be just stored in the cache. Users of the library would have to generate this file, which means type checkers run code in the library.

The other problem this solves is one that I would like to annotate dataclasses whose elements can be None or int besides whatever they've specified as. See here for a description as to why. It would be pretty easy to code a Python transformation from a dataclass type to a new dataclass type with the transformed field types.

I realize this is pretty extreme, but the payoff would be commensurate.

Hướng dẫn đóng góp

Chưa lập chỉ mục được hướng dẫn đóng góp cho kho mã nguồn này

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 đề xuất và các tham chiếu được liên kết đến tjax leaky_integral.py, chú thích kiểu của NumPy và pytrees của JAX. Xem xét workflow SyntheticType được đề xuất, bước thu thập type-checker, định dạng stub được tạo và phép biến đổi dataclass, sau đó xác định liệu phạm vi này có thể trở thành một thiết kế được thống nhất hay không. Công việc chỉ được xem là hoàn tất khi đã quyết định được hướng đi hoặc đặc tả, chứ không phải chỉ với một thay đổi nhỏ, riêng lẻ.

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

Đánh giá

Công nghệ
numpy, python
Lĩnh vực
devtools
Loại issue
Tính năng
Độ khó
5/5
Thời gian dự kiến
Hơn một tuần
Mức độ hoạt động
Đình trệ
Độ rõ ràng
Cần làm rõ
Mức phù hợp với người mới
20/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.