fastest.type.type_inference   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 3
eloc 23
dl 0
loc 35
rs 10
c 0
b 0
f 0

1 Function

Rating   Name   Duplication   Size   Complexity  
A infer() 0 23 3
1
from fastest.type import type_usage_patterns
2
3
4
INT         = 0
5
STR         = 1
6
LIST        = 2
7
TUPLE       = 3
8
DICT        = 4
9
type_map    = ['int', 'str', 'list', 'tuple', 'dict']
10
11
12
def infer(variable, statements):
13
    """
14
    example: infer("list_var", "def fn():\n\tlist_var = [1]") -> ['list'] #
15
    example: infer("some_var", "def fn():\n\tsome_var + some_other") -> ['int', 'str'] #
16
    :param variable:
17
    :param statements:
18
    :return:
19
    """
20
    statements = statements.split('\n')
21
    statements = statements[1:]
22
    type_chances = [0] * 5
23
    for statement in statements:
24
        type_chances[INT]   += type_usage_patterns.used_as_int(statement, variable)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable INT does not seem to be defined.
Loading history...
25
        type_chances[STR]   += type_usage_patterns.used_as_str(statement, variable)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable STR does not seem to be defined.
Loading history...
26
        type_chances[LIST]  += type_usage_patterns.used_as_list(statement, variable)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable LIST does not seem to be defined.
Loading history...
27
        type_chances[TUPLE] += type_usage_patterns.used_as_tuple(statement, variable)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable TUPLE does not seem to be defined.
Loading history...
28
        type_chances[DICT]  += type_usage_patterns.used_as_dict(statement, variable)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable DICT does not seem to be defined.
Loading history...
29
30
    max_prob_type = max(type_chances)
31
    if type_chances.count(max_prob_type) > 1:
32
        return [type_map[i] for i, type_chance in enumerate(type_chances) if type_chance == max_prob_type]
33
    else:
34
        return [type_map[type_chances.index(max_prob_type)]]
35