Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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.

like image 335
VanillaSpinIce Avatar asked Nov 06 '25 14:11

VanillaSpinIce


1 Answers

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.

like image 155
Oleh Rybalchenko Avatar answered Nov 09 '25 04:11

Oleh Rybalchenko



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!