Skip to content

Commit 2d4b854

Browse files
committed
Use importlib.import_module instead of __import__
As recommended by Python's documentation for `__import__`.
1 parent 0e05bfe commit 2d4b854

File tree

7 files changed

+14
-13
lines changed

7 files changed

+14
-13
lines changed

src/_pytest/config/__init__.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import enum
1919
from functools import lru_cache
2020
import glob
21+
import importlib
2122
import importlib.metadata
2223
import inspect
2324
import os
@@ -874,7 +875,7 @@ def import_plugin(self, modname: str, consider_entry_points: bool = False) -> No
874875
return
875876

876877
try:
877-
__import__(importspec)
878+
mod = importlib.import_module(importspec)
878879
except ImportError as e:
879880
raise ImportError(
880881
f'Error importing plugin "{modname}": {e.args[0]}'
@@ -883,7 +884,6 @@ def import_plugin(self, modname: str, consider_entry_points: bool = False) -> No
883884
except Skipped as e:
884885
self.skipped_plugins.append((modname, e.msg or ""))
885886
else:
886-
mod = sys.modules[importspec]
887887
self.register(mod, modname)
888888

889889

@@ -2122,7 +2122,7 @@ def _resolve_warning_category(category: str) -> type[Warning]:
21222122
klass = category
21232123
else:
21242124
module, _, klass = category.rpartition(".")
2125-
m = __import__(module, None, None, [klass])
2125+
m = importlib.import_module(module)
21262126
cat = getattr(m, klass)
21272127
if not issubclass(cat, Warning):
21282128
raise UsageError(f"{cat} is not a Warning subclass")

src/_pytest/debugging.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from collections.abc import Callable
99
from collections.abc import Generator
1010
import functools
11+
import importlib
1112
import sys
1213
import types
1314
from typing import Any
@@ -123,8 +124,7 @@ def _import_pdb_cls(cls, capman: CaptureManager | None):
123124
modname, classname = usepdb_cls
124125

125126
try:
126-
__import__(modname)
127-
mod = sys.modules[modname]
127+
mod = importlib.import_module(modname)
128128

129129
# Handle --pdbcls=pdb:pdb.Pdb (useful e.g. with pdbpp).
130130
parts = classname.split(".")

src/_pytest/monkeypatch.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from collections.abc import Mapping
88
from collections.abc import MutableMapping
99
from contextlib import contextmanager
10+
import importlib
1011
import os
1112
from pathlib import Path
1213
import re
@@ -66,7 +67,7 @@ def resolve(name: str) -> object:
6667
parts = name.split(".")
6768

6869
used = parts.pop(0)
69-
found: object = __import__(used)
70+
found: object = importlib.import_module(used)
7071
for part in parts:
7172
used += "." + part
7273
try:
@@ -78,7 +79,7 @@ def resolve(name: str) -> object:
7879
# We use explicit un-nesting of the handling block in order
7980
# to avoid nested exceptions.
8081
try:
81-
__import__(used)
82+
importlib.import_module(used)
8283
except ImportError as ex:
8384
expected = str(ex).split()[-1]
8485
if expected == used:

src/_pytest/outcomes.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
from __future__ import annotations
55

6+
import importlib
67
import sys
78
from typing import Any
89
from typing import ClassVar
@@ -268,7 +269,7 @@ def importorskip(
268269
warnings.simplefilter("ignore")
269270

270271
try:
271-
__import__(modname)
272+
importlib.import_module(modname)
272273
except exc_type as exc:
273274
# Do not raise or issue warnings inside the catch_warnings() block.
274275
if reason is None:

testing/acceptance_test.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,9 +129,9 @@ class DummyEntryPoint:
129129
group: str = "pytest11"
130130

131131
def load(self):
132-
__import__(self.module)
132+
mod = importlib.import_module(self.module)
133133
loaded.append(self.name)
134-
return sys.modules[self.module]
134+
return mod
135135

136136
entry_points = [
137137
DummyEntryPoint("myplugin1", "mytestplugin1_module"),

testing/test_config.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -600,8 +600,7 @@ class DummyEntryPoint:
600600
group: str = "pytest11"
601601

602602
def load(self):
603-
__import__(self.module)
604-
return sys.modules[self.module]
603+
return importlib.import_module(self.module)
605604

606605
entry_points = [
607606
DummyEntryPoint("myplugin1", "myplugin1_module"),

testing/test_pathlib.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -964,7 +964,7 @@ def test_demo():
964964
)
965965

966966
# unit test
967-
__import__(name) # import standard library
967+
importlib.import_module(name) # import standard library
968968

969969
import_path( # import user files
970970
file_path,

0 commit comments

Comments
 (0)