2

I am working on some python codes in PyCharm IDE. The autocomplete feature works fine in some cases, but not the others. For example, after I import matplotlib as plt, after I type "plt.", all possible methods shows below

autocomplete works here

However, some variables do not seem to have autocomplete. If I type the methods manually, the program works fine. But the IDE just does not prompt me what methods are available to the variable. It shows something starting with "__", but not methods that I am supposed to use. For example, after I create ax1 = plt.subplot2grid((1,1), (0,0)), I can invoke tick_params method on ax1 variable. It is a valid method. However, this method does not show up in the list when I type "ax1."

autocomplete does not work

I don't think this problem is specific to PyCharm. I have run into the similar issue in other Python IDE or Python Notebook in the past. What am I missing here?

Community
  • 1
  • 1
Lan
  • 6,470
  • 3
  • 26
  • 37
  • 1
    Does this answer your question? [PyCharm - Auto Completion for matplotlib (and other imported modules)](https://stackoverflow.com/questions/36936555/pycharm-auto-completion-for-matplotlib-and-other-imported-modules) – BeastOfCaerbannog Mar 21 '20 at 14:34

1 Answers1

2

autocomplete can only help you with classes ... that is a method that you are calling to get the axes object

consider

def get_something():
    if caseA: return Something1()
    return DefaultSomething()

pycharm(or any ide) has no idea which class would be returned its either one of those or maybe nothing ... what should it give you as autocomplete?

that said you can tell pycharm it must be of type matplotlib.Axes

ax1 = plt.subplots(....)
assert isinstance(ax1,matplotlib.Axes)
ax1.   # now autocomplete works
Joran Beasley
  • 110,522
  • 12
  • 160
  • 179
  • Have exactly this case not hinting, isinstance sometimes still fail, while type hint in duplicate question works – halt9k Mar 16 '23 at 16:03