How to use class_context method in Testify

Best Python code snippet using Testify_python

instance.py

Source:instance.py Github

copy

Full Screen

1from abc import abstractproperty2from jedi import debug3from jedi import settings4from jedi.evaluate import compiled5from jedi.evaluate import filters6from jedi.evaluate.base_context import Context, NO_CONTEXTS, ContextSet, \7 iterator_to_context_set8from jedi.evaluate.lazy_context import LazyKnownContext, LazyKnownContexts9from jedi.evaluate.cache import evaluator_method_cache10from jedi.evaluate.arguments import AbstractArguments, AnonymousArguments11from jedi.evaluate.context.function import FunctionExecutionContext, \12 FunctionContext, AbstractFunction13from jedi.evaluate.context.klass import ClassContext, apply_py__get__, ClassFilter14from jedi.evaluate.context import iterable15from jedi.parser_utils import get_parent_scope16class InstanceExecutedParam(object):17 def __init__(self, instance):18 self._instance = instance19 def infer(self):20 return ContextSet(self._instance)21class AnonymousInstanceArguments(AnonymousArguments):22 def __init__(self, instance):23 self._instance = instance24 def get_executed_params(self, execution_context):25 from jedi.evaluate.dynamic import search_params26 self_param = InstanceExecutedParam(self._instance)27 tree_params = execution_context.tree_node.get_params()28 if len(tree_params) == 1:29 # If the only param is self, we don't need to try to find30 # executions of this function, we have all the params already.31 return [self_param]32 executed_params = list(search_params(33 execution_context.evaluator,34 execution_context,35 execution_context.tree_node36 ))37 executed_params[0] = self_param38 return executed_params39class AbstractInstanceContext(Context):40 """41 This class is used to evaluate instances.42 """43 api_type = u'instance'44 def __init__(self, evaluator, parent_context, class_context, var_args):45 super(AbstractInstanceContext, self).__init__(evaluator, parent_context)46 # Generated instances are classes that are just generated by self47 # (No var_args) used.48 self.class_context = class_context49 self.var_args = var_args50 def is_class(self):51 return False52 @property53 def py__call__(self):54 names = self.get_function_slot_names(u'__call__')55 if not names:56 # Means the Instance is not callable.57 raise AttributeError58 def execute(arguments):59 return ContextSet.from_sets(name.infer().execute(arguments) for name in names)60 return execute61 def py__class__(self):62 return self.class_context63 def py__bool__(self):64 # Signalize that we don't know about the bool type.65 return None66 def get_function_slot_names(self, name):67 # Python classes don't look at the dictionary of the instance when68 # looking up `__call__`. This is something that has to do with Python's69 # internal slot system (note: not __slots__, but C slots).70 for filter in self.get_filters(include_self_names=False):71 names = filter.get(name)72 if names:73 return names74 return []75 def execute_function_slots(self, names, *evaluated_args):76 return ContextSet.from_sets(77 name.infer().execute_evaluated(*evaluated_args)78 for name in names79 )80 def py__get__(self, obj):81 # Arguments in __get__ descriptors are obj, class.82 # `method` is the new parent of the array, don't know if that's good.83 names = self.get_function_slot_names(u'__get__')84 if names:85 if isinstance(obj, AbstractInstanceContext):86 return self.execute_function_slots(names, obj, obj.class_context)87 else:88 none_obj = compiled.builtin_from_name(self.evaluator, u'None')89 return self.execute_function_slots(names, none_obj, obj)90 else:91 return ContextSet(self)92 def get_filters(self, search_global=None, until_position=None,93 origin_scope=None, include_self_names=True):94 if include_self_names:95 for cls in self.class_context.py__mro__():96 if not isinstance(cls, compiled.CompiledObject) \97 or cls.tree_node is not None:98 # In this case we're excluding compiled objects that are99 # not fake objects. It doesn't make sense for normal100 # compiled objects to search for self variables.101 yield SelfAttributeFilter(self.evaluator, self, cls, origin_scope)102 for cls in self.class_context.py__mro__():103 if isinstance(cls, compiled.CompiledObject):104 yield CompiledInstanceClassFilter(self.evaluator, self, cls)105 else:106 yield InstanceClassFilter(self.evaluator, self, cls, origin_scope)107 def py__getitem__(self, index):108 try:109 names = self.get_function_slot_names(u'__getitem__')110 except KeyError:111 debug.warning('No __getitem__, cannot access the array.')112 return NO_CONTEXTS113 else:114 index_obj = compiled.create_simple_object(self.evaluator, index)115 return self.execute_function_slots(names, index_obj)116 def py__iter__(self):117 iter_slot_names = self.get_function_slot_names(u'__iter__')118 if not iter_slot_names:119 debug.warning('No __iter__ on %s.' % self)120 return121 for generator in self.execute_function_slots(iter_slot_names):122 if isinstance(generator, AbstractInstanceContext):123 # `__next__` logic.124 if self.evaluator.environment.version_info.major == 2:125 name = u'next'126 else:127 name = u'__next__'128 iter_slot_names = generator.get_function_slot_names(name)129 if iter_slot_names:130 yield LazyKnownContexts(131 generator.execute_function_slots(iter_slot_names)132 )133 else:134 debug.warning('Instance has no __next__ function in %s.', generator)135 else:136 for lazy_context in generator.py__iter__():137 yield lazy_context138 @abstractproperty139 def name(self):140 pass141 def _create_init_execution(self, class_context, bound_method):142 return bound_method.get_function_execution(self.var_args)143 def create_init_executions(self):144 for name in self.get_function_slot_names(u'__init__'):145 if isinstance(name, LazyInstanceClassName):146 function = FunctionContext.from_context(147 self.parent_context,148 name.tree_name.parent149 )150 bound_method = BoundMethod(self, name.class_context, function)151 yield self._create_init_execution(name.class_context, bound_method)152 @evaluator_method_cache()153 def create_instance_context(self, class_context, node):154 if node.parent.type in ('funcdef', 'classdef'):155 node = node.parent156 scope = get_parent_scope(node)157 if scope == class_context.tree_node:158 return class_context159 else:160 parent_context = self.create_instance_context(class_context, scope)161 if scope.type == 'funcdef':162 func = FunctionContext.from_context(163 parent_context,164 scope,165 )166 bound_method = BoundMethod(self, class_context, func)167 if scope.name.value == '__init__' and parent_context == class_context:168 return self._create_init_execution(class_context, bound_method)169 else:170 return bound_method.get_function_execution()171 elif scope.type == 'classdef':172 class_context = ClassContext(self.evaluator, parent_context, scope)173 return class_context174 elif scope.type == 'comp_for':175 # Comprehensions currently don't have a special scope in Jedi.176 return self.create_instance_context(class_context, scope)177 else:178 raise NotImplementedError179 return class_context180 def __repr__(self):181 return "<%s of %s(%s)>" % (self.__class__.__name__, self.class_context,182 self.var_args)183class CompiledInstance(AbstractInstanceContext):184 def __init__(self, evaluator, parent_context, class_context, var_args):185 self._original_var_args = var_args186 # I don't think that dynamic append lookups should happen here. That187 # sounds more like something that should go to py__iter__.188 if class_context.py__name__() in ['list', 'set'] \189 and parent_context.get_root_context() == evaluator.builtins_module:190 # compare the module path with the builtin name.191 if settings.dynamic_array_additions:192 var_args = iterable.get_dynamic_array_instance(self, var_args)193 super(CompiledInstance, self).__init__(evaluator, parent_context, class_context, var_args)194 @property195 def name(self):196 return compiled.CompiledContextName(self, self.class_context.name.string_name)197 def create_instance_context(self, class_context, node):198 if get_parent_scope(node).type == 'classdef':199 return class_context200 else:201 return super(CompiledInstance, self).create_instance_context(class_context, node)202 def get_first_non_keyword_argument_contexts(self):203 key, lazy_context = next(self._original_var_args.unpack(), ('', None))204 if key is not None:205 return NO_CONTEXTS206 return lazy_context.infer()207class TreeInstance(AbstractInstanceContext):208 def __init__(self, evaluator, parent_context, class_context, var_args):209 super(TreeInstance, self).__init__(evaluator, parent_context,210 class_context, var_args)211 self.tree_node = class_context.tree_node212 @property213 def name(self):214 return filters.ContextName(self, self.class_context.name.tree_name)215class AnonymousInstance(TreeInstance):216 def __init__(self, evaluator, parent_context, class_context):217 super(AnonymousInstance, self).__init__(218 evaluator,219 parent_context,220 class_context,221 var_args=AnonymousInstanceArguments(self),222 )223class CompiledInstanceName(compiled.CompiledName):224 def __init__(self, evaluator, instance, klass, name):225 super(CompiledInstanceName, self).__init__(226 evaluator,227 klass.parent_context,228 name.string_name229 )230 self._instance = instance231 self._class = klass232 self._class_member_name = name233 @iterator_to_context_set234 def infer(self):235 for result_context in self._class_member_name.infer():236 is_function = result_context.api_type == 'function'237 if result_context.tree_node is not None and is_function:238 yield BoundMethod(self._instance, self._class, result_context)239 else:240 if is_function:241 yield CompiledBoundMethod(result_context)242 else:243 yield result_context244class CompiledInstanceClassFilter(filters.AbstractFilter):245 name_class = CompiledInstanceName246 def __init__(self, evaluator, instance, klass):247 self._evaluator = evaluator248 self._instance = instance249 self._class = klass250 self._class_filter = next(klass.get_filters(is_instance=True))251 def get(self, name):252 return self._convert(self._class_filter.get(name))253 def values(self):254 return self._convert(self._class_filter.values())255 def _convert(self, names):256 return [257 CompiledInstanceName(self._evaluator, self._instance, self._class, n)258 for n in names259 ]260class BoundMethod(AbstractFunction):261 def __init__(self, instance, klass, function):262 super(BoundMethod, self).__init__(263 function.evaluator,264 function.parent_context,265 function.tree_node,266 )267 self._instance = instance268 self._class = klass269 self._function = function270 def py__class__(self):271 return compiled.get_special_object(self.evaluator, u'BOUND_METHOD_CLASS')272 def get_function_execution(self, arguments=None):273 if arguments is None:274 arguments = AnonymousInstanceArguments(self._instance)275 arguments = InstanceArguments(self._instance, arguments)276 if isinstance(self._function, compiled.CompiledObject):277 # This is kind of weird, because it's coming from a compiled object278 # and we're not sure if we want that in the future.279 return FunctionExecutionContext(280 self.evaluator, self.parent_context, self, arguments281 )282 return self._function.get_function_execution(arguments)283 def __repr__(self):284 return '<%s: %s>' % (self.__class__.__name__, self._function)285class CompiledBoundMethod(compiled.CompiledObject):286 def __init__(self, func):287 super(CompiledBoundMethod, self).__init__(288 func.evaluator, func.access_handle, func.parent_context, func.tree_node)289 def get_param_names(self):290 return list(super(CompiledBoundMethod, self).get_param_names())[1:]291class SelfName(filters.TreeNameDefinition):292 """293 This name calculates the parent_context lazily.294 """295 def __init__(self, instance, class_context, tree_name):296 self._instance = instance297 self.class_context = class_context298 self.tree_name = tree_name299 @property300 def parent_context(self):301 return self._instance.create_instance_context(self.class_context, self.tree_name)302class LazyInstanceClassName(object):303 def __init__(self, instance, class_context, class_member_name):304 self._instance = instance305 self.class_context = class_context306 self._class_member_name = class_member_name307 @iterator_to_context_set308 def infer(self):309 for result_context in self._class_member_name.infer():310 if isinstance(result_context, FunctionContext):311 # Classes are never used to resolve anything within the312 # functions. Only other functions and modules will resolve313 # those things.314 yield BoundMethod(self._instance, self.class_context, result_context)315 else:316 for c in apply_py__get__(result_context, self._instance):317 yield c318 def __getattr__(self, name):319 return getattr(self._class_member_name, name)320class InstanceClassFilter(filters.AbstractFilter):321 """322 This filter is special in that it uses the class filter and wraps the323 resulting names in LazyINstanceClassName. The idea is that the class name324 filtering can be very flexible and always be reflected in instances.325 """326 def __init__(self, evaluator, context, class_context, origin_scope):327 self._instance = context328 self._class_context = class_context329 self._class_filter = next(class_context.get_filters(330 search_global=False,331 origin_scope=origin_scope,332 is_instance=True,333 ))334 def get(self, name):335 return self._convert(self._class_filter.get(name))336 def values(self):337 return self._convert(self._class_filter.values())338 def _convert(self, names):339 return [LazyInstanceClassName(self._instance, self._class_context, n) for n in names]340class SelfAttributeFilter(ClassFilter):341 """342 This class basically filters all the use cases where `self.*` was assigned.343 """344 name_class = SelfName345 def __init__(self, evaluator, context, class_context, origin_scope):346 super(SelfAttributeFilter, self).__init__(347 evaluator=evaluator,348 context=context,349 node_context=class_context,350 origin_scope=origin_scope,351 is_instance=True,352 )353 self._class_context = class_context354 def _filter(self, names):355 names = self._filter_self_names(names)356 if isinstance(self._parser_scope, compiled.CompiledObject) and False:357 # This would be for builtin skeletons, which are not yet supported.358 return list(names)359 else:360 start, end = self._parser_scope.start_pos, self._parser_scope.end_pos361 return [n for n in names if start < n.start_pos < end]362 def _filter_self_names(self, names):363 for name in names:364 trailer = name.parent365 if trailer.type == 'trailer' \366 and len(trailer.children) == 2 \367 and trailer.children[0] == '.':368 if name.is_definition() and self._access_possible(name):369 yield name370 def _convert_names(self, names):371 return [self.name_class(self.context, self._class_context, name) for name in names]372 def _check_flows(self, names):373 return names374class InstanceArguments(AbstractArguments):375 def __init__(self, instance, var_args):376 self.instance = instance377 self._var_args = var_args378 @property379 def argument_node(self):380 return self._var_args.argument_node381 @property382 def trailer(self):383 return self._var_args.trailer384 def unpack(self, func=None):385 yield None, LazyKnownContext(self.instance)386 for values in self._var_args.unpack(func):387 yield values388 def get_calling_nodes(self):389 return self._var_args.get_calling_nodes()390 def get_executed_params(self, execution_context):391 if isinstance(self._var_args, AnonymousInstanceArguments):392 return self._var_args.get_executed_params(execution_context)...

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 Testify 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