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 11# Changelog
22
33## [ 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 )
46
57## [ 1.0.1] - 2021-03-03
68### Added
Original file line number Diff line number Diff line change @@ -72,13 +72,25 @@ def visit_module(self, node):
7272 # run pytest session with customized plugin to collect fixtures
7373 fixture_collector = FixtureCollector ()
7474
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+
7579 # save and restore sys.path to prevent pytest.main from altering it
7680 sys_path = sys .path .copy ()
81+
7782 pytest .main (
7883 [node .file , '--fixtures' , '--collect-only' ],
7984 plugins = [fixture_collector ],
8085 )
86+
87+ # restore sys.path
8188 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+
8294 FixtureChecker ._pytest_fixtures = fixture_collector .fixtures
8395 finally :
8496 # restore output devices
You can’t perform that action at this time.
0 commit comments