From be9701c324763c515347883b82f47418e6bde984 Mon Sep 17 00:00:00 2001 From: Zen Lee Date: Sat, 8 Nov 2025 23:54:17 +0800 Subject: [PATCH 1/2] Fix crash when a slice object is called --- doc/whatsnew/fragments/10708.bugfix | 4 ++++ pylint/extensions/code_style.py | 2 +- .../functional/ext/code_style/cs_prefer_typing_namedtuple.py | 5 +++++ tests/functional/ext/code_style/cs_use_math_not_float.py | 5 +++++ 4 files changed, 15 insertions(+), 1 deletion(-) create mode 100644 doc/whatsnew/fragments/10708.bugfix diff --git a/doc/whatsnew/fragments/10708.bugfix b/doc/whatsnew/fragments/10708.bugfix new file mode 100644 index 0000000000..2a9b1a3793 --- /dev/null +++ b/doc/whatsnew/fragments/10708.bugfix @@ -0,0 +1,4 @@ +Fix crash for ``prefer-typing-namedtuple`` and ``consider-math-not-float`` when +a ``slice`` object is called. + +Closes #10708 diff --git a/pylint/extensions/code_style.py b/pylint/extensions/code_style.py index d8ea869cb9..0eff5f4786 100644 --- a/pylint/extensions/code_style.py +++ b/pylint/extensions/code_style.py @@ -113,7 +113,7 @@ def open(self) -> None: def visit_call(self, node: nodes.Call) -> None: if self._py36_plus: called = safe_infer(node.func) - if not called: + if not (called and isinstance(called, (nodes.FunctionDef, nodes.ClassDef))): return if called.qname() == "collections.namedtuple": self.add_message( diff --git a/tests/functional/ext/code_style/cs_prefer_typing_namedtuple.py b/tests/functional/ext/code_style/cs_prefer_typing_namedtuple.py index 7b0e7c58de..18ca3138f8 100644 --- a/tests/functional/ext/code_style/cs_prefer_typing_namedtuple.py +++ b/tests/functional/ext/code_style/cs_prefer_typing_namedtuple.py @@ -7,3 +7,8 @@ class SearchMatch( namedtuple('SearchMatch', ['els', 'index', 'iterator']) # [prefer-typing-namedtuple] ): """Adapted from primer package `music21`.""" + + +# Regression test for https://github.com/pylint-dev/pylint/issues/10708 +x = slice(42) +x() # pylint: disable=not-callable diff --git a/tests/functional/ext/code_style/cs_use_math_not_float.py b/tests/functional/ext/code_style/cs_use_math_not_float.py index e8ac8ab302..b8012e9df7 100644 --- a/tests/functional/ext/code_style/cs_use_math_not_float.py +++ b/tests/functional/ext/code_style/cs_use_math_not_float.py @@ -16,3 +16,8 @@ upper_nan_float = float("NaN") # [consider-math-not-float] typo_nan_float = float("nani") # [consider-math-not-float] other_typo_nan_float = float("nna") # [consider-math-not-float] + + +# Regression test for https://github.com/pylint-dev/pylint/issues/10708 +x = slice(42) +x() # pylint: disable=not-callable From d9988c0ad8357016835f58b1040038f11a869ff6 Mon Sep 17 00:00:00 2001 From: Pierre Sassoulas Date: Sun, 9 Nov 2025 19:51:02 +0100 Subject: [PATCH 2/2] Update tests/functional/ext/code_style/cs_use_math_not_float.py --- tests/functional/ext/code_style/cs_use_math_not_float.py | 5 ----- 1 file changed, 5 deletions(-) diff --git a/tests/functional/ext/code_style/cs_use_math_not_float.py b/tests/functional/ext/code_style/cs_use_math_not_float.py index b8012e9df7..e8ac8ab302 100644 --- a/tests/functional/ext/code_style/cs_use_math_not_float.py +++ b/tests/functional/ext/code_style/cs_use_math_not_float.py @@ -16,8 +16,3 @@ upper_nan_float = float("NaN") # [consider-math-not-float] typo_nan_float = float("nani") # [consider-math-not-float] other_typo_nan_float = float("nna") # [consider-math-not-float] - - -# Regression test for https://github.com/pylint-dev/pylint/issues/10708 -x = slice(42) -x() # pylint: disable=not-callable