Commit bb7c57c6 authored by Guido van Rossum's avatar Guido van Rossum

Issue #25472: In B[<type>], insert B in front of __bases__, to make the __dict__ descriptor work.

parent 6efc7e72
from collections import namedtuple from collections import namedtuple
import pickle
import re import re
import sys import sys
from unittest import TestCase, main from unittest import TestCase, main
...@@ -583,6 +584,35 @@ class GenericTests(TestCase): ...@@ -583,6 +584,35 @@ class GenericTests(TestCase):
self.assertEqual(repr(MySimpleMapping), self.assertEqual(repr(MySimpleMapping),
__name__ + '.' + 'MySimpleMapping[~XK, ~XV]') __name__ + '.' + 'MySimpleMapping[~XK, ~XV]')
def test_dict(self):
T = TypeVar('T')
class B(Generic[T]):
pass
b = B()
b.foo = 42
self.assertEqual(b.__dict__, {'foo': 42})
class C(B[int]):
pass
c = C()
c.bar = 'abc'
self.assertEqual(c.__dict__, {'bar': 'abc'})
def test_pickle(self):
T = TypeVar('T')
class B(Generic[T]):
pass
global C # pickle wants to reference the class by name
class C(B[int]):
pass
c = C()
c.foo = 42
c.bar = 'abc'
z = pickle.dumps(c)
x = pickle.loads(z)
self.assertEqual(x.foo, 42)
self.assertEqual(x.bar, 'abc')
self.assertEqual(x.__dict__, {'foo': 42, 'bar': 'abc'})
def test_errors(self): def test_errors(self):
with self.assertRaises(TypeError): with self.assertRaises(TypeError):
B = SimpleMapping[XK, Any] B = SimpleMapping[XK, Any]
......
...@@ -981,7 +981,7 @@ class GenericMeta(TypingMeta, abc.ABCMeta): ...@@ -981,7 +981,7 @@ class GenericMeta(TypingMeta, abc.ABCMeta):
"Cannot substitute %s for %s in %s" % "Cannot substitute %s for %s in %s" %
(_type_repr(new), _type_repr(old), self)) (_type_repr(new), _type_repr(old), self))
return self.__class__(self.__name__, self.__bases__, return self.__class__(self.__name__, (self,) + self.__bases__,
dict(self.__dict__), dict(self.__dict__),
parameters=params, parameters=params,
origin=self, origin=self,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment