From a4a610490836e79babc0675f8f4763497e85b5d3 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Sun, 16 Apr 2023 19:27:26 +0000 Subject: [PATCH] Suppress UserWarning when finding module specs (#2121) (#2122) Found when linting this code: ``` import setuptools import pip ``` (cherry picked from commit 6723e635e54e991a4304e45293308f5076b0bcb8) Co-authored-by: Jacob Walls --- ChangeLog | 4 ++++ astroid/interpreter/_import/spec.py | 5 ++++- tests/test_manager.py | 10 ++++++++++ 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/ChangeLog b/ChangeLog index a9b26f1386..9179420f60 100644 --- a/ChangeLog +++ b/ChangeLog @@ -16,6 +16,10 @@ Release date: TBA Closes #1735 +* Suppress ``UserWarning`` when finding module specs. + + Closes pylint-dev/pylint#7906 + What's New in astroid 2.15.2? ============================= diff --git a/astroid/interpreter/_import/spec.py b/astroid/interpreter/_import/spec.py index f000468e92..5a7c4e822a 100644 --- a/astroid/interpreter/_import/spec.py +++ b/astroid/interpreter/_import/spec.py @@ -13,6 +13,7 @@ import pathlib import sys import types +import warnings import zipimport from collections.abc import Iterator, Sequence from pathlib import Path @@ -147,7 +148,9 @@ def find_module( ) else: try: - spec = importlib.util.find_spec(modname) + with warnings.catch_warnings(): + warnings.filterwarnings("ignore", category=UserWarning) + spec = importlib.util.find_spec(modname) if ( spec and spec.loader # type: ignore[comparison-overlap] # noqa: E501 diff --git a/tests/test_manager.py b/tests/test_manager.py index 9b9b24fe59..19efc730cd 100644 --- a/tests/test_manager.py +++ b/tests/test_manager.py @@ -7,6 +7,7 @@ import sys import time import unittest +import warnings from collections.abc import Iterator from contextlib import contextmanager from unittest import mock @@ -383,6 +384,15 @@ def test_raises_exception_for_empty_modname(self) -> None: self.manager.ast_from_module_name(None) +class IsolatedAstroidManagerTest(resources.AstroidCacheSetupMixin, unittest.TestCase): + def test_no_user_warning(self): + mgr = manager.AstroidManager() + with warnings.catch_warnings(): + warnings.filterwarnings("error", category=UserWarning) + mgr.ast_from_module_name("setuptools") + mgr.ast_from_module_name("pip") + + class BorgAstroidManagerTC(unittest.TestCase): def test_borg(self) -> None: """Test that the AstroidManager is really a borg, i.e. that two different