|
1 | 1 | import typing
|
2 | 2 | from typing import Dict, Optional
|
3 | 3 |
|
4 |
| -from mypy.nodes import Expression, FuncDef, ImportedName, MypyFile, NameExpr, SymbolNode, TypeInfo, Var, AssignmentStmt, \ |
5 |
| - CallExpr |
| 4 | +from mypy.nodes import Expression, ImportedName, MypyFile, NameExpr, SymbolNode, TypeInfo |
6 | 5 | from mypy.plugin import FunctionContext
|
7 |
| -from mypy.types import AnyType, CallableType, Instance, Type, TypeOfAny, TypeVarType, UnionType |
| 6 | +from mypy.types import AnyType, Instance, Type, TypeOfAny, TypeVarType |
8 | 7 |
|
9 | 8 | MODEL_CLASS_FULLNAME = 'django.db.models.base.Model'
|
10 | 9 | FIELD_FULLNAME = 'django.db.models.fields.Field'
|
@@ -119,74 +118,6 @@ def fill_typevars(tp: Instance, type_to_fill: Instance) -> Instance:
|
119 | 118 | return reparametrize_with(type_to_fill, typevar_values)
|
120 | 119 |
|
121 | 120 |
|
122 |
| -def extract_field_setter_type(tp: Instance) -> Optional[Type]: |
123 |
| - if tp.type.has_base(FIELD_FULLNAME): |
124 |
| - set_method = tp.type.get_method('__set__') |
125 |
| - if isinstance(set_method, FuncDef) and isinstance(set_method.type, CallableType): |
126 |
| - if 'value' in set_method.type.arg_names: |
127 |
| - set_value_type = set_method.type.arg_types[set_method.type.arg_names.index('value')] |
128 |
| - if isinstance(set_value_type, Instance): |
129 |
| - set_value_type = fill_typevars(tp, set_value_type) |
130 |
| - return set_value_type |
131 |
| - elif isinstance(set_value_type, UnionType): |
132 |
| - items_no_typevars = [] |
133 |
| - for item in set_value_type.items: |
134 |
| - if isinstance(item, Instance): |
135 |
| - item = fill_typevars(tp, item) |
136 |
| - items_no_typevars.append(item) |
137 |
| - return UnionType(items_no_typevars) |
138 |
| - |
139 |
| - get_method = tp.type.get_method('__get__') |
140 |
| - if isinstance(get_method, FuncDef) and isinstance(get_method.type, CallableType): |
141 |
| - return get_method.type.ret_type |
142 |
| - # GenericForeignKey |
143 |
| - if tp.type.has_base(GENERIC_FOREIGN_KEY_FULLNAME): |
144 |
| - return AnyType(TypeOfAny.special_form) |
145 |
| - return None |
146 |
| - |
147 |
| - |
148 |
| -def extract_primary_key_type(model: TypeInfo) -> Optional[Type]: |
149 |
| - # only primary keys defined in current class for now |
150 |
| - for stmt in model.defn.defs.body: |
151 |
| - if isinstance(stmt, AssignmentStmt) and isinstance(stmt.rvalue, CallExpr): |
152 |
| - name_expr = stmt.lvalues[0] |
153 |
| - if isinstance(name_expr, NameExpr): |
154 |
| - name = name_expr.name |
155 |
| - if 'primary_key' in stmt.rvalue.arg_names: |
156 |
| - is_primary_key = stmt.rvalue.args[stmt.rvalue.arg_names.index('primary_key')] |
157 |
| - if is_primary_key: |
158 |
| - return extract_field_setter_type(model.names[name].type) |
159 |
| - return None |
160 |
| - |
161 |
| - |
162 |
| -def extract_expected_types(ctx: FunctionContext, model: TypeInfo) -> Dict[str, Type]: |
163 |
| - expected_types: Dict[str, Type] = {} |
164 |
| - |
165 |
| - primary_key_type = extract_primary_key_type(model) |
166 |
| - if not primary_key_type: |
167 |
| - # no explicit primary key, set pk to Any and add id |
168 |
| - primary_key_type = AnyType(TypeOfAny.special_form) |
169 |
| - expected_types['id'] = ctx.api.named_generic_type('builtins.int', []) |
170 |
| - |
171 |
| - expected_types['pk'] = primary_key_type |
172 |
| - |
173 |
| - for base in model.mro: |
174 |
| - for name, sym in base.names.items(): |
175 |
| - if isinstance(sym.node, Var) and isinstance(sym.node.type, Instance): |
176 |
| - tp = sym.node.type |
177 |
| - field_type = extract_field_setter_type(tp) |
178 |
| - if tp.type.fullname() in {FOREIGN_KEY_FULLNAME, ONETOONE_FIELD_FULLNAME}: |
179 |
| - ref_to_model = tp.args[0] |
180 |
| - if isinstance(ref_to_model, Instance) and ref_to_model.type.has_base(MODEL_CLASS_FULLNAME): |
181 |
| - primary_key_type = extract_primary_key_type(ref_to_model.type) |
182 |
| - if not primary_key_type: |
183 |
| - primary_key_type = AnyType(TypeOfAny.special_form) |
184 |
| - expected_types[name + '_id'] = primary_key_type |
185 |
| - if field_type: |
186 |
| - expected_types[name] = field_type |
187 |
| - return expected_types |
188 |
| - |
189 |
| - |
190 | 121 | def get_argument_by_name(ctx: FunctionContext, name: str) -> Optional[Expression]:
|
191 | 122 | """Return the expression for the specific argument.
|
192 | 123 |
|
|
0 commit comments