How to hint type (return value of a library)?

How can I tell pycharm that the return type of is, in this case, a  CategoryQuestion object?

def category_ask(request, pk):

    category = models.Category.objects.get(pk=pk)

    if request.method == 'POST':

        form = CategoryQuestion_form(request.POST)

        if form.is_valid():

            question =


            question.who = request.user



When pycharm gets the type, it is wonderful.  But so many cases like this pycharm has no clue!

Comment actions Permalink

I found a way.  Use

assert isinstance(question, models.CategoryQuestion)

Pycharm will now know what question is an instance of CategoryQuestion, and offer competion.

See also

But don't expect the same for templates.  Where you most want the type hinting (in the templates) you get none.

Comment actions Permalink

Note that in this particular case PyCharm's Django integration should have figured out the return type on its own:


Please sign in to leave a comment.