From 769b3556d2a81231ab4b5139e021de89b9562d68 Mon Sep 17 00:00:00 2001 From: Dave Halter Date: Fri, 13 Dec 2019 01:48:45 +0100 Subject: [PATCH] Make sure warnings are not shown if a property is executed, fixes #1383 --- jedi/inference/compiled/access.py | 7 ++++++- test/test_api/test_interpreter.py | 15 +++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/jedi/inference/compiled/access.py b/jedi/inference/compiled/access.py index e65c855c..c0722e0d 100644 --- a/jedi/inference/compiled/access.py +++ b/jedi/inference/compiled/access.py @@ -4,6 +4,7 @@ import types import sys import operator as op from collections import namedtuple +import warnings from jedi._compatibility import unicode, is_py3, builtins, \ py_version, force_unicode @@ -350,7 +351,11 @@ class DirectObjectAccess(object): def getattr_paths(self, name, default=_sentinel): try: - return_obj = getattr(self._obj, name) + # Make sure no warnings are printed here, this is autocompletion, + # warnings should not be shown. See also GH #1383. + with warnings.catch_warnings(record=True): + warnings.simplefilter("always") + return_obj = getattr(self._obj, name) except Exception as e: if default is _sentinel: if isinstance(e, AttributeError): diff --git a/test/test_api/test_interpreter.py b/test/test_api/test_interpreter.py index 2161bd77..34fbf1b6 100644 --- a/test/test_api/test_interpreter.py +++ b/test/test_api/test_interpreter.py @@ -2,6 +2,7 @@ Tests of ``jedi.api.Interpreter``. """ import sys +import warnings import pytest @@ -197,6 +198,20 @@ def test_getitem_side_effects(): _assert_interpreter_complete('foo["asdf"].upper', locals(), ['upper']) +@pytest.mark.parametrize('stacklevel', [1, 2]) +@pytest.mark.filterwarnings("error") +def test_property_warnings(stacklevel): + class Foo3: + @property + def prop(self): + # Possible side effects here, should therefore not call this. + warnings.warn("foo", DeprecationWarning, stacklevel=stacklevel) + return '' + + foo = Foo3() + _assert_interpreter_complete('foo.prop.uppe', locals(), ['upper']) + + @pytest.fixture(params=[False, True]) def allow_descriptor_access_or_not(request, monkeypatch): monkeypatch.setattr(jedi.Interpreter, '_allow_descriptor_getattr_default', request.param)