python / python/typing_extensions

PEP-696 specialisation tests from CPython fail

Đang mở
#396 3 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
Ngôn ngữ chính
Python
Star
583
Fork
146
Merge trung bình
10 giờ 11 phút
Pull request đã merge (30 ngày)
5

Mô tả

We've fixed a number of issues with our PEP-696 backport in recent days, and we're now a lot closer to CPython's implementation on Python 3.13. We're still missing 5 specialisation-related tests from CPython's test suite, however, and they all fail using the typing_extensions implementation on Python 3.12:

Missing tests
diff --git a/src/test_typing_extensions.py b/src/test_typing_extensions.py
index fa04e59..0ab1e7f 100644
--- a/src/test_typing_extensions.py
+++ b/src/test_typing_extensions.py
@@ -6402,6 +6402,26 @@ class TypeVarLikeDefaultsTests(BaseTestCase):
         class A(Generic[Unpack[Ts]]): ...
         Alias = Optional[Unpack[Ts]]
 
+    def test_typevartuple_specialization(self):
+        T = TypeVar("T")
+        Ts = TypeVarTuple('Ts', default=Unpack[Tuple[str, int]])
+        self.assertEqual(Ts.__default__, Unpack[Tuple[str, int]])
+        class A(Generic[T, Unpack[Ts]]): ...
+        self.assertEqual(A[float].__args__, (float, str, int))
+        self.assertEqual(A[float, range].__args__, (float, range))
+        self.assertEqual(A[float, *tuple[int, ...]].__args__, (float, *tuple[int, ...]))
+
+    def test_typevar_and_typevartuple_specialization(self):
+        T = TypeVar("T")
+        U = TypeVar("U", default=float)
+        Ts = TypeVarTuple('Ts', default=Unpack[Tuple[str, int]])
+        self.assertEqual(Ts.__default__, Unpack[Tuple[str, int]])
+        class A(Generic[T, U, Unpack[Ts]]): ...
+        self.assertEqual(A[int].__args__, (int, float, str, int))
+        self.assertEqual(A[int, str].__args__, (int, str, str, int))
+        self.assertEqual(A[int, str, range].__args__, (int, str, range))
+        self.assertEqual(A[int, str, *tuple[int, ...]].__args__, (int, str, *tuple[int, ...]))
+
     def test_no_default_after_typevar_tuple(self):
         T = TypeVar("T", default=int)
         Ts = TypeVarTuple("Ts")
@@ -6487,6 +6507,34 @@ class TypeVarLikeDefaultsTests(BaseTestCase):
         a4 = Callable[[Unpack[Ts]], T]
         self.assertEqual(a4.__args__, (Unpack[Ts], T))
 
+    def test_paramspec_specialization(self):
+        T = TypeVar("T")
+        P = ParamSpec('P', default=[str, int])
+        self.assertEqual(P.__default__, [str, int])
+        class A(Generic[T, P]): ...
+        self.assertEqual(A[float].__args__, (float, (str, int)))
+        self.assertEqual(A[float, [range]].__args__, (float, (range,)))
+
+    def test_typevar_and_paramspec_specialization(self):
+        T = TypeVar("T")
+        U = TypeVar("U", default=float)
+        P = ParamSpec('P', default=[str, int])
+        self.assertEqual(P.__default__, [str, int])
+        class A(Generic[T, U, P]): ...
+        self.assertEqual(A[float].__args__, (float, float, (str, int)))
+        self.assertEqual(A[float, int].__args__, (float, int, (str, int)))
+        self.assertEqual(A[float, int, [range]].__args__, (float, int, (range,)))
+
+    def test_paramspec_and_typevar_specialization(self):
+        T = TypeVar("T")
+        P = ParamSpec('P', default=[str, int])
+        U = TypeVar("U", default=float)
+        self.assertEqual(P.__default__, [str, int])
+        class A(Generic[T, P, U]): ...
+        self.assertEqual(A[float].__args__, (float, (str, int), float))
+        self.assertEqual(A[float, [range]].__args__, (float, (range,), float))
+        self.assertEqual(A[float, [range], int].__args__, (float, (range,), int))

Here are the test failures if I add those tests to our suite:

(main)⚡ % python test_typing_extensions.py   ~/dev/typing_extensions/src
.................................................................................................................................................................s..s..s...........................................................................................................................................................................................E.E...EE...F..........................................s.........................
======================================================================
ERROR: test_paramspec_and_typevar_specialization (__main__.TypeVarLikeDefaultsTests.test_paramspec_and_typevar_specialization)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Users/alexw/dev/typing_extensions/src/test_typing_extensions.py", line 6534, in test_paramspec_and_typevar_specialization
    self.assertEqual(A[float].__args__, (float, (str, int), float))
                     ~^^^^^^^
  File "/Users/alexw/.pyenv/versions/3.12.3/lib/python3.12/typing.py", line 398, in inner
    return func(*args, **kwds)
           ^^^^^^^^^^^^^^^^^^^
  File "/Users/alexw/.pyenv/versions/3.12.3/lib/python3.12/typing.py", line 1082, in _generic_class_getitem
    params = prepare(cls, params)
             ^^^^^^^^^^^^^^^^^^^^
  File "/Users/alexw/.pyenv/versions/3.12.3/lib/python3.12/typing.py", line 1036, in _paramspec_prepare_subst
    raise TypeError(f"Too few arguments for {alias}")
