def test_variable_initialization(ast_var_node: ast.Name): variable = VariableEntity(ast_var_node) assert variable.line_nr == ast_var_node.lineno assert variable.name == ast_var_node.id assert variable.context == 'Load' assert variable.used_in_unpacking_assignment is False
def test_starred_entity_set_value(mocker, ast_starred_node, ast_var_node): variable = VariableEntity(ast_var_node) starred_entity = StarredEntity(ast_starred_node) mocker.spy(variable, 'mark_as_starred') starred_entity.set_value_entity(variable) assert starred_entity.value == variable assert variable.mark_as_starred.call_count == 1
def parse(self, node: ast.Name, module: Module, context: Context): """ Add a variable to the statistics based on the type of context it is encountered in the source code. There are three possible context values: - `ast.Load` that denotes loading a variable's value, like in `print(a)` - `ast.Store` that denotes storing a value in a variable, like in `a = 1` - `ast.Del` that denotes deleting a variable, like in `del a` :param node: The node that represents a variable name to be added to the statistics. :param module: The python module where this variable is encountered. :param context: The context of the given node in the Abstract Syntax Tree. :return: ??? """ variable = VariableEntity(node) module.add_variable(variable) return variable
def test_variable_mark_as_participating_as_keyword_arg(variable_load_entity: VariableEntity, is_double_starred: bool): variable_load_entity.mark_as_participating_as_keyword_arg(True, False, is_double_starred) assert variable_load_entity.used_in_function_call is True assert variable_load_entity.used_in_function_definition is False assert variable_load_entity.is_double_starred == is_double_starred
def test_variable_mark_as_participating_in_unpacking(variable_load_entity: VariableEntity, in_tuple: bool, in_list: bool): variable_load_entity.mark_as_participating_in_unpacking(in_tuple=in_tuple, in_list=in_list) assert variable_load_entity.used_in_unpacking_assignment is True assert variable_load_entity.used_in_tuple == in_tuple assert variable_load_entity.used_in_list == in_list
def starred_entity(): starred_entity = StarredEntity(ast_starred_var) starred_entity.set_value_entity(VariableEntity(ast_name_load)) return starred_entity
def call_name_entity(): return VariableEntity(ast_call_name)
def keyword_argument_num_entity(): return KeywordEntity(arg='ka', value=VariableEntity(ast_name_load))
def keyword_argument_double_starred_entity(): return KeywordEntity(arg=None, value=VariableEntity(ast_name_load))
def variable_store_entity(): return VariableEntity(ast_name_store)
def variable_load_entity(): return VariableEntity(ast_name_load)