-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
Conversation
This does the bare minimum, it checks that the calls are well-type. It does not check whether applying the decorator makes sense. Helps with python#3135
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! Just one small comment.
test-data/unit/check-classes.test
Outdated
|
||
@decorate(y=17) # E: Unexpected keyword argument "y" for "decorate" | ||
class A: | ||
pass |
There was a problem hiding this comment.
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.
test-data/unit/check-classes.test
Outdated
@@ -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]" |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Few more comments.
# Decorators are applied in reverse order. | ||
for decorator in reversed(defn.decorators): | ||
if (isinstance(decorator, CallExpr) | ||
and isinstance(decorator.analyzed, PromoteExpr)): |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done!
mypy/checker.py
Outdated
if isinstance(decorator, RefExpr): | ||
fullname = decorator.fullname | ||
|
||
sig, t2 = self.expr_checker.check_call(dec, [temp], |
There was a problem hiding this comment.
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.
@@ -1254,6 +1254,24 @@ def visit_class_def(self, defn: ClassDef) -> None: | |||
# Otherwise we've already found errors; more errors are not useful | |||
self.check_multiple_inheritance(typ) | |||
|
|||
if defn.decorators: |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added below
class A3: pass | ||
|
||
not_a_function = 17 | ||
@not_a_function() # E: "int" not callable |
There was a problem hiding this comment.
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: ...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
|
||
[case testClassDecoratorIncorrect] | ||
def not_a_class_decorator(x: int) -> int: ... | ||
@not_a_class_decorator(7) # E: "int" not callable |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
Done! |
* master: (32 commits) Fix some fine-grained cache/fswatcher problems (python#4560) Sync typeshed (python#4559) Add _cached suffix to test cases in fine-grained tests with cache (python#4558) Add back support for simplified fine-grained logging (python#4557) Type checking of class decorators (python#4544) Sync typeshed (python#4556) When loading from a fine-grained cache, use the real path, not the cached (python#4555) Switch all of the fine-grained debug logging to use manager.log (python#4550) Caching for fine-grained incremental mode (python#4483) Fix --warn-return-any for NotImplemented (python#4545) Remove myunit (python#4369) Store line numbers of imports in the cache metadata (python#4533) README.md: Fix a typo (python#4529) Enable generation and caching of fine-grained dependencies from normal runs (python#4526) Move argument parsing for the fine-grained flag into the main arg parsing code (python#4524) Don't warn about unrecognized options starting with 'x_' (python#4522) stubgen: don't append star arg when args list already has varargs appended (python#4518) Handle TypedDict in diff and deps (python#4510) Fix Options.__repr__ to not infinite recurse (python#4514) Fix some fine-grained incremental bugs with newly imported files (python#4502) ...
It checks that the calls are well-type and that applying the decorators works.
It does not check the end result because what should I do with that?
Helps with #3135