pylint icon indicating copy to clipboard operation
pylint copied to clipboard

incorrect type inference after isinstance

Open sam-s opened this issue 5 months ago • 2 comments

Bug description

pylint seems to expect that the type of the 1st return statement is applicable to all subsequent returns:

"https://github.com/pylint-dev/pylint/issues/"
import matplotlib.pyplot as plt
def figrow(figsize, ncols):
    "Return figure and axes for a row with ncol columns."
    if not isinstance(figsize[0], int):
        return figsize
    return plt.subplots(figsize=(ncols*figsize[0], figsize[1]),nrows=1,ncols=ncols)
fig, axes = figrow(figsize=(4,7), ncols=3)
axes[0].plot([1,2,3],[4,5,6])
fig.tight_layout()

Command used

pylint pylint-10423.py

Pylint output

************* Module pylint-10423
pylint-10423.py:9:0: E1136: Value 'axes' is unsubscriptable (unsubscriptable-object)
pylint-10423.py:10:0: E1101: Instance of 'int' has no 'tight_layout' member (no-member)

Expected behavior

no diagnostics

Pylint version

pylint 3.3.7
astroid 3.3.10
Python 3.13.2 (tags/v3.13.2:4f8bb39, Feb  4 2025, 15:23:48) [MSC v.1942 64 bit (AMD64)]

OS / Environment

windows

sam-s avatar Jun 11 '25 17:06 sam-s

type annotation like

def figrow(figsize, ncols) -> Tuple[Figure, List[Axes]]:
    ...

does not help

sam-s avatar Jun 17 '25 12:06 sam-s

workaround:

def figrow(figsize, ncols):
    "Return figure and axes for a row with ncol columns."
    if isinstance(figsize[0], int):
        return plt.subplots(figsize=(ncols*figsize[0], figsize[1]),nrows=1,ncols=ncols)
    return figsize

Apparently, pylint infers the return type from the 1st return statement.

sam-s avatar Jun 17 '25 12:06 sam-s

Thanks, we have a major class of bugs in pylint where by habit we default to safe_infer() instead of infer_all().

cc/ @Pierre-Sassoulas @mbyrnepr2 @zenlyj for awareness during triage.

jacobtylerwalls avatar Aug 27 '25 11:08 jacobtylerwalls

I mean, should this be configurable with --strict mode?

jacobtylerwalls avatar Aug 27 '25 11:08 jacobtylerwalls

I don't think we have a strict mode in pylint. (Maybe related to adding configuration template -- discussed everywhere and in no issue in particular after 5/10mn of searching for it)

Pierre-Sassoulas avatar Aug 27 '25 13:08 Pierre-Sassoulas