VanillaSpinIce
VanillaSpinIce

Reputation: 293

How to specify the object type so it is recognized prior to running the code in Python?

Suppose I have the following code excerpt:

import pickle
with open('my_object.pkl','r') as f:
  object = pickle.load(f)

My question is:

Suppose object is from a class I defined previously, how can I specify this in the code such that my interpreter knows prior to running the code what the object class is ? My goal here is to have the auto-completion of my IDE (I use VSCode) recognize the object so I can auto-complete and easily search the methods and attributes of that object.

Upvotes: 4

Views: 234

Answers (1)

Oleh Rybalchenko
Oleh Rybalchenko

Reputation: 8039

It depends on the version of Python and IDE, but in general looks like an additional statement with assertion instance type is the only way so far. This will trigger VS autocomplete settings

import pickle
with open('my_object.pkl','r') as f:
    object = pickle.load(f)
    assert isinstance(object, YourType)
    # and now you can use autocompletion with the object

The following issue is tracking that feature: #82.

Upvotes: 3

Related Questions