[Python-checkins] Fix a bug in Generic.__new__ (GH-6758)

Miss Islington (bot) webhook-mailer at python.org
Thu May 10 23:30:58 EDT 2018


https://github.com/python/cpython/commit/c5444b355695755035d4a5cadc0467e850bb04c2
commit: c5444b355695755035d4a5cadc0467e850bb04c2
branch: 3.7
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: GitHub <noreply at github.com>
date: 2018-05-10T20:30:47-07:00
summary:

Fix a bug in Generic.__new__ (GH-6758)

(cherry picked from commit b551e9f0ff25018a5606465003e2c51c04f693a3)

Co-authored-by: Ivan Levkivskyi <levkivskyi at gmail.com>

files:
M Lib/test/test_typing.py
M Lib/typing.py

diff --git a/Lib/test/test_typing.py b/Lib/test/test_typing.py
index 46bab5eba6db..314716cd7de3 100644
--- a/Lib/test/test_typing.py
+++ b/Lib/test/test_typing.py
@@ -1367,6 +1367,9 @@ def test_new_no_args(self):
         class A(Generic[T]):
             pass
 
+        with self.assertRaises(TypeError):
+            A('foo')
+
         class B:
             def __new__(cls):
                 # call object
diff --git a/Lib/typing.py b/Lib/typing.py
index 89b73db15837..8025dfd93262 100644
--- a/Lib/typing.py
+++ b/Lib/typing.py
@@ -836,7 +836,7 @@ def __new__(cls, *args, **kwds):
         if cls is Generic:
             raise TypeError("Type Generic cannot be instantiated; "
                             "it can be used only as a base class")
-        if super().__new__ is object.__new__:
+        if super().__new__ is object.__new__ and cls.__init__ is not object.__init__:
             obj = super().__new__(cls)
         else:
             obj = super().__new__(cls, *args, **kwds)



More information about the Python-checkins mailing list