TypeError: Too few arguments for <class '__main__.TypeVarLikeDefaultsTests.test_paramspec_and_typevar_specialization.<locals>.A'>

======================================================================
ERROR: test_paramspec_specialization (__main__.TypeVarLikeDefaultsTests.test_paramspec_specialization)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Users/alexw/dev/typing_extensions/src/test_typing_extensions.py", line 6515, in test_paramspec_specialization
    self.assertEqual(A[float].__args__, (float, (str, int)))
                     ~^^^^^^^
  File "/Users/alexw/.pyenv/versions/3.12.3/lib/python3.12/typing.py", line 398, in inner
    return func(*args, **kwds)
           ^^^^^^^^^^^^^^^^^^^
  File "/Users/alexw/.pyenv/versions/3.12.3/lib/python3.12/typing.py", line 1082, in _generic_class_getitem
    params = prepare(cls, params)
             ^^^^^^^^^^^^^^^^^^^^
  File "/Users/alexw/.pyenv/versions/3.12.3/lib/python3.12/typing.py", line 1036, in _paramspec_prepare_subst
    raise TypeError(f"Too few arguments for {alias}")
TypeError: Too few arguments for <class '__main__.TypeVarLikeDefaultsTests.test_paramspec_specialization.<locals>.A'>

======================================================================
ERROR: test_typevar_and_paramspec_specialization (__main__.TypeVarLikeDefaultsTests.test_typevar_and_paramspec_specialization)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Users/alexw/dev/typing_extensions/src/test_typing_extensions.py", line 6524, in test_typevar_and_paramspec_specialization
    self.assertEqual(A[float].__args__, (float, float, (str, int)))
                     ~^^^^^^^
  File "/Users/alexw/.pyenv/versions/3.12.3/lib/python3.12/typing.py", line 398, in inner
    return func(*args, **kwds)
           ^^^^^^^^^^^^^^^^^^^
  File "/Users/alexw/.pyenv/versions/3.12.3/lib/python3.12/typing.py", line 1082, in _generic_class_getitem
    params = prepare(cls, params)
             ^^^^^^^^^^^^^^^^^^^^
  File "/Users/alexw/.pyenv/versions/3.12.3/lib/python3.12/typing.py", line 1036, in _paramspec_prepare_subst
    raise TypeError(f"Too few arguments for {alias}")
TypeError: Too few arguments for <class '__main__.TypeVarLikeDefaultsTests.test_typevar_and_paramspec_specialization.<locals>.A'>

======================================================================
ERROR: test_typevar_and_typevartuple_specialization (__main__.TypeVarLikeDefaultsTests.test_typevar_and_typevartuple_specialization)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Users/alexw/dev/typing_extensions/src/test_typing_extensions.py", line 6420, in test_typevar_and_typevartuple_specialization
    self.assertEqual(A[int].__args__, (int, float, str, int))
                     ~^^^^^
  File "/Users/alexw/.pyenv/versions/3.12.3/lib/python3.12/typing.py", line 398, in inner
    return func(*args, **kwds)
           ^^^^^^^^^^^^^^^^^^^
  File "/Users/alexw/.pyenv/versions/3.12.3/lib/python3.12/typing.py", line 1082, in _generic_class_getitem
    params = prepare(cls, params)
             ^^^^^^^^^^^^^^^^^^^^
  File "/Users/alexw/.pyenv/versions/3.12.3/lib/python3.12/typing.py", line 1011, in _typevartuple_prepare_subst
    raise TypeError(f"Too few arguments for {alias};"
TypeError: Too few arguments for <class '__main__.TypeVarLikeDefaultsTests.test_typevar_and_typevartuple_specialization.<locals>.A'>; actual 1, expected at least 2

======================================================================
FAIL: test_typevartuple_specialization (__main__.TypeVarLikeDefaultsTests.test_typevartuple_specialization)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Users/alexw/dev/typing_extensions/src/test_typing_extensions.py", line 6410, in test_typevartuple_specialization
    self.assertEqual(A[float].__args__, (float, str, int))
AssertionError: Tuples differ: (<class 'float'>,) != (<class 'float'>, <class 'str'>, <class 'int'>)

Second tuple contains 2 additional elements.
First extra element 1:
<class 'str'>

- (<class 'float'>,)
+ (<class 'float'>, <class 'str'>, <class 'int'>)

----------------------------------------------------------------------
Ran 435 tests in 0.059s

FAILED (failures=1, errors=4, skipped=4)

I don't know if it's going to be possible to fix this easily in typing_extensions, or if we should try to do so before the next release.

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

Trước tiên, hãy chạy các trường hợp đã thêm trong src/test_typing_extensions.py trên Python 3.12 và so sánh các lỗi của chúng với hành vi thay thế liên quan trong typing.py của Python. Tiếp đó, hãy lần theo các đường dẫn chuyên biệt hóa của PEP-696 TypeVarTuple và ParamSpec, rồi làm cho cả năm bài kiểm thử bắt nguồn từ CPython đều vượt qua mà không gây hồi quy trong bộ kiểm thử hiện 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
developer-experience, testing
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
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.