|
40 | 40 | from mypy.sametypes import is_same_type, is_same_types
|
41 | 41 | from mypy.messages import MessageBuilder, make_inferred_type_note
|
42 | 42 | import mypy.checkexpr
|
43 |
| -from mypy.checkmember import map_type_from_supertype, bind_self, erase_to_bound |
| 43 | +from mypy.checkmember import map_type_from_supertype, bind_self, erase_to_bound, type_object_type |
44 | 44 | from mypy import messages
|
45 | 45 | from mypy.subtypes import (
|
46 | 46 | is_subtype, is_equivalent, is_proper_subtype, is_more_precise,
|
@@ -1255,6 +1255,29 @@ def visit_class_def(self, defn: ClassDef) -> None:
|
1255 | 1255 | # Otherwise we've already found errors; more errors are not useful
|
1256 | 1256 | self.check_multiple_inheritance(typ)
|
1257 | 1257 |
|
| 1258 | + if defn.decorators: |
| 1259 | + sig = type_object_type(defn.info, self.named_type) |
| 1260 | + # Decorators are applied in reverse order. |
| 1261 | + for decorator in reversed(defn.decorators): |
| 1262 | + if (isinstance(decorator, CallExpr) |
| 1263 | + and isinstance(decorator.analyzed, PromoteExpr)): |
| 1264 | + # _promote is a special type checking related construct. |
| 1265 | + continue |
| 1266 | + |
| 1267 | + dec = self.expr_checker.accept(decorator) |
| 1268 | + temp = self.temp_node(sig) |
| 1269 | + fullname = None |
| 1270 | + if isinstance(decorator, RefExpr): |
| 1271 | + fullname = decorator.fullname |
| 1272 | + |
| 1273 | + # TODO: Figure out how to have clearer error messages. |
| 1274 | + # (e.g. "class decorator must be a function that accepts a type." |
| 1275 | + sig, _ = self.expr_checker.check_call(dec, [temp], |
| 1276 | + [nodes.ARG_POS], defn, |
| 1277 | + callable_name=fullname) |
| 1278 | + # TODO: Apply the sig to the actual TypeInfo so we can handle decorators |
| 1279 | + # that completely swap out the type. (e.g. Callable[[Type[A]], Type[B]]) |
| 1280 | + |
1258 | 1281 | def check_protocol_variance(self, defn: ClassDef) -> None:
|
1259 | 1282 | """Check that protocol definition is compatible with declared
|
1260 | 1283 | variances of type variables.
|
|
0 commit comments