python / python/mypy

Plugin hook to customize __repr__ of an Instance

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

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

feature priority-1-normal topic-plugins
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ả

For example, in Django, there's this QuerySet object with a lot of methods, and to properly support those methods we need two (and maybe more in the future) generic parameters.

With get_type_analyze_hook I can convert QuerySet[MyModel] into QuerySet[MyModel, MyModel], so in the code user don't need to think about number of generics. But in error messages, all those generics values are present, though they don't have any semantics, it's just implementation detail.

Or, for Field, I need to have two generic params, one for type of __set__, one for __get__, so they appear in error message as (CharField for example)

django.db.models.fields.CharField[Union[builtins.str, builtins.int, django.db.models.expressions.Combinable], builtins.str]

None of this info makes any sense to users, so I want it to just be django.db.models.fields.CharField.

This is monkeypatching version that works for one of the cases, should be simple enough to add hook for that,

def make_queryset_repr_return_only_one_type():
    from mypy.types import TypeStrVisitor

    old_visit_instance = TypeStrVisitor.visit_instance

    def patched_visit_instance(self, t: Instance) -> str:
        if t.type.has_base(helpers.QUERYSET_CLASS_FULLNAME):
            return old_visit_instance(self, helpers.reparametrize_instance(t, [t.args[0]]))
        else:
            return old_visit_instance(self, t)

    TypeStrVisitor.visit_instance = patched_visit_instance

I can make a PR, if you approve it as a viable feature.

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 với TypeStrVisitor.visit_instance và cơ chế get_type_analyze_hook hiện có, sử dụng monkeypatch được trình bày làm tham chiếu về hành vi. Xác định cách một plugin hook được hỗ trợ sẽ tùy chỉnh việc kết xuất Instance, đồng thời xem các ví dụ về QuerySet và Field là tiêu chí hoàn thành: các đối số generic chỉ dành cho việc triển khai không còn được xuất hiện trong các lỗi kiểu hướng đến người dùng.

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
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
Khá rõ ràng
Mức phù hợp với người mới
30/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.