8000 Type checking of class decorators by euresti · Pull Request #4544 · python/mypy · GitHub
[go: up one dir, main page]

Skip to content

Type checking of class decorators #4544

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

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
Prev Previous commit
Next Next commit
Fully decorator support
  • Loading branch information
euresti committed Feb 6, 2018
commit c900ed7d4e68e7b63a905c0f2cf7e55fc7aedd6f
23 changes: 18 additions & 5 deletions mypy/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
from mypy.sametypes import is_same_type, is_same_types
from mypy.messages import MessageBuilder, make_inferred_type_note
import mypy.checkexpr
from mypy.checkmember import map_type_from_supertype, bind_self, erase_to_bound
from mypy.checkmember import map_type_from_supertype, bind_self, erase_to_bound, type_object_type
from mypy import messages
from mypy.subtypes import (
is_subtype, is_equivalent, is_proper_subtype, is_more_precise,
Expand Down Expand Up @@ -1254,10 +1254,23 @@ def visit_class_def(self, defn: ClassDef) -> None 8000 :
# 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)
if defn.decorators:
Copy link
Member

Choose a reason for hiding this comment

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

I would leave some kind of TODO here. One of main use cases:

dec: Callable[[Type[A]], Type[B]]

@dec
class A:
    pass

def fun(arg: B) -> None:
    pass

fun(A())  # Should be OK

is still not supported.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added below

sig = type_object_type(defn.info, self.named_type)
# Decorators are applied in reverse order.
for decorator in reversed(defn.decorators):
if (isinstance(decorator, CallExpr)
and isinstance(decorator.analyzed, PromoteExpr)):
8000
Copy link
Member

Choose a reason for hiding this comment

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

This exception deserves a comment.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done!

continue

dec = self.expr_checker.accept(decorator)
temp = self.temp_node(sig)
fullname = None
if isinstance(decorator, RefExpr):
fullname = decorator.fullname

sig, t2 = self.expr_checker.check_call(dec, [temp],
Copy link
Member

Choose a reason for hiding this comment

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

Better use an underscore for throw-away variable.

[nodes.ARG_POS], defn,
callable_name=fullname)

def check_protocol_variance(self, defn: ClassDef) -> None:
"""Check that protocol definition is compatible with declared
Expand Down
13 changes: 9 additions & 4 deletions test-data/unit/check-classes.test
Original file line number Diff line number Diff line change
Expand Up @@ -4236,8 +4236,15 @@ def decorate_forward_ref() -> Callable[[Type[A]], Type[A]]:
@decorate(22, 25) # E: Too many arguments for "decorate"
@decorate_forward_ref()
@decorate(11)
class A:
pass
class A: pass

@decorate # E: Argument 1 to "decorate" has incompatible type "Type[A2]"; expected "int"
class A2: pass

[case testClassDecoratorIncorrect]
def not_a_class_decorator(x: int) -> int: ...
@not_a_class_decorator(7) # E: "int" not callable
Copy link
Member

Choose a reason for hiding this comment

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

The error message is quite obscure. I would somehow highlight that a class decorator must be a function that accepts a type. If this is too hard, then leave a TODO about this in code.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Left a TODO. Only thing I can figure out is collect all the errors with self.msg.copy() then modify all the messages to say what we want. Or check the count of errors before and after and add another one if it has increased.

class A3: pass

not_a_function = 17
@not_a_function() # E: "int" not callable
Copy link
Member

Choose a reason for hiding this comment

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

Also add a test for directly applying not_a_function:

@not_a_function
class C: ...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

Expand All @@ -4249,5 +4256,3 @@ class C: pass

@undefined # E: Name 'undefined' is not defined
class D: pass


0