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

How can I tell pycharm that the return type of form.save() 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 = form.save(commit=False)

            question.category=category

            question.who = request.user

            question.dat<tab>

            question.save()


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

2 comments
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 http://devnet.jetbrains.net/thread/432651?tstart=0

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

0
Comment actions Permalink

Note that in this particular case PyCharm's Django integration should have figured out the return type on its own: http://youtrack.jetbrains.com/issue/PY-7031

0

Please sign in to leave a comment.