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

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

    According to the pycharm docs as close as you can get is:

    () -> SomeClass
    

    So in your example

    def do_something(klass):
       """
       :type klass: () -> A
       """
       pass
    

    This means (for PyCharm) that the argument you are providing is a function that returns an object of a given type. It will properly type hint anything after the object creation.

提交回复
热议问题