Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pycharm does not show all attributes and methods of a object

Tags:

python

opencv

When I use Python to write opencv, I have got the image object using the imread method, but when I try to use the object, I cannot see any attribute or method of the method.

Like this enter image description here

When I use iPython or use the dir() method to check, I can see it enter image description here

enter image description here

like image 205
Xavier Avatar asked Feb 01 '26 02:02

Xavier


1 Answers

This happens when PyCharm can't guess the type of the object returned by method - imread() in this case. Some methods return different types of object based on the input. You'd have to take a look into opencv source code to see why it isn't clear what the returned type is. Static analysis of the code detects obvious cases.
IPython has already executed the method, so it's clear what type was returned.
One solution, if you know the type returned, is to use type comments like this:

import cv2
import numpy
# I've checked with IPython that the returned object is a `numpy.ndarray` instance

img = cv2.imread('/home/me/Pictures/image.jpg')  # type: numpy.ndarray

And then if you type img. you will see enter image description here

The feauture is described on PEP 0484.
This PEP says it's introduced in Python 3.5. However it might be that PyCharm could handle this simple case in older Python versions than 3.5 but I haven't check.
This PEP describes features of typing module which is not available in older Python versions, so most of the features from this document won't work but I'm not sure if PyCharm is really using typing module to parse type comments or does it natively.

like image 57
ElmoVanKielmo Avatar answered Feb 03 '26 16:02

ElmoVanKielmo



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!