move function specific imports to function

re: review from @asottile that this should only get imported in the function
modify the else/if logic since inside the function we already know the python version is >= 3.10, and just have to know if it is 3.11 or greater
This commit is contained in:
Elijah DeLee 2022-02-07 11:39:19 -05:00 committed by Ran Benita
parent f86a87a315
commit d45a19cfde
1 changed files with 6 additions and 5 deletions

View File

@ -43,11 +43,7 @@ from _pytest.stash import StashKey
if TYPE_CHECKING:
from _pytest.assertion import AssertionState
if sys.version_info >= (3, 11):
from importlib.resources.readers import FileReader
elif sys.version_info >= (3, 10):
from importlib.readers import FileReader
assertstate_key = StashKey["AssertionState"]()
@ -280,6 +276,11 @@ class AssertionRewritingHook(importlib.abc.MetaPathFinder, importlib.abc.Loader)
if sys.version_info >= (3, 10):
def get_resource_reader(self, name: str) -> importlib.abc.TraversableResources: # type: ignore
if sys.version_info < (3, 11):
from importlib.readers import FileReader
else:
from importlib.resources.readers import FileReader
return FileReader(types.SimpleNamespace(path=self._rewritten_names[name]))