gh-137191: Fix how type parameters are collected from Protocol and Generic bases with parameters (#137281)
Some checks are pending
Tests / (push) Blocked by required conditions
Tests / Windows MSI (push) Blocked by required conditions
Tests / Change detection (push) Waiting to run
Tests / Docs (push) Blocked by required conditions
Tests / Check if Autoconf files are up to date (push) Blocked by required conditions
Tests / Check if generated files are up to date (push) Blocked by required conditions
Tests / Ubuntu SSL tests with OpenSSL (push) Blocked by required conditions
Tests / Sanitizers (push) Blocked by required conditions
Tests / Ubuntu SSL tests with AWS-LC (push) Blocked by required conditions
Tests / WASI (push) Blocked by required conditions
Tests / Hypothesis tests on Ubuntu (push) Blocked by required conditions
Tests / Address sanitizer (push) Blocked by required conditions
Tests / Cross build Linux (push) Blocked by required conditions
Tests / CIFuzz (push) Blocked by required conditions
Tests / All required checks pass (push) Blocked by required conditions
Lint / lint (push) Waiting to run
mypy / Run mypy on Lib/_pyrepl (push) Waiting to run
mypy / Run mypy on Lib/test/libregrtest (push) Waiting to run
mypy / Run mypy on Lib/tomllib (push) Waiting to run
mypy / Run mypy on Tools/build (push) Waiting to run
mypy / Run mypy on Tools/cases_generator (push) Waiting to run
mypy / Run mypy on Tools/clinic (push) Waiting to run
mypy / Run mypy on Tools/jit (push) Waiting to run
mypy / Run mypy on Tools/peg_generator (push) Waiting to run

This commit is contained in:
sobolevn 2025-08-03 10:40:55 +03:00 committed by GitHub
parent 801cf3fcdd
commit 158b28dd19
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 83 additions and 4 deletions

View file

@ -3958,6 +3958,7 @@ class ProtocolTests(BaseTestCase):
def test_defining_generic_protocols(self):
T = TypeVar('T')
T2 = TypeVar('T2')
S = TypeVar('S')
@runtime_checkable
@ -3967,17 +3968,26 @@ class ProtocolTests(BaseTestCase):
class P(PR[int, T], Protocol[T]):
y = 1
self.assertEqual(P.__parameters__, (T,))
with self.assertRaises(TypeError):
PR[int]
with self.assertRaises(TypeError):
P[int, str]
with self.assertRaisesRegex(
TypeError,
re.escape('Some type variables (~S) are not listed in Protocol[~T, ~T2]'),
):
class ExtraTypeVars(P[S], Protocol[T, T2]): ...
class C(PR[int, T]): pass
self.assertEqual(C.__parameters__, (T,))
self.assertIsInstance(C[str](), C)
def test_defining_generic_protocols_old_style(self):
T = TypeVar('T')
T2 = TypeVar('T2')
S = TypeVar('S')
@runtime_checkable
@ -3996,9 +4006,19 @@ class ProtocolTests(BaseTestCase):
class P1(Protocol, Generic[T]):
def bar(self, x: T) -> str: ...
self.assertEqual(P1.__parameters__, (T,))
class P2(Generic[T], Protocol):
def bar(self, x: T) -> str: ...
self.assertEqual(P2.__parameters__, (T,))
msg = re.escape('Some type variables (~S) are not listed in Protocol[~T, ~T2]')
with self.assertRaisesRegex(TypeError, msg):
class ExtraTypeVars(P1[S], Protocol[T, T2]): ...
with self.assertRaisesRegex(TypeError, msg):
class ExtraTypeVars(P2[S], Protocol[T, T2]): ...
@runtime_checkable
class PSub(P1[str], Protocol):
x = 1
@ -4011,6 +4031,28 @@ class ProtocolTests(BaseTestCase):
self.assertIsInstance(Test(), PSub)
def test_protocol_parameter_order(self):
# https://github.com/python/cpython/issues/137191
T1 = TypeVar("T1")
T2 = TypeVar("T2", default=object)
class A(Protocol[T1]): ...
class B0(A[T2], Generic[T1, T2]): ...
self.assertEqual(B0.__parameters__, (T1, T2))
class B1(A[T2], Protocol, Generic[T1, T2]): ...
self.assertEqual(B1.__parameters__, (T1, T2))
class B2(A[T2], Protocol[T1, T2]): ...
self.assertEqual(B2.__parameters__, (T1, T2))
class B3[T1, T2](A[T2], Protocol):
@staticmethod
def get_typeparams():
return (T1, T2)
self.assertEqual(B3.__parameters__, B3.get_typeparams())
def test_pep695_generic_protocol_callable_members(self):
@runtime_checkable
class Foo[T](Protocol):