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

Conversation

Projects
None yet
2 participants
@euresti
Contributor

euresti commented Feb 5, 2018

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

Simple type checking of class decorators
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 #3135
@ilevkivskyi

Thanks! Just one small comment.

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

This comment has been minimized.

@ilevkivskyi

ilevkivskyi Feb 5, 2018

Collaborator

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.

@@ -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]"

This comment has been minimized.

@ilevkivskyi

ilevkivskyi Feb 5, 2018

Collaborator

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

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

euresti added some commits Feb 6, 2018

@euresti euresti changed the title from Simple type checking of class decorators to Type checking of class decorators Feb 6, 2018

@ilevkivskyi

Few more comments.

# Decorators are applied in reverse order.
for decorator in reversed(defn.decorators):
if (isinstance(decorator, CallExpr)
and isinstance(decorator.analyzed, PromoteExpr)):

This comment has been minimized.

@ilevkivskyi

ilevkivskyi Feb 7, 2018

Collaborator

This exception deserves a comment.

This comment has been minimized.

@euresti

euresti Feb 7, 2018

Contributor

Done!

if isinstance(decorator, RefExpr):
fullname = decorator.fullname
sig, t2 = self.expr_checker.check_call(dec, [temp],

This comment has been minimized.

@ilevkivskyi

ilevkivskyi Feb 7, 2018

Collaborator

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:

This comment has been minimized.

@ilevkivskyi

ilevkivskyi Feb 7, 2018

Collaborator

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.

This comment has been minimized.

@euresti

euresti Feb 7, 2018

Contributor

Added below

class A3: pass
not_a_function = 17
@not_a_function() # E: "int" not callable

This comment has been minimized.

@ilevkivskyi

ilevkivskyi Feb 7, 2018

Collaborator

Also add a test for directly applying not_a_function:

@not_a_function
class C: ...

This comment has been minimized.

@euresti

euresti Feb 7, 2018

Contributor

Done

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

This comment has been minimized.

@ilevkivskyi

ilevkivskyi Feb 7, 2018

Collaborator

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.

This comment has been minimized.

@euresti

euresti Feb 7, 2018

Contributor

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.

@euresti

This comment has been minimized.

Contributor

euresti commented Feb 7, 2018

Done!

@ilevkivskyi ilevkivskyi merged commit 46ecea5 into python:master Feb 9, 2018

2 checks passed

continuous-integration/appveyor/pr AppVeyor build succeeded
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details

carljm added a commit to carljm/mypy that referenced this pull request Feb 14, 2018

Merge branch 'master' into import-cycle
* 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)
  ...

@euresti euresti deleted the euresti:check_class_decorators branch Feb 23, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment