|
1 | 1 | from collections import OrderedDict |
2 | | -from typing import TYPE_CHECKING, Any, Dict, Iterable, Iterator, List, Optional, Set, Tuple, Union |
| 2 | +from typing import TYPE_CHECKING, Any, Dict, Iterable, Iterator, List, Optional, Set, Union |
3 | 3 |
|
4 | 4 | from django.db.models.fields import Field |
5 | 5 | from django.db.models.fields.related import RelatedField |
|
10 | 10 | from mypy.nodes import ( |
11 | 11 | GDEF, |
12 | 12 | MDEF, |
13 | | - Argument, |
14 | 13 | Block, |
15 | 14 | ClassDef, |
16 | 15 | Expression, |
17 | | - FuncDef, |
18 | 16 | MemberExpr, |
19 | 17 | MypyFile, |
20 | 18 | NameExpr, |
|
34 | 32 | MethodContext, |
35 | 33 | SemanticAnalyzerPluginInterface, |
36 | 34 | ) |
37 | | -from mypy.plugins.common import add_method_to_class |
38 | 35 | from mypy.semanal import SemanticAnalyzer |
39 | | -from mypy.types import AnyType, CallableType, Instance, NoneTyp, TupleType |
| 36 | +from mypy.types import AnyType, Instance, NoneTyp, TupleType |
40 | 37 | from mypy.types import Type as MypyType |
41 | | -from mypy.types import TypedDictType, TypeOfAny, UnboundType, UnionType |
| 38 | +from mypy.types import TypedDictType, TypeOfAny, UnionType |
42 | 39 |
|
43 | 40 | from mypy_django_plugin.lib import fullnames |
44 | 41 | from mypy_django_plugin.lib.fullnames import WITH_ANNOTATIONS_FULLNAME |
@@ -361,86 +358,6 @@ def add_new_sym_for_info(info: TypeInfo, *, name: str, sym_type: MypyType, no_se |
361 | 358 | info.names[name] = SymbolTableNode(MDEF, var, plugin_generated=True, no_serialize=no_serialize) |
362 | 359 |
|
363 | 360 |
|
364 | | -def build_unannotated_method_args(method_node: FuncDef) -> Tuple[List[Argument], MypyType]: |
365 | | - prepared_arguments = [] |
366 | | - try: |
367 | | - arguments = method_node.arguments[1:] |
368 | | - except AttributeError: |
369 | | - arguments = [] |
370 | | - for argument in arguments: |
371 | | - argument.type_annotation = AnyType(TypeOfAny.unannotated) |
372 | | - prepared_arguments.append(argument) |
373 | | - return_type = AnyType(TypeOfAny.unannotated) |
374 | | - return prepared_arguments, return_type |
375 | | - |
376 | | - |
377 | | -def bind_or_analyze_type(t: MypyType, api: SemanticAnalyzer, module_name: Optional[str] = None) -> Optional[MypyType]: |
378 | | - """Analyze a type. If an unbound type, try to look it up in the given module name. |
379 | | -
|
380 | | - That should hopefully give a bound type.""" |
381 | | - if isinstance(t, UnboundType) and module_name is not None: |
382 | | - node = api.lookup_fully_qualified_or_none(module_name + "." + t.name) |
383 | | - if node is not None and node.type is not None: |
384 | | - return node.type |
385 | | - |
386 | | - return api.anal_type(t) |
387 | | - |
388 | | - |
389 | | -def copy_method_to_another_class( |
390 | | - api: SemanticAnalyzer, |
391 | | - cls: ClassDef, |
392 | | - self_type: Instance, |
393 | | - new_method_name: str, |
394 | | - method_node: FuncDef, |
395 | | - return_type: Optional[MypyType] = None, |
396 | | - original_module_name: Optional[str] = None, |
397 | | -) -> bool: |
398 | | - if method_node.type is None: |
399 | | - arguments, return_type = build_unannotated_method_args(method_node) |
400 | | - add_method_to_class(api, cls, new_method_name, args=arguments, return_type=return_type, self_type=self_type) |
401 | | - return True |
402 | | - |
403 | | - method_type = method_node.type |
404 | | - if not isinstance(method_type, CallableType): |
405 | | - if not api.final_iteration: |
406 | | - api.defer() |
407 | | - return False |
408 | | - |
409 | | - if return_type is None: |
410 | | - return_type = bind_or_analyze_type(method_type.ret_type, api, original_module_name) |
411 | | - if return_type is None: |
412 | | - return False |
413 | | - |
414 | | - # We build the arguments from the method signature (`CallableType`), because if we were to |
415 | | - # use the arguments from the method node (`FuncDef.arguments`) we're not compatible with |
416 | | - # a method loaded from cache. As mypy doesn't serialize `FuncDef.arguments` when caching |
417 | | - arguments = [] |
418 | | - # Note that the first argument is excluded, as that's `self` |
419 | | - for pos, (arg_type, arg_kind, arg_name) in enumerate( |
420 | | - zip(method_type.arg_types[1:], method_type.arg_kinds[1:], method_type.arg_names[1:]), |
421 | | - start=1, |
422 | | - ): |
423 | | - bound_arg_type = bind_or_analyze_type(arg_type, api, original_module_name) |
424 | | - if bound_arg_type is None: |
425 | | - return False |
426 | | - if arg_name is None and hasattr(method_node, "arguments"): |
427 | | - arg_name = method_node.arguments[pos].variable.name |
428 | | - arguments.append( |
429 | | - Argument( |
430 | | - # Positional only arguments can have name as `None`, if we can't find a name, we just invent one.. |
431 | | - variable=Var(name=arg_name if arg_name is not None else str(pos), type=arg_type), |
432 | | - type_annotation=bound_arg_type, |
433 | | - initializer=None, |
434 | | - kind=arg_kind, |
435 | | - pos_only=arg_name is None, |
436 | | - ) |
437 | | - ) |
438 | | - |
439 | | - add_method_to_class(api, cls, new_method_name, args=arguments, return_type=return_type, self_type=self_type) |
440 | | - |
441 | | - return True |
442 | | - |
443 | | - |
444 | 361 | def add_new_manager_base(api: SemanticAnalyzerPluginInterface, fullname: str) -> None: |
445 | 362 | sym = api.lookup_fully_qualified_or_none(fullnames.MANAGER_CLASS_FULLNAME) |
446 | 363 | if sym is not None and isinstance(sym.node, TypeInfo): |
|
0 commit comments