python / python/mypy

Referring to class attributes of a generic type

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

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

feature priority-2-low topic-type-variables
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ả

I apologize for posting my question here as an issue; I am doing this out of desperation since my original post on StackOverflow went unanswered for three weeks. Below is a near-verbatim copy from StackOverflow.

I have an abstract base class which defines several class attributes which are types themselves (i.e., nested types); the attributes are overridden in concrete derived classes inheriting from the abstract class. The concrete derived classes are used with several other classes which are type-annotated as generics, where the type parameter has a type constraint that it must be derived from the abstract class. I want these other classes (generics) to refer to the class attributes (nested types) of the concrete classes. The following example should demonstrate the idea:

import typing

class Base:
    A: typing.Type[Base]

class One(Base):
    class A(Base):  # overrides Base.A
        pass

class Two(Base):
    class A(One):  # overrides Base.A
        pass


T = typing.TypeVar('T', bound=Base)

class Foo(typing.Generic[T]):                 # the generic
    def foo(self, a: typing.Type[T.A]) -> T:  # <-- here, see T.A
        ...

f: Foo[Two] = Foo()
f.foo(Two.A)        # line 22 is here

MyPy v0.701 reports an error on line 22:

Argument 1 to "foo" of "Foo" has incompatible type "Type[A]"; expected "Type[Two]"

MyPy seems to ignore the attribute reference at T.A. How do I make it understand that I am trying to refer to the type available via the attribute .A? The attribute is guaranteed to be available because the type variable T is constrained to Base, which has A. Note that extending the set of generic types is not an option; I could elaborate on this but the explanation would be too specific to my application.

UPDATE: MyPy v0.711 with --new-semantic-analyzer yields a more comprehensible error message:

Name 'T.A' is not defined

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

Issue không nêu tên các tệp hoặc test nào trong repository; hãy bắt đầu với ví dụ Python tối thiểu và theo dõi cách mypy xử lý các bound của TypeVar, các annotation generic và các tham chiếu kiểu của thuộc tính lớp. Công việc được xem là hoàn tất khi hành vi typing được yêu cầu có implementation hoặc limitation được xác định và được bao phủ bởi một regression test.

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
Cần làm rõ
Mức phù hợp với người mới
25/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.