Python: Variable Type Specifier

variable: variableType = ...

The Problem

I’ve recently switched job and I was “thrown” into a large project(s) based on Python. I had to implement only a few simple functions, but since I was/am a newbie there, this task wasn’t so easy. The problem was, that we use a giant custom “library” – numerous files, classes, modules, variables, …

I use Visual Studio Code for Python, and it is great. It has IntelliSense with great autocomplete capabilities, and I can’t imagine returning back to any other IDE. Anyway, in large projects you can easily get lost and start wondering what type of variable is expected for some function/method parameter. Especially with nested classes accross many modules and imports everywhere. Since Python doesn’t have variable type specified like in C/C++, it is easy to make mistake and pass wrong parameters to functions/methods, but that is (syntactically) fine by Python, except it won’t work at the end, throwing “AttributeError: ‘MyVariables’ object has no attribute ‘missingVariable’.
Also, when using an autocomplete/recommendation functions and parser can’t figure what type of this nested variable should be, autocomplete recommendations become useless.

IntelliSense recommends everything it finds, which is useless.

 

The Solution

Introduced with PEP 484, Python above 3.5.
The idea is to help IDE with a little magic code added to variables. The syntax is simple:

variable: variableType = ...

variable = value : variableType 

where variableType can be either any of Python data types or a custom class. If a class can be found by recommendation engine, variables and methods are recommended, which is very convenient for developer. It also speeds up code writing and minimizes stupid bugs.

Example: create a class with a custom variables, and name it MyVariables. Now crate two classes that are the same and should (somewhere in this module or wherever later on) both receive variable var of type MyVariables. Take one class and specify variable type with var: MyVariables syntax and try if recommendations on internal self.variable works for both of them.

Two similar classes, but one with specified parameter

Of course not. In first class TypeNotSpecified,  the only thing you can do when writing to self.variable is by forcing recommendation engine to display everything it knows about var, which is not really usefull.

Forced recommendations are not usefull.

In second class WithTypeSpecified recommendation engine can parse variable type and can recommend internal MyVariables variables.

If type is specified, recommendations can be usefull.

Awesome.

An alternative is to add this magic to variable initialisation:
Specified variable type at variable declaration.

Final Note

You must remember that this magic does not define variable type, since this can’t be done in Python. You can still make mistakes and write whatever you want into this variables, Python doesn’t care. This is just a helping mechanism for your recommendation engine.

 

UPDATE!
Here is how to structure Python application and use this syntax to get the best out of IDE. 

Leave a Reply