Skip to content

Commit d2b791f

Browse files
Avoid getters and setters: enable EO007 check
1 parent 2bba202 commit d2b791f

64 files changed

Lines changed: 214 additions & 224 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.flake8

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
[flake8]
2-
ignore = W504,EO001,EO002,EO003,EO004,EO005,EO007,EO008,EO009,EO01,EO02
2+
ignore = W504,EO001,EO002,EO003,EO004,EO005,EO008,EO009,EO01,EO02
33
max-line-length = 120
44
max-complexity = 10

aibolit/__main__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -272,8 +272,8 @@ def calculate_patterns_and_metrics_with_decomposition(
272272
]
273273
ast = AST.build_from_javalang(build_ast(file_path))
274274
classes_ast = [
275-
ast.get_subtree(node)
276-
for node in ast.get_root().types
275+
ast.subtree(node)
276+
for node in ast.root().types
277277
if node.node_type == ASTNodeType.CLASS_DECLARATION
278278
]
279279
for class_ast in classes_ast:

aibolit/ast_framework/ast.py

Lines changed: 56 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@
3030

3131
class AST:
3232
def __init__(self, networkx_tree: DiGraph, root: int):
33-
self.tree = networkx_tree
34-
self.root = root
33+
self._tree = networkx_tree
34+
self._root = root
3535

3636
@staticmethod
3737
def build_from_javalang(javalang_ast_root: Node) -> 'AST':
@@ -45,15 +45,15 @@ def build_from_javalang(javalang_ast_root: Node) -> 'AST':
4545
def __str__(self) -> str:
4646
printed_graph = ''
4747
depth = 0
48-
for _, destination, edge_type in dfs_labeled_edges(self.tree, self.root):
48+
for _, destination, edge_type in dfs_labeled_edges(self._tree, self._root):
4949
if edge_type == 'forward':
5050
printed_graph += '| ' * depth
51-
node_type = self.tree.nodes[destination]['node_type']
51+
node_type = self._tree.nodes[destination]['node_type']
5252
printed_graph += str(node_type) + ': '
5353
if node_type == ASTNodeType.STRING:
54-
printed_graph += self.tree.nodes[destination]['string'] + ', '
54+
printed_graph += self._tree.nodes[destination]['string'] + ', '
5555
printed_graph += f'node index = {destination}'
56-
node_line = self.tree.nodes[destination]['line']
56+
node_line = self._tree.nodes[destination]['line']
5757
if node_line is not None:
5858
printed_graph += f', line = {node_line}'
5959
printed_graph += '\n'
@@ -62,14 +62,14 @@ def __str__(self) -> str:
6262
depth -= 1
6363
return printed_graph
6464

65-
def get_root(self) -> ASTNode:
66-
return ASTNode(self.tree, self.root)
65+
def root(self) -> ASTNode:
66+
return ASTNode(self._tree, self._root)
6767

6868
def __iter__(self) -> Iterator[ASTNode]:
69-
for node_index in self.tree.nodes:
70-
yield ASTNode(self.tree, node_index)
69+
for node_index in self._tree.nodes:
70+
yield ASTNode(self._tree, node_index)
7171

72-
def get_subtrees(self, *root_type: ASTNodeType) -> Iterator['AST']:
72+
def subtrees(self, *root_type: ASTNodeType) -> Iterator['AST']:
7373
"""
7474
Yields subtrees with given type of the root.
7575
If such subtrees are one including the other, only the larger one is
@@ -78,23 +78,23 @@ def get_subtrees(self, *root_type: ASTNodeType) -> Iterator['AST']:
7878
is_inside_subtree = False
7979
current_subtree_root = -1 # all node indexes are positive
8080
subtree: List[int] = []
81-
for _, destination, edge_type in dfs_labeled_edges(self.tree, self.root):
81+
for _, destination, edge_type in dfs_labeled_edges(self._tree, self._root):
8282
if edge_type == 'forward':
8383
if is_inside_subtree:
8484
subtree.append(destination)
85-
elif self.tree.nodes[destination]['node_type'] in root_type:
85+
elif self._tree.nodes[destination]['node_type'] in root_type:
8686
subtree.append(destination)
8787
is_inside_subtree = True
8888
current_subtree_root = destination
8989
elif edge_type == 'reverse' and destination == current_subtree_root:
9090
is_inside_subtree = False
91-
yield AST(self.tree.subgraph(subtree), current_subtree_root)
91+
yield AST(self._tree.subgraph(subtree), current_subtree_root)
9292
subtree = []
9393
current_subtree_root = -1
9494

95-
def get_subtree(self, node: ASTNode) -> 'AST':
96-
subtree_nodes_indexes = dfs_preorder_nodes(self.tree, node.node_index)
97-
subtree = self.tree.subgraph(subtree_nodes_indexes)
95+
def subtree(self, node: ASTNode) -> 'AST':
96+
subtree_nodes_indexes = dfs_preorder_nodes(self._tree, node.node_index)
97+
subtree = self._tree.subgraph(subtree_nodes_indexes)
9898
return AST(subtree, node.node_index)
9999

100100
def traverse(
@@ -104,31 +104,31 @@ def traverse(
104104
source_node: Optional[ASTNode] = None,
105105
undirected=False
106106
):
107-
traverse_graph = self.tree.to_undirected(as_view=True) if undirected else self.tree
107+
traverse_graph = self._tree.to_undirected(as_view=True) if undirected else self._tree
108108
if source_node is None:
109-
source_node = self.get_root()
109+
source_node = self.root()
110110

111111
for _, destination, edge_type in dfs_labeled_edges(traverse_graph, source_node.node_index):
112112
if edge_type == 'forward':
113-
on_node_entering(ASTNode(self.tree, destination))
113+
on_node_entering(ASTNode(self._tree, destination))
114114
elif edge_type == 'reverse':
115-
on_node_leaving(ASTNode(self.tree, destination))
115+
on_node_leaving(ASTNode(self._tree, destination))
116116

117117
@deprecated(reason='Use ASTNode functionality instead.')
118118
def children_with_type(self, node: int, child_type: ASTNodeType) -> Iterator[int]:
119119
"""
120120
Yields children of node with given type.
121121
"""
122-
for child in self.tree.succ[node]:
123-
if self.tree.nodes[child]['node_type'] == child_type:
122+
for child in self._tree.succ[node]:
123+
if self._tree.nodes[child]['node_type'] == child_type:
124124
yield child
125125

126126
@deprecated(reason='Use ASTNode functionality instead.')
127127
def list_all_children_with_type(self, node: int, child_type: ASTNodeType) -> List[int]:
128128
list_node: List[int] = []
129-
for child in self.tree.succ[node]:
129+
for child in self._tree.succ[node]:
130130
list_node = list_node + self.list_all_children_with_type(child, child_type)
131-
if self.tree.nodes[child]['node_type'] == child_type:
131+
if self._tree.nodes[child]['node_type'] == child_type:
132132
list_node.append(child)
133133
return sorted(list_node)
134134

@@ -140,68 +140,68 @@ def all_children_with_type(self, node: int, child_type: ASTNodeType) -> Iterator
140140
yield from self.list_all_children_with_type(node, child_type)
141141

142142
@deprecated(reason='Use ASTNode functionality instead.')
143-
def get_first_n_children_with_type(
143+
def first_n_children_with_type(
144144
self, node: int, child_type: ASTNodeType, quantity: int
145145
) -> List[int]:
146146
"""
147147
Returns first quantity of children of node with type child_type.
148148
Resulted list is padded with None to length quantity.
149149
"""
150150
children_with_type = (
151-
child for child in self.tree.succ[node] if self.get_type(child) == child_type
151+
child for child in self._tree.succ[node] if self.type(child) == child_type
152152
)
153153
children_with_type_padded = chain(children_with_type, repeat(None))
154154
return list(islice(children_with_type_padded, 0, quantity))
155155

156156
@deprecated(reason='Use ASTNode functionality instead.')
157-
def get_binary_operation_name(self, node: int) -> str:
158-
assert self.get_type(node) == ASTNodeType.BINARY_OPERATION
157+
def binary_operation_name(self, node: int) -> str:
158+
assert self.type(node) == ASTNodeType.BINARY_OPERATION
159159
name_node, = islice(self.children_with_type(node, ASTNodeType.STRING), 1)
160-
return self.get_attr(name_node, 'string')
160+
return self.attr(name_node, 'string')
161161

162162
@deprecated(reason='Use ASTNode functionality instead.')
163-
def get_line_number_from_children(self, node: int) -> int:
164-
for child in self.tree.succ[node]:
165-
cur_line = self.get_attr(child, 'line')
163+
def line_number_from_children(self, node: int) -> int:
164+
for child in self._tree.succ[node]:
165+
cur_line = self.attr(child, 'line')
166166
if cur_line is not None:
167167
return cur_line
168168
return 0
169169

170170
@deprecated(reason='Use get_proxy_nodes instead.')
171-
def get_nodes(self, type: Union[ASTNodeType, None] = None) -> Iterator[int]:
172-
for node in self.tree.nodes:
173-
if type is None or self.tree.nodes[node]['node_type'] == type:
171+
def nodes(self, type: Union[ASTNodeType, None] = None) -> Iterator[int]:
172+
for node in self._tree.nodes:
173+
if type is None or self._tree.nodes[node]['node_type'] == type:
174174
yield node
175175

176-
def get_proxy_nodes(self, *types: ASTNodeType) -> Iterator[ASTNode]:
177-
for node in self.tree.nodes:
178-
if len(types) == 0 or self.tree.nodes[node]['node_type'] in types:
179-
yield ASTNode(self.tree, node)
176+
def proxy_nodes(self, *types: ASTNodeType) -> Iterator[ASTNode]:
177+
for node in self._tree.nodes:
178+
if len(types) == 0 or self._tree.nodes[node]['node_type'] in types:
179+
yield ASTNode(self._tree, node)
180180

181181
@deprecated(reason='Use ASTNode functionality instead.')
182-
def get_attr(self, node: int, attr_name: str, default_value: Any = None) -> Any:
183-
return self.tree.nodes[node].get(attr_name, default_value)
182+
def attr(self, node: int, attr_name: str, default_value: Any = None) -> Any:
183+
return self._tree.nodes[node].get(attr_name, default_value)
184184

185185
@deprecated(reason='Use ASTNode functionality instead.')
186-
def get_type(self, node: int) -> ASTNodeType:
187-
return self.get_attr(node, 'node_type')
186+
def type(self, node: int) -> ASTNodeType:
187+
return self.attr(node, 'node_type')
188188

189189
@deprecated(reason='Use ASTNode functionality instead.')
190-
def get_method_invocation_params(self, invocation_node: int) -> MethodInvocationParams:
191-
assert self.get_type(invocation_node) == ASTNodeType.METHOD_INVOCATION
190+
def method_invocation_params(self, invocation_node: int) -> MethodInvocationParams:
191+
assert self.type(invocation_node) == ASTNodeType.METHOD_INVOCATION
192192
# first two STRING nodes represent object and method names
193193
children = list(self.children_with_type(invocation_node, ASTNodeType.STRING))
194194
if len(children) == 1:
195-
return MethodInvocationParams('', self.get_attr(children[0], 'string'))
195+
return MethodInvocationParams('', self.attr(children[0], 'string'))
196196

197-
return MethodInvocationParams(self.get_attr(children[0], 'string'),
198-
self.get_attr(children[1], 'string'))
197+
return MethodInvocationParams(self.attr(children[0], 'string'),
198+
self.attr(children[1], 'string'))
199199

200200
@deprecated(reason='Use ASTNode functionality instead.')
201-
def get_member_reference_params(self, member_reference_node: int) -> MemberReferenceParams:
202-
assert self.get_type(member_reference_node) == ASTNodeType.MEMBER_REFERENCE
201+
def member_reference_params(self, member_reference_node: int) -> MemberReferenceParams:
202+
assert self.type(member_reference_node) == ASTNodeType.MEMBER_REFERENCE
203203
params = [
204-
self.get_attr(child, 'string') for child in
204+
self.attr(child, 'string') for child in
205205
self.children_with_type(member_reference_node, ASTNodeType.STRING)
206206
]
207207

@@ -225,11 +225,11 @@ def get_member_reference_params(self, member_reference_node: int) -> MemberRefer
225225
return member_reference_params
226226

227227
@deprecated(reason='Use ASTNode functionality instead.')
228-
def get_binary_operation_params(self, binary_operation_node: int) -> BinaryOperationParams:
229-
assert self.get_type(binary_operation_node) == ASTNodeType.BINARY_OPERATION
230-
operation_node, left_side_node, right_side_node = self.tree.succ[binary_operation_node]
228+
def binary_operation_params(self, binary_operation_node: int) -> BinaryOperationParams:
229+
assert self.type(binary_operation_node) == ASTNodeType.BINARY_OPERATION
230+
operation_node, left_side_node, right_side_node = self._tree.succ[binary_operation_node]
231231
return BinaryOperationParams(
232-
self.get_attr(operation_node, 'string'), left_side_node, right_side_node
232+
self.attr(operation_node, 'string'), left_side_node, right_side_node
233233
)
234234

235235
@staticmethod

aibolit/ast_framework/ast_node.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ def line(self) -> int:
7171
def __getattr__(self, attribute_name: str):
7272
node_type = self._get_type(self._node_index)
7373
javalang_fields = attributes_by_node_type[node_type]
74-
computed_fields = computed_fields_registry.get_fields(node_type)
74+
computed_fields = computed_fields_registry.fields(node_type)
7575
if attribute_name not in common_attributes and \
7676
attribute_name not in javalang_fields and \
7777
attribute_name not in computed_fields:
@@ -100,7 +100,7 @@ def __dir__(self) -> List[str]:
100100
return ASTNode._public_fixed_interface + \
101101
list(common_attributes) + \
102102
list(attributes_by_node_type[node_type]) + \
103-
list(computed_fields_registry.get_fields(node_type).keys())
103+
list(computed_fields_registry.fields(node_type).keys())
104104

105105
def __str__(self) -> str:
106106
text_representation = f'node index: {self._node_index}'

aibolit/ast_framework/computed_fields_registry.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def register(
3333

3434
computed_fields[name] = compute_field
3535

36-
def get_fields(
36+
def fields(
3737
self, node_type: 'ASTNodeType'
3838
) -> Dict[str, Callable[['ASTNode'], Any]]:
3939
return self._registry[node_type]

aibolit/ast_framework/java_class_decomposition.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ def find_patterns(tree: AST, patterns: List[Any]) -> Set[str]:
2222
"""
2323

