How to use _get_class_globals method in refurb

Best Python code snippet using refurb_python

mypy_visitor.py

Source:mypy_visitor.py Github

copy

Full Screen

...100 for mod_name in list(loaded_mypy_modules()):101 del sys.modules[mod_name]102 for mod_name, module in saved_mypy.items():103 sys.modules[mod_name] = module104def _get_class_globals(target_class: type, localns: Namespace) -> Namespace:105 """106 Get the globals namespace for the full class hierarchy that starts in107 target_class.108 This follows the recommendation of PEP-563 to resolve stringified type109 annotations at runtime.110 """111 all_globals = localns.copy()112 for base in inspect.getmro(target_class):113 all_globals.update(vars(sys.modules[base.__module__]))114 return all_globals115def _make_mappings(globalns: Namespace) -> VisitorNodeTypeMap:116 """117 Generate a mapping between the name of a visitor method in TraverserVisitor118 and the type of its first (non-self) parameter.119 """120 visitor_method_map = {}121 from mypy.traverser import TraverserVisitor122 methods = inspect.getmembers(123 TraverserVisitor,124 lambda o: inspect.isfunction(o) and o.__name__.startswith("visit_"),125 )126 for method_name, method in methods:127 method_params = list(inspect.signature(method).parameters.values())128 param_name = method_params[1].name129 method_types = typing.get_type_hints(method, globalns=globalns)130 visitor_method_map[method_name] = method_types[param_name]131 return visitor_method_map132# Capture the global namespace of the hierarchy of TraverserVisitor before we133# replace it with a short-lived pure-python version inside the context manager134# below.135_globals = _get_class_globals(mypy.traverser.TraverserVisitor, locals())136def get_mypy_visitor_mapping() -> VisitorNodeTypeMap:137 """138 Provide the visitor method name to node type mapping as it comes from Mypy.139 Resolve the mappings using the pure-python version of mypy (necessary to140 obtain method signature type info) but then ensure the types are resolved to141 their native counterparts (by passing the previously captured global142 namespace)143 """144 with pure_python_mypy():145 mapping = _make_mappings(globalns=_globals)...

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