Skip to content

Commit 8666418

Browse files
Fix crash in has_known_bases() for a non-class metaclass
`class C(metaclass=sum)` makes the type of C the `sum` function, and has_known_bases() read `klass.bases` on it, so a binary operation such as `C | None` crashed inference. A node that is not a ClassDef has no bases, so return False and let the callers raise _NonDeducibleTypeHierarchy. Closes #3191
1 parent 5952c15 commit 8666418

3 files changed

Lines changed: 40 additions & 0 deletions

File tree

‎ChangeLog‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,13 @@ Release date: TBA
3434

3535
Closes #3190
3636

37+
* Fix a crash when a binary operation involves a class whose metaclass is not a
38+
class, as in ``class C(metaclass=sum)`` followed by ``C | None``. The type of
39+
such a class is a function, which has no bases, so ``has_known_bases()`` now
40+
returns ``False`` for anything that is not a class.
41+
42+
Closes #3191
43+
3744
* The documentation build now fails on any Sphinx warning, so a cross-reference
3845
that does not resolve is caught by CI instead of quietly rendering as plain
3946
text. ``Uninferable``, ``Position`` and the ``BadOperationMessage`` classes

‎astroid/helpers.py‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,11 @@ def class_or_tuple_to_container(
187187

188188
def has_known_bases(klass, context: InferenceContext | None = None) -> bool:
189189
"""Return whether all base classes of a class could be inferred."""
190+
if not isinstance(klass, scoped_nodes.ClassDef):
191+
# Not a class, so it has no bases at all. This happens for the type of a
192+
# class whose metaclass is not a class either, as in
193+
# ``class C(metaclass=sum)``, whose type is the ``sum`` function.
194+
return False
190195
try:
191196
return klass._all_bases_known
192197
except AttributeError:

‎tests/test_helpers.py‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,34 @@ class A(type): #@
238238
self.assertTrue(helpers.is_supertype(builtin_type, cls_a))
239239
self.assertTrue(helpers.is_subtype(cls_a, builtin_type))
240240

241+
def test_is_subtype_supertype_function_metaclass(self) -> None:
242+
"""A metaclass that is a function makes the type of the class a function.
243+
244+
Regression test for https://github.com/pylint-dev/astroid/issues/3191
245+
"""
246+
cls_a = builder.extract_node("""
247+
class A(metaclass=sum): #@
248+
pass
249+
""")
250+
cls_a_type = helpers.object_type(cls_a)
251+
self.assertIsInstance(cls_a_type, nodes.FunctionDef)
252+
builtin_type = self._extract("type")
253+
# A function has no bases, so the hierarchy cannot be deduced.
254+
with self.assertRaises(_NonDeducibleTypeHierarchy):
255+
helpers.is_subtype(cls_a_type, builtin_type)
256+
with self.assertRaises(_NonDeducibleTypeHierarchy):
257+
helpers.is_supertype(cls_a_type, builtin_type)
258+
259+
def test_binop_with_function_metaclass_no_crash(self) -> None:
260+
"""Regression test for https://github.com/pylint-dev/astroid/issues/3191"""
261+
node = builder.extract_node("""
262+
class A(metaclass=sum):
263+
pass
264+
A | None #@
265+
""")
266+
# Inference must not raise ``AttributeError``.
267+
self.assertIs(next(node.infer()), util.Uninferable)
268+
241269

242270
def test_uninferable_for_safe_infer() -> None:
243271
uninfer = util.Uninferable

0 commit comments

Comments
 (0)