mirror of
https://github.com/davidhalter/django-stubs.git
synced 2025-12-11 06:21:58 +08:00
lint fixes
This commit is contained in:
@@ -1,10 +1,16 @@
|
||||
from typing import OrderedDict, List, Optional, Dict, Set, Union
|
||||
from typing import Dict, List, Optional, Set, Union
|
||||
|
||||
from mypy import checker
|
||||
from mypy.checker import TypeChecker
|
||||
from mypy.nodes import MypyFile, TypeInfo, Var, MDEF, SymbolTableNode, GDEF, Expression
|
||||
from mypy.plugin import CheckerPluginInterface, FunctionContext, MethodContext, AttributeContext
|
||||
from mypy.types import Type as MypyType, Instance, TupleType, TypeOfAny, AnyType, TypedDictType
|
||||
from mypy.nodes import (
|
||||
GDEF, MDEF, Expression, MypyFile, SymbolTableNode, TypeInfo, Var,
|
||||
)
|
||||
from mypy.plugin import (
|
||||
AttributeContext, CheckerPluginInterface, FunctionContext, MethodContext,
|
||||
)
|
||||
from mypy.types import AnyType, Instance, TupleType
|
||||
from mypy.types import Type as MypyType
|
||||
from mypy.types import TypedDictType, TypeOfAny
|
||||
|
||||
from mypy_django_plugin.lib import helpers
|
||||
|
||||
@@ -49,10 +55,12 @@ def make_tuple(api: 'TypeChecker', fields: List[MypyType]) -> TupleType:
|
||||
return TupleType(fields, fallback=fallback)
|
||||
|
||||
|
||||
def make_oneoff_typeddict(api: CheckerPluginInterface, fields: 'OrderedDict[str, MypyType]',
|
||||
def make_oneoff_typeddict(api: CheckerPluginInterface, fields: 'Dict[str, MypyType]',
|
||||
required_keys: Set[str]) -> TypedDictType:
|
||||
object_type = api.named_generic_type('mypy_extensions._TypedDict', [])
|
||||
typed_dict_type = TypedDictType(fields, required_keys=required_keys, fallback=object_type)
|
||||
typed_dict_type = TypedDictType(fields, # type: ignore
|
||||
required_keys=required_keys,
|
||||
fallback=object_type)
|
||||
return typed_dict_type
|
||||
|
||||
|
||||
|
||||
@@ -2,20 +2,20 @@ from typing import (
|
||||
TYPE_CHECKING, Any, Dict, Iterable, Iterator, List, Optional, Union,
|
||||
)
|
||||
|
||||
from django.db.models.fields import Field
|
||||
from django.db.models.fields.related import RelatedField
|
||||
from django.db.models.fields.reverse_related import ForeignObjectRel
|
||||
from mypy.checker import TypeChecker
|
||||
from mypy.mro import calculate_mro
|
||||
from mypy.nodes import (
|
||||
Block, ClassDef, Expression, MemberExpr, MypyFile, NameExpr, StrExpr, SymbolNode,
|
||||
SymbolTable, SymbolTableNode, TypeInfo, Var,
|
||||
Block, ClassDef, Expression, MemberExpr, MypyFile, NameExpr, StrExpr, SymbolNode, SymbolTable, SymbolTableNode,
|
||||
TypeInfo, Var,
|
||||
)
|
||||
from mypy.semanal import SemanticAnalyzer
|
||||
from mypy.types import AnyType, Instance, NoneTyp
|
||||
from mypy.types import Type as MypyType
|
||||
from mypy.types import TypeOfAny, UnionType
|
||||
|
||||
from django.db.models.fields import Field
|
||||
from mypy_django_plugin.lib import fullnames
|
||||
|
||||
if TYPE_CHECKING:
|
||||
@@ -50,7 +50,8 @@ def lookup_fully_qualified_sym(fullname: str, all_modules: Dict[str, MypyFile])
|
||||
# nested class
|
||||
for parent_cls_name in parent_cls_name.split('.'):
|
||||
sym = sym_table.get(parent_cls_name)
|
||||
if sym is None:
|
||||
if (sym is None or sym.node is None
|
||||
or not isinstance(sym.node, TypeInfo)):
|
||||
return None
|
||||
sym_table = sym.node.names
|
||||
|
||||
|
||||
@@ -1,11 +1,12 @@
|
||||
from typing import Union, Tuple, List, Optional, NamedTuple, cast
|
||||
from typing import List, NamedTuple, Optional, Tuple, Union, cast
|
||||
|
||||
from mypy.nodes import Argument, FuncDef, Var, TypeInfo
|
||||
from mypy.plugin import DynamicClassDefContext, ClassDefContext
|
||||
from mypy.nodes import Argument, FuncDef, TypeInfo, Var
|
||||
from mypy.plugin import ClassDefContext, DynamicClassDefContext
|
||||
from mypy.plugins.common import add_method
|
||||
from mypy.semanal import SemanticAnalyzer
|
||||
from mypy.types import Instance, CallableType, AnyType, TypeOfAny, PlaceholderType
|
||||
from mypy.types import AnyType, CallableType, Instance
|
||||
from mypy.types import Type as MypyType
|
||||
from mypy.types import TypeOfAny
|
||||
|
||||
|
||||
class IncompleteDefnException(Exception):
|
||||
@@ -39,7 +40,7 @@ def prepare_unannotated_method_signature(method_node: FuncDef) -> Tuple[List[Arg
|
||||
|
||||
|
||||
class SignatureTuple(NamedTuple):
|
||||
arguments: Optional[List[Argument]]
|
||||
arguments: List[Argument]
|
||||
return_type: Optional[MypyType]
|
||||
cannot_be_bound: bool
|
||||
|
||||
@@ -53,16 +54,14 @@ def analyze_callable_signature(api: SemanticAnalyzer, method_node: FuncDef) -> S
|
||||
for arg_name, arg_type, original_argument in zip(method_type.arg_names[1:],
|
||||
method_type.arg_types[1:],
|
||||
method_node.arguments[1:]):
|
||||
arg_type = api.anal_type(arg_type, allow_placeholder=True)
|
||||
if isinstance(arg_type, PlaceholderType):
|
||||
analyzed_arg_type = api.anal_type(arg_type)
|
||||
if analyzed_arg_type is None:
|
||||
unbound = True
|
||||
|
||||
var = Var(name=original_argument.variable.name,
|
||||
type=arg_type)
|
||||
type=analyzed_arg_type)
|
||||
var.set_line(original_argument.variable)
|
||||
|
||||
if isinstance(arg_type, PlaceholderType):
|
||||
unbound = True
|
||||
argument = Argument(variable=var,
|
||||
type_annotation=arg_type,
|
||||
initializer=original_argument.initializer,
|
||||
@@ -70,10 +69,10 @@ def analyze_callable_signature(api: SemanticAnalyzer, method_node: FuncDef) -> S
|
||||
argument.set_line(original_argument)
|
||||
arguments.append(argument)
|
||||
|
||||
ret_type = api.anal_type(method_type.ret_type, allow_placeholder=True)
|
||||
if isinstance(ret_type, PlaceholderType):
|
||||
analyzed_ret_type = api.anal_type(method_type.ret_type)
|
||||
if analyzed_ret_type is None:
|
||||
unbound = True
|
||||
return SignatureTuple(arguments, ret_type, unbound)
|
||||
return SignatureTuple(arguments, analyzed_ret_type, unbound)
|
||||
|
||||
|
||||
def copy_method_or_incomplete_defn_exception(ctx: ClassDefContext,
|
||||
@@ -103,15 +102,17 @@ def copy_method_or_incomplete_defn_exception(ctx: ClassDefContext,
|
||||
and name not in semanal_api.cur_mod_node.names):
|
||||
semanal_api.add_imported_symbol(name, sym, context=semanal_api.cur_mod_node)
|
||||
|
||||
arguments, return_type, unbound = analyze_callable_signature(semanal_api, method_node)
|
||||
arguments, analyzed_return_type, unbound = analyze_callable_signature(semanal_api, method_node)
|
||||
assert len(arguments) + 1 == len(method_node.arguments)
|
||||
if unbound:
|
||||
raise IncompleteDefnException(f'Signature of method {method_node.fullname!r} is not ready')
|
||||
|
||||
assert analyzed_return_type is not None
|
||||
|
||||
if new_method_name in ctx.cls.info.names:
|
||||
del ctx.cls.info.names[new_method_name]
|
||||
add_method(ctx,
|
||||
new_method_name,
|
||||
args=arguments,
|
||||
return_type=return_type,
|
||||
return_type=analyzed_return_type,
|
||||
self_type=self_type)
|
||||
|
||||
Reference in New Issue
Block a user