Type hinting and annotations for lambdas, methods, and the like & circular imports

I've tried to ask this on StackOverflow & got voted down & closed so, I'll try here.

PyCharm's inspections find many, many more defects when all of the types are annotated.

How do I annotate the operator example?

class Class1:
    def add(self, x: int, y: int):
        return x + y

    def manipulate(self, operator, x: int, y: int):
        return operator(x,y)

Using type() to print() the type of operator doesn't help, functions are "function", but pycharm reports that as a syntax error, ditto for "method"

Also, I have two files, A, imports, B, B import C, C has a reference to a parent object defined in A. If I reference it in an annotation– else no intellisense and no inspection help, PyCharm then says syntax error until I add an import, but now if I run the code, I get the circular import runtime error.  Fully qualified class names in the form of """:type: package.SomeType""" don't help either.
1 comment
Comment actions Permalink

Try this https://www.jetbrains.com/pycharm/help/type-hinting-in-pycharm.html

(Foo, Bar) -> Baz Function of Foo and Bar that returns Baz

Please sign in to leave a comment.