Skip to content
Advertisement

Is there a way to show the IDE which Type of Objects are in a List, so it colors / autocompletes properly?

I recognize from c# that you could do

(int)(randomVariable + 1 + anythingElse)

and the IDE would understand that the named variable/result is an integer.

Now in Python I want to let my IDE know, that in the list ceDocs which I am sending as a parameter will be objects of the class Doc so I can use autocomplete and proper coloring instead of “any” everywhere.

def createDocuments(ceDocs):
    for doc in ceDocs:
        doc.name = doc.path # example action

createDocuments(getFromPath(path))

Is there a way to do this in python?

Please correct me if this makes no sense… :)

Advertisement

Answer

Use type hints with the typing module.

from typing import List

class Doc:
   # ...

def createDocuments(ceDocs: List[Doc]):
    for doc in ceDocs:
        doc.name = doc.path # example action
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement