File tree 4 files changed +54
-0
lines changed
4 files changed +54
-0
lines changed Original file line number Diff line number Diff line change @@ -90,6 +90,7 @@ Dmitry Pribysh
90
90
Duncan Betts
91
91
Edison Gustavo Muenz
92
92
Edoardo Batini
93
+ Edson Tadeu M. Manoel
93
94
Eduardo Schettino
94
95
Eli Boyarski
95
96
Elizaveta Shashkova
Original file line number Diff line number Diff line change
1
+ An error with ``--import-mode=importlib `` used with modules containing dataclasses or pickle was fixed.
Original file line number Diff line number Diff line change @@ -480,6 +480,7 @@ def import_path(
480
480
"Can't find module {} at location {}" .format (module_name , str (path ))
481
481
)
482
482
mod = importlib .util .module_from_spec (spec )
483
+ sys .modules [module_name ] = mod
483
484
spec .loader .exec_module (mod ) # type: ignore[union-attr]
484
485
return mod
485
486
Original file line number Diff line number Diff line change @@ -401,3 +401,54 @@ def test_commonpath() -> None:
401
401
assert commonpath (subpath , path ) == path
402
402
assert commonpath (Path (str (path ) + "suffix" ), path ) == path .parent
403
403
assert commonpath (path , path .parent .parent ) == path .parent .parent
404
+
405
+
406
+ @pytest .fixture
407
+ def module_with_dataclass (tmpdir ):
408
+ fn = tmpdir .join ("test_dataclass.py" )
409
+ fn .write (
410
+ dedent (f"""\
411
+ { 'from __future__ import annotations' if (3 , 7 ) <= sys .version_info < (3 , 10 ) else '' }
412
+
413
+ from dataclasses import dataclass
414
+
415
+ @dataclass
416
+ class DataClass:
417
+ value: str
418
+
419
+ def test_dataclass():
420
+ assert DataClass(value='test').value == 'test'
421
+ """
422
+ )
423
+ )
424
+ return fn
425
+
426
+
427
+ @pytest .fixture
428
+ def module_with_pickle (tmpdir ):
429
+ fn = tmpdir .join ("test_dataclass.py" )
430
+ fn .write (
431
+ dedent ("""\
432
+ import pickle
433
+
434
+ def do_action():
435
+ pass
436
+
437
+ def test_pickle():
438
+ pickle.dumps(do_action)
439
+ """
440
+ )
441
+ )
442
+ return fn
443
+
444
+
445
+ def test_importmode_importlib_with_dataclass (module_with_dataclass ):
446
+ """Ensure that importlib mode works with a module containing dataclasses"""
447
+ module = import_path (module_with_dataclass , mode = "importlib" )
448
+ module .test_dataclass ()
449
+
450
+
451
+ def test_importmode_importlib_with_pickle (module_with_pickle ):
452
+ """Ensure that importlib mode works with pickle"""
453
+ module = import_path (module_with_pickle , mode = "importlib" )
454
+ module .test_pickle ()
You can’t perform that action at this time.
0 commit comments