Skip to content

Commit

Permalink
Suppress UserWarning when finding module specs (#2121) (#2122)
Browse files Browse the repository at this point in the history
Found when linting this code:
```
import setuptools
import pip
```

(cherry picked from commit 6723e63)

Co-authored-by: Jacob Walls <[email protected]>
  • Loading branch information
github-actions[bot] and jacobtylerwalls authored Apr 16, 2023
1 parent b65f191 commit a4a6104
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
4 changes: 4 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -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?
=============================
Expand Down
5 changes: 4 additions & 1 deletion astroid/interpreter/_import/spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import pathlib
import sys
import types
import warnings
import zipimport
from collections.abc import Iterator, Sequence
from pathlib import Path
Expand Down Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions tests/test_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit a4a6104

Please sign in to comment.