| Total Complexity | 3 |
| Total Lines | 35 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
| 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) |
||
|
|
|||
| 25 | type_chances[STR] += type_usage_patterns.used_as_str(statement, variable) |
||
| 26 | type_chances[LIST] += type_usage_patterns.used_as_list(statement, variable) |
||
| 27 | type_chances[TUPLE] += type_usage_patterns.used_as_tuple(statement, variable) |
||
| 28 | type_chances[DICT] += type_usage_patterns.used_as_dict(statement, variable) |
||
| 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 |