How to use prefer_pure_python_imports method in refurb

Best Python code snippet using refurb_python

mypy_visitor.py

Source:mypy_visitor.py Github

copy

Full Screen

...36import mypy.traverser37VisitorNodeTypeMap = dict[str, type[mypy.nodes.Node]]38Namespace = dict[str, Any] # type: ignore39@contextmanager40def prefer_pure_python_imports() -> Iterator[None]:41 """42 During the scope of this context manager, all imports will be done using43 pure python versions when available.44 Credit to this answer on SO: https://stackoverflow.com/a/68685189/45 """46 @dataclass47 class PreferPureLoaderHook:48 orig_hook: Callable[[str], PathEntryFinder]49 def __call__(self, path: str) -> PathEntryFinder:50 finder = self.orig_hook(path)51 if isinstance(finder, FileFinder):52 # Move pure python file loaders to the front53 finder._loaders.sort( # type: ignore54 key=lambda pair: 0 if pair[0] in (".py", ".pyc") else 155 )56 return finder57 sys.path_hooks = [PreferPureLoaderHook(h) for h in sys.path_hooks]58 sys.path_importer_cache.clear()59 yield60 # Restore the previous behaviour61 original_hooks = []62 for hook in sys.path_hooks:63 assert isinstance(hook, PreferPureLoaderHook)64 original_hooks.append(hook.orig_hook)65 sys.path_hooks = original_hooks66 sys.path_importer_cache.clear()67@contextmanager68def pure_python_mypy() -> Iterator[None]:69 """70 Inside this context, all mypy related imports are done with the pure python71 versions.72 Any existing mypy module that was imported before needs to be reimported73 before use within the context.74 Upon exiting, the previous implementations are restored.75 """76 def loaded_mypy_modules() -> Iterator[str]:77 """Covenient block to get names of imported mypy modules"""78 for mod_name in sys.modules:79 if mod_name == "mypy" or mod_name.startswith("mypy."):80 yield mod_name81 # First, backup all imported mypy modules and remove them from sys.modules,82 # so they will not be found in resolution83 saved_mypy = {}84 for mod_name in list(loaded_mypy_modules()):85 saved_mypy[mod_name] = sys.modules.pop(mod_name)86 with prefer_pure_python_imports():87 # After the modules are clean, ensure the newly imported mypy modules88 # are their pure python versions.89 # - Pure python: methods are FunctionType90 # - Native: methods are MethodDescriptorType91 from mypy.traverser import TraverserVisitor92 assert isinstance(93 typing.cast(FunctionType, TraverserVisitor.visit_var), FunctionType94 )95 # Give back control96 yield97 # We're back and this is where we do cleanup. We'll remove all imported98 # mypy modules (pure python) and restore the previously backed-up ones99 # (allegedly native implementations)100 for mod_name in list(loaded_mypy_modules()):...

Full Screen

Full Screen

pure_importer.py

Source:pure_importer.py Github

copy

Full Screen

...13 # Move pure python file loaders to the front14 finder._loaders.sort(key=lambda pair: 0 if pair[0] in (".py", ".pyc") else 1) # type: ignore15 return finder16@contextmanager17def prefer_pure_python_imports():18 sys.path_hooks = [PreferPureLoaderHook(h) for h in sys.path_hooks]19 sys.path_importer_cache.clear()20 yield21 assert all(isinstance(h, PreferPureLoaderHook) for h in sys.path_hooks)22 sys.path_hooks = [h.orig_hook for h in sys.path_hooks]...

Full Screen

Full Screen

pure_importer_test.py

Source:pure_importer_test.py Github

copy

Full Screen

...15 sys.modules.update(saved)16@pytest.mark.skipif(17 sys.version_info >= (3, 10), reason="no compiled pydantic on 3.10 (yet)"18)19def test_prefer_pure_python_imports():20 pydantic = importlib.import_module("pydantic")21 assert pydantic.compiled22 with _pydantic_unloaded():23 with prefer_pure_python_imports():24 pydantic = importlib.import_module("pydantic")...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run refurb automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful