Python docstring type annotation — a class, not an instance?

前端 未结 5 1119
闹比i
闹比i 2021-01-18 01:23

Let\'s say I have:

class A(object):
   pass

class B(A):
   pass

I want to declare a function that takes a subclass of A as an argument:

5条回答
  •  一向
    一向 (楼主)
    2021-01-18 01:49

    Guido answered this question here, but I believe PyCharm does not correctly support the appropriate syntax in Python 2. I believe the syntax should be (...) -> A in PyCharm with Python 2. In Python 3, the appropriate syntax is Callable[..., A].

    I note that PyCharm doesn't treat () -> A as a class either; if you call a classmethod on A using this syntax, PyCharm inspection will flag that it can't find the referenced classmethod.

    This has been filed in the JetBrains bugtracker, though was closed based on earlier comments. Given Guido's recent comments in the first reference, I'm hoping JetBrains will reopen.

提交回复
热议问题