From 95403ff6f0cd8c016422cd07a75d1efbe869be19 Mon Sep 17 00:00:00 2001 From: Jacob Walls Date: Sat, 7 May 2022 14:38:07 -0400 Subject: [PATCH] Fix crash while obtaining `object_type()` of an `Unknown` node (#1547) --- ChangeLog | 2 ++ astroid/helpers.py | 2 ++ tests/unittest_helpers.py | 8 ++++++++ 3 files changed, 12 insertions(+) diff --git a/ChangeLog b/ChangeLog index 1e044a984..35f3bf32b 100644 --- a/ChangeLog +++ b/ChangeLog @@ -28,7 +28,9 @@ What's New in astroid 2.11.5? ============================= Release date: TBA +* Fix crash while obtaining ``object_type()`` of an ``Unknown`` node. + Refs PyCQA/pylint#6539 What's New in astroid 2.11.4? diff --git a/astroid/helpers.py b/astroid/helpers.py index 527ac1f18..8462f87db 100644 --- a/astroid/helpers.py +++ b/astroid/helpers.py @@ -55,6 +55,8 @@ def _object_type(node, context=None): yield _function_type(inferred, builtins) elif isinstance(inferred, scoped_nodes.Module): yield _build_proxy_class("module", builtins) + elif isinstance(inferred, nodes.Unknown): + raise InferenceError else: yield inferred._proxied diff --git a/tests/unittest_helpers.py b/tests/unittest_helpers.py index 086976d21..e0da009aa 100644 --- a/tests/unittest_helpers.py +++ b/tests/unittest_helpers.py @@ -5,7 +5,10 @@ import builtins import unittest +import pytest + from astroid import builder, helpers, manager, nodes, raw_building, util +from astroid.const import IS_PYPY from astroid.exceptions import _NonDeducibleTypeHierarchy from astroid.nodes.scoped_nodes import ClassDef @@ -144,6 +147,11 @@ def test_inference_errors(self) -> None: ) self.assertEqual(helpers.object_type(node), util.Uninferable) + @pytest.mark.skipif(IS_PYPY, reason="__code__ will not be Unknown on PyPy") + def test_inference_errors_2(self) -> None: + node = builder.extract_node("type(float.__new__.__code__)") + self.assertIs(helpers.object_type(node), util.Uninferable) + def test_object_type_too_many_types(self) -> None: node = builder.extract_node( """