2424
patterns_method_names: Set[str] = set()
25-
for method_declaration in tree.get_root().methods:
26-
method_ast = tree.get_subtree(method_declaration)
25+
for method_declaration in tree.root().methods:
26+
method_ast = tree.subtree(method_declaration)
2727
for pattern in patterns:
2828
if is_ast_pattern(method_ast, pattern):
2929
patterns_method_names.add(method_declaration.name)
@@ -108,7 +108,7 @@ def _create_usage_graph(class_ast: AST) -> DiGraph:
108108
fields_ids: Dict[str, int] = {}
109109
methods_ids: Dict[str, int] = {}
110110

111-
class_declaration = class_ast.get_root()
111+
class_declaration = class_ast.root()
112112

113113
for field_declaration in class_declaration.fields:
114114
# several fields can be declared at one line
@@ -127,7 +127,7 @@ def _create_usage_graph(class_ast: AST) -> DiGraph:
127127
)
128128

129129
for method_declaration in class_declaration.methods:
130-
method_ast = class_ast.get_subtree(method_declaration)
130+
method_ast = class_ast.subtree(method_declaration)
131131

132132
for invoked_method_name in _find_local_method_invocations(method_ast):
133133
if invoked_method_name in methods_ids:
@@ -147,7 +147,7 @@ def _create_usage_graph(class_ast: AST) -> DiGraph:
147147

