8000 gh-104600: Make type.__type_params__ writable by JelleZijlstra · Pull Request #104634 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-104600: Make type.__type_params__ writable #104634

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
May 19, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add tests for generic namedtuples and typeddicts
  • Loading branch information
JelleZijlstra committed May 18, 2023
commit 9b982c4d943a8b326b47b7a94562300bda3d770c
24 changes: 24 additions & 0 deletions Lib/test/test_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -6795,6 +6795,16 @@ class Y(Generic[T], NamedTuple):
with self.assertRaises(TypeError):
G[int, str]

def test_generic_pep695(self):
class X[T](NamedTuple):
x: T
T, = X.__type_params__
self.assertIsInstance(T, TypeVar)
self.assertEqual(T.__name__, 'T')
self.assertEqual(X.__bases__, (tuple, Generic))
self.assertEqual(X.__orig_bases__, (NamedTuple, Generic[T]))
self.assertEqual(X.__mro__, (X, tuple, Generic, object))

def test_non_generic_subscript(self):
# For backward compatibility, subscription works
# on arbitrary NamedTuple types.
Expand Down Expand Up @@ -7205,6 +7215,20 @@ class FooBarGeneric(BarGeneric[int]):
{'a': typing.Optional[T], 'b': int, 'c': str}
)

def test_pep695_generic_typeddict(self):
class A[T](TypedDict):
a: T

T, = A.__type_params__
self.assertIsInstance(T, TypeVar)
self.assertEqual(T.__name__, 'T')
self.assertEqual(A.__bases__, (Generic, dict))
self.assertEqual(A.__orig_bases__, (TypedDict, Generic[T]))
self.assertEqual(A.__mro__, (A, Generic, dict, object))
self.assertEqual(A.__parameters__, (T,))
self.assertEqual(A[str].__parameters__, ())
self.assertEqual(A[str].__args__, (str,))

def test_generic_inheritance(self):
class A(TypedDict, Generic[T]):
a: T
Expand Down
0