pylint
pylint copied to clipboard
incorrect type inference after isinstance
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
type annotation like
def figrow(figsize, ncols) -> Tuple[Figure, List[Axes]]:
...
does not help
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.
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.
I mean, should this be configurable with --strict mode?
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)