148148
def _find_local_method_invocations(method_ast: AST) -> Set[str]:
149149
invoked_methods: Set[str] = set()
150-
for method_invocation in method_ast.get_proxy_nodes(ASTNodeType.METHOD_INVOCATION):
150+
for method_invocation in method_ast.proxy_nodes(ASTNodeType.METHOD_INVOCATION):
151151
if method_invocation.qualifier is None:
152152
invoked_methods.add(method_invocation.member)
153153

@@ -156,17 +156,17 @@ def _find_local_method_invocations(method_ast: AST) -> Set[str]:
156156

157157
def _find_fields_usage(method_ast: AST) -> Set[str]:
158158
local_variables: Set[str] = set()
159-
for variable_declaration in method_ast.get_proxy_nodes(
159+
for variable_declaration in method_ast.proxy_nodes(
160160
ASTNodeType.LOCAL_VARIABLE_DECLARATION
161161
):
162162
local_variables.update(variable_declaration.names)
163163

164-
method_declaration = method_ast.get_root()
164+
method_declaration = method_ast.root()
165165
for parameter in method_declaration.parameters:
166166
local_variables.add(parameter.name)
167167

168168
used_fields: Set[str] = set()
169-
for member_reference in method_ast.get_proxy_nodes(ASTNodeType.MEMBER_REFERENCE):
169+
for member_reference in method_ast.proxy_nodes(ASTNodeType.MEMBER_REFERENCE):
170170
if member_reference.qualifier is None and \
171171
member_reference.member not in local_variables:
172172
used_fields.add(member_reference.member)
@@ -179,17 +179,17 @@ def _filter_class_methods_and_fields(
179179
allowed_fields_names: Set[str],
180180
allowed_methods_names: Set[str]
181181
) -> AST:
182-
class_declaration = class_ast.get_root()
182+
class_declaration = class_ast.root()
183183
allowed_nodes = {class_declaration.node_index}
184184

185185
for field_declaration in class_declaration.fields:
186186
if len(allowed_fields_names & set(field_declaration.names)) != 0:
187-
field_ast = class_ast.get_subtree(field_declaration)
187+
field_ast = class_ast.subtree(field_declaration)
188188
allowed_nodes.update(node.node_index for node in field_ast)
189189

190190
for method_declaration in class_declaration.methods:
191191
if method_declaration.name in allowed_methods_names:
192-
method_ast = class_ast.get_subtree(method_declaration)
192+
method_ast = class_ast.subtree(method_declaration)
193193
allowed_nodes.update(node.node_index for node in method_ast)
194194

195-
return AST(class_ast.tree.subgraph(allowed_nodes), class_declaration.node_index)
195+
return AST(class_ast._tree.subgraph(allowed_nodes), class_declaration.node_index)

aibolit/ast_framework/scope.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def __init__(self, scope_tree: DiGraph, scope_id: int):
1818

1919
@staticmethod
2020
def build_from_method_ast(method_ast: AST) -> 'Scope':
21-
method_declaration = method_ast.get_root()
21+
method_declaration = method_ast.root()
2222
assert (
2323
method_declaration.node_type == ASTNodeType.METHOD_DECLARATION
2424
), 'Building scopes tree supported only for method declaration.'

0 commit comments

Comments
 (0)