Skip to content

Commit bb14257

Browse files
tadeunicoddemus
authored andcommitted
Fix errors when using --import-mode=importlib with dataclasses or pickle
Fixes pytest-dev#7856, fixes pytest-dev#7859
1 parent 89dcfbf commit bb14257

File tree

4 files changed

+57
-0
lines changed

4 files changed

+57
-0
lines changed

AUTHORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ Dominic Mortlock
9494
Duncan Betts
9595
Edison Gustavo Muenz
9696
Edoardo Batini
97+
Edson Tadeu M. Manoel
9798
Eduardo Schettino
9899
Eli Boyarski
99100
Elizaveta Shashkova

changelog/7856.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
An error with ``--import-mode=importlib`` used with modules containing dataclasses or pickle was fixed.

src/_pytest/pathlib.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -500,6 +500,7 @@ def import_path(
500500
"Can't find module {} at location {}".format(module_name, str(path))
501501
)
502502
mod = importlib.util.module_from_spec(spec)
503+
sys.modules[module_name] = mod
503504
spec.loader.exec_module(mod) # type: ignore[union-attr]
504505
return mod
505506

testing/test_pathlib.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -435,3 +435,57 @@ def test_samefile_false_negatives(tmp_path: Path, monkeypatch: MonkeyPatch) -> N
435435
mp.setattr(os.path, "samefile", lambda x, y: False)
436436
module = import_path(module_path)
437437
assert getattr(module, "foo")() == 42
438+
439+
440+
@pytest.fixture
441+
def module_with_dataclass(tmpdir):
442+
fn = tmpdir.join("test_dataclass.py")
443+
fn.write(
444+
dedent(
445+
f"""
446+
{'from __future__ import annotations' if (3, 7) <= sys.version_info < (3, 10) else ''}
447+
448+
from dataclasses import dataclass
449+
450+
@dataclass
451+
class DataClass:
452+
value: str
453+
454+
def test_dataclass():
455+
assert DataClass(value='test').value == 'test'
456+
"""
457+
)
458+
)
459+
return fn
460+
461+
462+
@pytest.fixture
463+
def module_with_pickle(tmpdir):
464+
fn = tmpdir.join("test_dataclass.py")
465+
fn.write(
466+
dedent(
467+
"""
468+
import pickle
469+
470+
def do_action():
471+
pass
472+
473+
def test_pickle():
474+
pickle.dumps(do_action)
475+
"""
476+
)
477+
)
478+
return fn
479+
480+
481+
@pytest.mark.skipif(sys.version_info < (3, 7), reason="Dataclasses in Python3.7+")
482+
def test_importmode_importlib_with_dataclass(module_with_dataclass):
483+
"""Ensure that importlib mode works with a module containing dataclasses"""
484+
module = import_path(module_with_dataclass, mode="importlib")
485+
module.test_dataclass() # type: ignore[attr-defined]
486+
487+
488+
def test_importmode_importlib_with_pickle(module_with_pickle):
489+
"""Ensure that importlib mode works with pickle"""
490+
module = import_path(module_with_pickle, mode="importlib")
491+
module.test_pickle() # type: ignore[attr-defined]

0 commit comments

Comments
 (0)