Methods and attributes do not show when dot is pressed (intellisense)

160 views Asked by At

Yesterday I upgraded from 3.11 to python 3.12. The editor that i use is VScode.

When i use standard patterns with matplotlib (from the official docs), the intellisense does not recognise the types and therefore pressing the dot fails to expose the methods and attributes.

Here is reproducible code:

import matplotlib.pyplot as plt

fig, ax = plt.subplots()

fruits = ['apple', 'blueberry', 'cherry', 'orange']
counts = [40, 100, 30, 55]
colours = ['red', 'blue', 'red', 'orange']

ax.bar(fruits, counts, label=colours, color=colours)

ax.set_ylabel('fruit supply')
ax.set_title('Fruit supply by kind and color')
ax.legend(title='Fruit color')

plt.show()

If i explicitly typehint, then it works. But this is very clunky and seems broken.

i now have to do this:

import matplotlib.pyplot as plt
from matplotlib.figure import Figure    # added this
from matplotlib.axes import Axes        # added this

fig, ax = plt.subplots()
fig: Figure                             # added this
ax: Axes                                # added this

fruits = ['apple', 'blueberry', 'cherry', 'orange']
counts = [40, 100, 30, 55]
colours = ['red', 'blue', 'red', 'orange']

ax.bar(fruits, counts, label=colours, color=colours)

ax.set_ylabel('fruit supply')
ax.set_title('Fruit supply by kind and color')
ax.legend(title='Fruit color')

plt.show()

I have never had this issue with earlier versions of python as far as i can recall.

How to fix this such that i do not have to explicitly typehint the fig and ax ?

1

There are 1 answers

4
Erik De Bonte On

I don't see Intellisense after ax. in either version of Python. The reason is that plt.subplots in matplotlib 3.8.0 returns tuple[Figure, Any], so Pylance doesn't know the type of ax. Matplotlib 3.8.0 added inline types which Pylance trusts because the lib is py.typed.

Maybe you are using different matplotlib versions in addition to different Python versions? If I install matplotlib 3.7.3 instead of 3.8.0 (regardless of Python version), then Pylance uses its bundled matplotlib stubs and treats ax as an Axes object.