Skip to content
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

Type checking of class decorators #4544

Merged
merged 5 commits into from
Feb 9, 2018
Merged
Show file tree
Hide file tree
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
5 changes: 5 additions & 0 deletions mypy/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -1254,6 +1254,11 @@ def visit_class_def(self, defn: ClassDef) -> None:
# Otherwise we've already found errors; more errors are not useful
self.check_multiple_inheritance(typ)

for decorator in defn.decorators:
# Currently this only checks that the decorator itself is well typed.
# TODO: Check that applying the decorator to the class would do the right thing.
self.expr_checker.accept(decorator)

def check_protocol_variance(self, defn: ClassDef) -> None:
"""Check that protocol definition is compatible with declared
variances of type variables.
Expand Down
11 changes: 10 additions & 1 deletion test-data/unit/check-classes.test
Original file line number Diff line number Diff line change
Expand Up @@ -4115,7 +4115,7 @@ def f() -> type: return M
class C1(six.with_metaclass(M), object): pass # E: Invalid base class
class C2(C1, six.with_metaclass(M)): pass # E: Invalid base class
class C3(six.with_metaclass(A)): pass # E: Metaclasses not inheriting from 'type' are not supported
@six.add_metaclass(A) # E: Metaclasses not inheriting from 'type' are not supported
@six.add_metaclass(A) # E: Metaclasses not inheriting from 'type' are not supported # E: Argument 1 to "add_metaclass" has incompatible type "Type[A]"; expected "Type[type]"
Copy link
Member

Choose a reason for hiding this comment

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

One more thing: Add \ somewhere here and format it nicely with a newline, like this:

failing_func(wrong) # E: Bad function \
                    # E: Bad argument

class D3(A): pass
class C4(six.with_metaclass(M), metaclass=M): pass # E: Multiple metaclass definitions
@six.add_metaclass(M) # E: Multiple metaclass definitions
Expand Down Expand Up @@ -4223,3 +4223,12 @@ class C(Any):
reveal_type(self.bar().__name__) # E: Revealed type is 'builtins.str'
[builtins fixtures/type.pyi]
[out]

[case testClassDecoratorIsTypeChecked]
from typing import Callable
def decorate(x: int) -> Callable[[type], type]: # N: "decorate" defined here
...

@decorate(y=17) # E: Unexpected keyword argument "y" for "decorate"
class A:
pass
Copy link
Member

Choose a reason for hiding this comment

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

The PR is trivial, but I would add few more test cases for various kinds of errors, like non-existing name/attribute, calling non-callable, too many args, forward reference in a decorator type, etc.