diff options
| author | github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> | 2023-04-16 19:27:26 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-04-16 19:27:26 +0000 |
| commit | a4a610490836e79babc0675f8f4763497e85b5d3 (patch) | |
| tree | d6ac232399d018e155a4e80043718e7863655c58 | |
| parent | b65f1917b17f8ad3d720288a70ec40f86a3b0057 (diff) | |
| download | astroid-git-a4a610490836e79babc0675f8f4763497e85b5d3.tar.gz | |
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 <jacobtylerwalls@gmail.com>
| -rw-r--r-- | ChangeLog | 4 | ||||
| -rw-r--r-- | astroid/interpreter/_import/spec.py | 5 | ||||
| -rw-r--r-- | tests/test_manager.py | 10 |
3 files changed, 18 insertions, 1 deletions
@@ -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 f000468e..5a7c4e82 100644 --- a/astroid/interpreter/_import/spec.py +++ b/astroid/interpreter/_import/spec.py @@ -13,6 +13,7 @@ import os import pathlib import sys import types +import warnings import zipimport from collections.abc import Iterator, Sequence from pathlib import Path @@ -147,7 +148,9 @@ class ImportlibFinder(Finder): ) 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 9b9b24fe..19efc730 100644 --- a/tests/test_manager.py +++ b/tests/test_manager.py @@ -7,6 +7,7 @@ import site 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 @@ class AstroidManagerTest( 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 |
