A
A
AlexBin2019-06-25 19:25:38
Python
AlexBin, 2019-06-25 19:25:38

How to do Type Hinting of classes in PyCharm?

I didn’t figure out how to type-hint in PyCharm (python2) for a function that takes a class or subclasses of this class as an argument. Class instances are hinted great, but how do you hint the classes themselves?
Tried doing it like this:

class Base(object):
    field = 0
    
class Sub1(Base):
    field1 = 1

class Sub2(Base):
    field2 = 2

class NotSubclass(object):
    field3 = 3

def some_func(SomeClass):
    # type: (T <= Base) -> None
    print SomeClass # если тут сделать "SomeClass." (поставить точку), 
    # то выпадающее меню после точки не предлагает список атрибутов нужных классов

In addition, if you call a function and pass it clearly not a class, but for example a number, then pycharm does not highlight the error. Although when hinting everything else, it highlights.
Either I misunderstood the documentation and googled it badly, or PyCharm is not yet able to hint classes.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Andy_U, 2019-06-25
@AlexBin

This is how it should be:
5d127782c5282869171049.png

B
bbkmzzzz, 2019-06-25
@bbkmzzzz

if I understand correctly what you mean. Here you can also read

def some_func(SomeClass):
        """

        :type SomeClass: Base
        """

docks You can install the Jedi
module (if not installed), description on habré

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question