Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Suppress UserWarning when finding module specs #2121

Merged
merged 3 commits into from
Apr 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,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
Loading