File tree Expand file tree Collapse file tree 2 files changed +14
-0
lines changed Expand file tree Collapse file tree 2 files changed +14
-0
lines changed Original file line number Diff line number Diff line change 1
1
# Changelog
2
2
3
3
## [ Unreleased]
4
+ ### Fixed
5
+ - Fix pytest ** Module already imported so cannot be rewritten** warning when the package being linted was used by pytest/conftest already (#10 )
4
6
5
7
## [ 1.0.1] - 2021-03-03
6
8
### Added
Original file line number Diff line number Diff line change @@ -72,13 +72,25 @@ def visit_module(self, node):
72
72
# run pytest session with customized plugin to collect fixtures
73
73
fixture_collector = FixtureCollector ()
74
74
75
+ # save and remove modules imported by pytest to prevent pytest warning during fixture collection:
76
+ # <PytestAssertRewriteWarning: Module already imported so cannot be rewritten>
77
+ sys_mods = set (sys .modules .keys ())
78
+
75
79
# save and restore sys.path to prevent pytest.main from altering it
76
80
sys_path = sys .path .copy ()
81
+
77
82
pytest .main (
78
83
[node .file , '--fixtures' , '--collect-only' ],
79
84
plugins = [fixture_collector ],
80
85
)
86
+
87
+ # restore sys.path
81
88
sys .path = sys_path
89
+
90
+ # unload modules imported by pytest.main
91
+ for module in set (sys .modules .keys ()) - sys_mods :
92
+ del sys .modules [module ]
93
+
82
94
FixtureChecker ._pytest_fixtures = fixture_collector .fixtures
83
95
finally :
84
96
# restore output devices
You can’t perform that action at this time.
0 commit comments