python / python/cpython

`get_original_bases` does not return what `cls.__orig_bases__` returns

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

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

docs topic-typing
Ngôn ngữ chính
Python
Star
77.2k
Fork
35.9k
Chỉ số merge pull request
Chỉ số pull request đang chờ

Mô tả

Bug report

Bug description:

The types documentation states for the types.get_original_bases function:

For classes that have an __orig_bases__ attribute, this function returns the value of cls.__orig_bases__. For classes without the __orig_bases__ attribute, cls.__bases__ is returned.

I need the functionality of cls.__orig_bases__, but need to use types.get_original_bases to make the type checker happy.

A short MRE:

from types import get_original_bases
from typing import Generic, TypeVar

T = TypeVar("T")


class One(Generic[T]):
    pass


class Two(One[int]):
    pass


class Three(Two):
    pass


assert get_original_bases(One) == One.__orig_bases__
assert get_original_bases(Two) == Two.__orig_bases__
assert get_original_bases(Three) == Three.__orig_bases__

# Traceback (most recent call last):
#   File "c:\<cut>\test.py", line 34, in <module>
#     assert get_original_bases(Three) == Three.__orig_bases__
#            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
# AssertionError

This is a easy solve, I would be happy to add a PR. Change here:

    try:
-       return cls.__dict__.get("__orig_bases__", cls.__bases__)
+       return getattr(cls, "__orig_bases__", cls.__bases__)
    except AttributeError:
        raise TypeError(
            f"Expected an instance of type, not {type(cls).__name__!r}"
        ) from None

To show this works:

from typing import Generic, TypeVar

T = TypeVar("T")


class One(Generic[T]):
    pass


class Two(One[int]):
    pass


class Three(Two):
    pass


def better_get_original_bases(cls):
    try:
        return getattr(cls, "__orig_bases__", cls.__bases__)
    except AttributeError:
        raise TypeError(
            f"Expected an instance of type, not {type(cls).__name__!r}"
        ) from None


assert better_get_original_bases(One) == One.__orig_bases__
assert better_get_original_bases(Two) == Two.__orig_bases__
assert better_get_original_bases(Three) == Three.__orig_bases__

# <No output>
CPython versions tested on:

3.12

Operating systems tested on:

Windows

Linked PRs
  • gh-156917

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 trong Lib/types.py tại get_original_bases và tái hiện ví dụ về tính kế thừa trong issue, bao gồm cả trường hợp lớp Three. Công việc được xem là hoàn tất khi hàm khớp với hành vi original-bases được tài liệu hóa và phạm vi kiểm thử hồi quy xác minh trường hợp thuộc tính được kế thừa; hãy xem lại PR được liên kết gh-156917 trước khi bắt đầu.

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
tooling
Loại issue
Lỗi
Độ khó
2/5
Thời gian dự kiến
1-3 giờ
Mức độ hoạt động
Đình trệ
Độ rõ ràng
Đặc tả rõ ràng
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.