Skip to content

Commit b0f6171

Browse files
committed
Allow arbitrary overriding of __new__
This partially addresses #794.
1 parent 6f9ca77 commit b0f6171

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

mypy/checker.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -761,8 +761,8 @@ def check_method_or_accessor_override_for_base(self, defn: FuncBase,
761761
"""Check if method definition is compatible with a base class."""
762762
if base:
763763
name = defn.name()
764-
if name != '__init__':
765-
# Check method override (__init__ is special).
764+
if name not in ('__init__', '__new__'):
765+
# Check method override (__init__ and __new__ are special).
766766
self.check_method_override_for_base_with_name(defn, name, base)
767767
if name in nodes.inplace_operator_methods:
768768
# Figure out the name of the corresponding operator method.

mypy/test/data/check-classes.test

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,15 @@ main: note: In class "B":
296296
main:6: error: Return type of "f" incompatible with supertype "A"
297297
main:7: error: Return type of "g" incompatible with supertype "A"
298298

299+
[case testOverride__new__WithDifferentSignature]
300+
class A:
301+
def __new__(cls, x: int) -> str:
302+
return ''
303+
304+
class B(A):
305+
def __new__(cls) -> int:
306+
return 1
307+
299308

300309
-- Constructors
301310
-- ------------

0 commit comments

Comments
 (0)