Skip to content

Fixes #1727: constructor after classmethod counts #5501

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

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
17 changes: 0 additions & 17 deletions mypy/plugins/dataclasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,23 +88,6 @@ def transform(self) -> None:
args=[attr.to_argument(info) for attr in attributes if attr.is_in_init],
return_type=NoneTyp(),
)
for stmt in self._ctx.cls.defs.body:
# Fix up the types of classmethods since, by default,
# they will be based on the parent class' init.
if isinstance(stmt, Decorator) and stmt.func.is_class:
func_type = stmt.func.type
if isinstance(func_type, CallableType):
func_type.arg_types[0] = self._ctx.api.class_type(self._ctx.cls.info)
if isinstance(stmt, OverloadedFuncDef) and stmt.is_class:
func_type = stmt.type
if isinstance(func_type, Overloaded):
class_type = ctx.api.class_type(ctx.cls.info)
for item in func_type.items():
item.arg_types[0] = class_type
if stmt.impl is not None:
assert isinstance(stmt.impl, Decorator)
if isinstance(stmt.impl.func.type, CallableType):
stmt.impl.func.type.arg_types[0] = class_type

# Add an eq method, but only if the class doesn't already have one.
if decorator_arguments['eq'] and info.get('__eq__') is None:
Expand Down
20 changes: 18 additions & 2 deletions mypy/semanal.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,10 @@ class SemanticAnalyzerPass2(NodeVisitor[None],
# postpone_nested_functions_stack[-1] == FUNCTION_FIRST_PHASE_POSTPONE_SECOND.
postponed_functions_stack = None # type: List[List[Node]]

# Classmethod definitions that couldn't be prepared yet
# until the class analysis is complete
postponed_classmethods_stack = None # type: List[List[FuncDef]]

loop_depth = 0 # Depth of breakable loops
cur_mod_id = '' # Current module id (or None) (phase 2)
is_stub_file = False # Are we analyzing a stub file?
Expand Down Expand Up @@ -246,6 +250,7 @@ def __init__(self,
self.missing_modules = missing_modules
self.postpone_nested_functions_stack = [FUNCTION_BOTH_PHASES]
self.postponed_functions_stack = []
self.postponed_classmethods_stack = []
self.all_exports = set() # type: Set[str]
self.plugin = plugin
# If True, process function definitions. If False, don't. This is used
Expand Down Expand Up @@ -456,6 +461,14 @@ def _visit_func_def(self, defn: FuncDef) -> None:
assert ret_type is not None, "Internal error: typing.Coroutine not found"
defn.type = defn.type.copy_modified(ret_type=ret_type)

def prepare_postponed_classmethods_signature(self) -> None:
for func in self.postponed_classmethods_stack[-1]:
functype = func.type
assert isinstance(functype, CallableType)
assert self.type, "Classmethod singatures preparation outside of a class"
leading_type = self.class_type(self.type)
func.type = replace_implicit_first_type(functype, leading_type)

def prepare_method_signature(self, func: FuncDef, info: TypeInfo) -> None:
"""Check basic signature validity and tweak annotation of self/cls argument."""
# Only non-static methods are special.
Expand All @@ -467,10 +480,10 @@ def prepare_method_signature(self, func: FuncDef, info: TypeInfo) -> None:
self_type = functype.arg_types[0]
if isinstance(self_type, AnyType):
if func.is_class or func.name() in ('__new__', '__init_subclass__'):
leading_type = self.class_type(info)
self.postponed_classmethods_stack[-1].append(func)
else:
leading_type = fill_typevars(info)
func.type = replace_implicit_first_type(functype, leading_type)
func.type = replace_implicit_first_type(functype, leading_type)

def set_original_def(self, previous: Optional[Node], new: FuncDef) -> bool:
"""If 'new' conditionally redefine 'previous', set 'previous' as original
Expand Down Expand Up @@ -834,13 +847,16 @@ def enter_class(self, info: TypeInfo) -> None:
self.locals.append(None) # Add class scope
self.block_depth.append(-1) # The class body increments this to 0
self.postpone_nested_functions_stack.append(FUNCTION_BOTH_PHASES)
self.postponed_classmethods_stack.append([])
self.type = info

def leave_class(self) -> None:
""" Restore analyzer state. """
self.postpone_nested_functions_stack.pop()
self.block_depth.pop()
self.locals.pop()
self.prepare_postponed_classmethods_signature()
self.postponed_classmethods_stack.pop()
self.type = self.type_stack.pop()

def analyze_class_decorator(self, defn: ClassDef, decorator: Expression) -> None:
Expand Down
56 changes: 56 additions & 0 deletions test-data/unit/check-classes.test
Original file line number Diff line number Diff line change
Expand Up @@ -616,6 +616,62 @@ main:3: error: Incompatible types in assignment (expression has type "B", variab
main:4: error: Incompatible types in assignment (expression has type "A", variable has type "D")
main:5: error: Incompatible types in assignment (expression has type "D2", variable has type "D")

[case testClassmethodBeforeInit]
class C:
@classmethod
def create(cls, arg: int) -> 'C':
reveal_type(cls) # E: Revealed type is 'def (arg: builtins.int) -> __main__.C'
reveal_type(cls(arg)) # E: Revealed type is '__main__.C'
cls() # E: Too few arguments for "C"
return cls(arg)

def __init__(self, arg: 'int') -> None:
self._arg: int = arg

reveal_type(C.create) # E: Revealed type is 'def (arg: builtins.int) -> __main__.C'
[builtins fixtures/classmethod.pyi]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure this test actually tests the situation enough. I think what you need to add is a reveal_type for cls and cls(<expected args>) in the method body, and as well check that cls(<unexpected args>) fails with a reasonable error.

Please be sure to include all the examples that appeared in the original issue. In addition please add at least tests that check your fix works correctly with the following:

  • Fine-grained daemon
  • Forward references
  • Generic classes
  • Overloaded methods (there are known issues, but few simple tests would help)


[case testClassmethodBeforeInitInNestedClass]
class A:
@classmethod
def create(cls, arg: int) -> 'A':
reveal_type(cls) # E: Revealed type is 'def (arg: builtins.int) -> __main__.A'
reveal_type(cls(arg)) # E: Revealed type is '__main__.A'
cls() # E: Too few arguments for "A"
return cls(arg)

class B:
@classmethod
def create(cls, arg: str) -> 'A.B':
reveal_type(cls) # E: Revealed type is 'def (arg: builtins.str) -> __main__.A.B'
reveal_type(cls(arg)) # E: Revealed type is '__main__.A.B'
cls() # E: Too few arguments for "B"
return cls(arg)

def __init__(self, arg: 'str') -> None:
self._arg: str = arg

def __init__(self, arg: 'int') -> None:
self._arg: int = arg

reveal_type(A.create) # E: Revealed type is 'def (arg: builtins.int) -> __main__.A'
reveal_type(A.B.create) # E: Revealed type is 'def (arg: builtins.str) -> __main__.A.B'
[builtins fixtures/classmethod.pyi]

[case testClassmethodBeforeInitInGenericClass]
from typing import Generic, TypeVar
T = TypeVar('T')
class C(Generic[T]):
@classmethod
def create(cls, arg: T) -> 'C':
cls() # E: Too few arguments for "C"
return cls(arg, arg)

def __init__(self, arg1: T, arg2: T) -> None:
self._arg1 = arg1
self._arg2 = arg2
[builtins fixtures/classmethod.pyi]


-- Attribute access in class body
-- ------------------------------
Expand Down
15 changes: 15 additions & 0 deletions test-data/unit/check-overloading.test
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,21 @@ class A:
def __init__(self, b: 'B') -> None: pass
class B: pass

[case testOverloadedInitAndClassMethod]
from foo import *
[file foo.pyi]
from typing import overload
class A:
@classmethod
def from_int(cls, x: int) -> 'A':
reveal_type(cls) # E: Revealed type is 'Any'
return cls(x)
@overload
def __init__(self, a: int) -> None: pass
@overload
def __init__(self, b: str) -> None: pass
[builtins fixtures/classmethod.pyi]

[case testIntersectionTypeCompatibility]
from foo import *
[file foo.pyi]
Expand Down
13 changes: 13 additions & 0 deletions test-data/unit/deps.test
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,19 @@ class A:
<m.A.g> -> m.f
<m.A> -> <m.f>, m.A, m.f

[case testCallClassMethod]
def f() -> None:
A.g()
class A:
@classmethod
def g(cls) -> None: pass
[out]
<m.A.__init__> -> m.f
<m.A.__new__> -> m.f
<m.A.g> -> m, m.f
<m.A> -> m.A, m.f
[builtins fixtures/classmethod.pyi]

[case testAccessAttribute]
def f(a: A) -> None:
a.x
Expand Down
45 changes: 45 additions & 0 deletions test-data/unit/fine-grained.test
Original file line number Diff line number Diff line change
Expand Up @@ -2143,6 +2143,51 @@ main:3: note: Possible overload variants:
main:3: note: def foo(cls: Wrapper, x: int) -> int
main:3: note: def foo(cls: Wrapper, x: str) -> str

[case testInitMovesAroundClassmethod]
[file m.py]
class C:
def __init__(self, x: int) -> None:
self._x = x
@classmethod
def create(cls, x: int) -> 'C':
return cls(x)
[file m.py.2]
class C:
@classmethod
def create(cls, x: int) -> 'C':
return cls(x)
def __init__(self, x: int) -> None:
self._x = x
[file m.py.3]
class C:
def __init__(self, x: int) -> None:
self._x = x
@classmethod
def create(cls, x: int) -> 'C':
return cls(x)
[builtins fixtures/classmethod.pyi]
[out]
==
==

[case testInitDisappearsClassmethodStays]
[file m.py]
class C:
def __init__(self, x: int) -> None:
self._x = x
@classmethod
def create(cls, x: int) -> 'C':
return cls(x)
[file m.py.2]
class C:
@classmethod
def create(cls, x: int) -> 'C':
return cls(x)
[builtins fixtures/classmethod.pyi]
[out]
==
m.py:4: error: Too many arguments for "C"

[case testRefreshGenericClass]
from typing import TypeVar, Generic
from a import A
Expand Down