Skip to content

Commit a4a6104

Browse files
Suppress UserWarning when finding module specs (#2121) (#2122)
Found when linting this code: ``` import setuptools import pip ``` (cherry picked from commit 6723e63) Co-authored-by: Jacob Walls <[email protected]>
1 parent b65f191 commit a4a6104

File tree

3 files changed

+18
-1
lines changed

3 files changed

+18
-1
lines changed

ChangeLog

+4
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ Release date: TBA
1616

1717
Closes #1735
1818

19+
* Suppress ``UserWarning`` when finding module specs.
20+
21+
Closes pylint-dev/pylint#7906
22+
1923

2024
What's New in astroid 2.15.2?
2125
=============================

astroid/interpreter/_import/spec.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import pathlib
1414
import sys
1515
import types
16+
import warnings
1617
import zipimport
1718
from collections.abc import Iterator, Sequence
1819
from pathlib import Path
@@ -147,7 +148,9 @@ def find_module(
147148
)
148149
else:
149150
try:
150-
spec = importlib.util.find_spec(modname)
151+
with warnings.catch_warnings():
152+
warnings.filterwarnings("ignore", category=UserWarning)
153+
spec = importlib.util.find_spec(modname)
151154
if (
152155
spec
153156
and spec.loader # type: ignore[comparison-overlap] # noqa: E501

tests/test_manager.py

+10
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import sys
88
import time
99
import unittest
10+
import warnings
1011
from collections.abc import Iterator
1112
from contextlib import contextmanager
1213
from unittest import mock
@@ -383,6 +384,15 @@ def test_raises_exception_for_empty_modname(self) -> None:
383384
self.manager.ast_from_module_name(None)
384385

385386

387+
class IsolatedAstroidManagerTest(resources.AstroidCacheSetupMixin, unittest.TestCase):
388+
def test_no_user_warning(self):
389+
mgr = manager.AstroidManager()
390+
with warnings.catch_warnings():
391+
warnings.filterwarnings("error", category=UserWarning)
392+
mgr.ast_from_module_name("setuptools")
393+
mgr.ast_from_module_name("pip")
394+
395+
386396
class BorgAstroidManagerTC(unittest.TestCase):
387397
def test_borg(self) -> None:
388398
"""Test that the AstroidManager is really a borg, i.e. that two different

0 commit comments

Comments
 (0)