Sometimes I write projects and don't return to them until months later. Unfortunately for me I forget what was intended to be passed into a function. I would like to be able to hover over an argument and see the type such as integer, string, some class, etc. Is there an IDE out there that will do this for me? Any help is appreciated.
There is no way to infer the type normally, so no IDE will be able to do this. Why not just use docstrings?
def foo(a, b):
"""
Take your arguments back, I don't want them!
a -- int
b -- str
"""
return a, b
In Python 3 you could also take advantage of function annotations:
def foo(a: int, b: str):
"""Take your arguments back, I don't want them!"""
return a, b
In Python, it is not possible to generically infer the types of variables without actually running the code and seeing what they end up referencing. And even then, much Python code works on many different types, so there really is no true unique type of a variable.
For example, the any
builtin function iterates over its argument and returns True
if any of its elements are true. Sounds like a function from list of bool to bool? Wrong. All it needs is something that can be iterated. This includes lists, tuples, dictionaries, sets, generators and more builtin types, not to mention user-defined classes that implement the iterator protocol. So there's no way you can even infer that because any
is called on something that it's a list. And any python value (I believe) can be evaluated as true/false in boolean context, so the contents of the sequence don't even have to be bools.
Most code doesn't use the full "dynamism" of Python, and is only expected to put values of a single type in each variable. So theoretically, you could use some annotations and heuristics to do type inference some of the time. I am not aware of any IDE that has attempted to do this, and I don't believe it's actually practical.
Fundamentally, you need documentation on the way your functions/classes should be called and what kinds of things they return. If you lack this knowledge, you will make mistakes whether you have type information or not.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With