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

前端 未结 5 1168
闹比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:48

    There is no way to do this using the docstring syntax for Python 2 in PyCharm or IDEA.

    However, you can get code completion for the variable by placing an assert statement above the usage:

    def call_foo(cls):
      """
      Calls the class method ``foo`` of the given class.
    
      :param cls: a subclass of SomeClass
      :type cls: type
      """
      assert issubclass(cls, SomeClass)
      cls.foo()  # you'll get code completion here for the foo method defined in SomeClass
    

提交回复
热议问题