mirror of
https://github.com/davidhalter/django-stubs.git
synced 2026-05-16 21:45:05 +08:00
new semanal wip 1
This commit is contained in:
@@ -1,13 +1,13 @@
|
||||
from typing import Optional, cast
|
||||
|
||||
from mypy.checker import TypeChecker
|
||||
from mypy.nodes import ListExpr, NameExpr, StrExpr, TupleExpr, TypeInfo
|
||||
from mypy.nodes import ListExpr, NameExpr, StrExpr, TupleExpr, TypeInfo, Expression
|
||||
from mypy.plugin import FunctionContext
|
||||
from mypy.types import (
|
||||
AnyType, CallableType, Instance, TupleType, Type, UnionType,
|
||||
)
|
||||
|
||||
from mypy_django_plugin.lib import metadata, fullnames, helpers
|
||||
from mypy_django_plugin.lib import fullnames, helpers, metadata
|
||||
|
||||
|
||||
def extract_referred_to_type(ctx: FunctionContext) -> Optional[Instance]:
|
||||
@@ -90,7 +90,7 @@ def fill_descriptor_types_for_related_field(ctx: FunctionContext) -> Type:
|
||||
|
||||
def set_descriptor_types_for_field(ctx: FunctionContext) -> Instance:
|
||||
default_return_type = cast(Instance, ctx.default_return_type)
|
||||
is_nullable = helpers.parse_bool(helpers.get_argument_by_name(ctx, 'null'))
|
||||
is_nullable = helpers.parse_bool(helpers.get_call_argument_by_name(ctx, 'null'))
|
||||
set_type = helpers.get_private_descriptor_type(default_return_type.type, '_pyi_private_set_type',
|
||||
is_nullable=is_nullable)
|
||||
get_type = helpers.get_private_descriptor_type(default_return_type.type, '_pyi_private_get_type',
|
||||
@@ -101,7 +101,7 @@ def set_descriptor_types_for_field(ctx: FunctionContext) -> Instance:
|
||||
def determine_type_of_array_field(ctx: FunctionContext) -> Type:
|
||||
default_return_type = set_descriptor_types_for_field(ctx)
|
||||
|
||||
base_field_arg_type = helpers.get_argument_type_by_name(ctx, 'base_field')
|
||||
base_field_arg_type = helpers.get_call_argument_type_by_name(ctx, 'base_field')
|
||||
if not base_field_arg_type or not isinstance(base_field_arg_type, Instance):
|
||||
return default_return_type
|
||||
|
||||
@@ -118,9 +118,7 @@ def transform_into_proper_return_type(ctx: FunctionContext) -> Type:
|
||||
if not isinstance(default_return_type, Instance):
|
||||
return default_return_type
|
||||
|
||||
if helpers.has_any_of_bases(default_return_type.type, (fullnames.FOREIGN_KEY_FULLNAME,
|
||||
fullnames.ONETOONE_FIELD_FULLNAME,
|
||||
fullnames.MANYTOMANY_FIELD_FULLNAME)):
|
||||
if helpers.has_any_of_bases(default_return_type.type, fullnames.RELATED_FIELDS_CLASSES):
|
||||
return fill_descriptor_types_for_related_field(ctx)
|
||||
|
||||
if default_return_type.type.has_base(fullnames.ARRAY_FIELD_FULLNAME):
|
||||
@@ -135,55 +133,99 @@ def process_field_instantiation(ctx: FunctionContext) -> Type:
|
||||
return transform_into_proper_return_type(ctx)
|
||||
|
||||
|
||||
def _parse_choices_type(ctx: FunctionContext, choices_arg: Expression) -> Optional[str]:
|
||||
if isinstance(choices_arg, (TupleExpr, ListExpr)):
|
||||
# iterable of 2 element tuples of two kinds
|
||||
_, analyzed_choices = ctx.api.analyze_iterable_item_type(choices_arg)
|
||||
if isinstance(analyzed_choices, TupleType):
|
||||
first_element_type = analyzed_choices.items[0]
|
||||
if isinstance(first_element_type, Instance):
|
||||
return first_element_type.type.fullname()
|
||||
|
||||
|
||||
def _parse_referenced_model(ctx: FunctionContext, to_arg: Expression) -> Optional[TypeInfo]:
|
||||
if isinstance(to_arg, NameExpr) and isinstance(to_arg.node, TypeInfo):
|
||||
# reference to the model class
|
||||
return to_arg.node
|
||||
|
||||
elif isinstance(to_arg, StrExpr):
|
||||
referenced_model_info = helpers.get_model_info(to_arg.value, ctx.api.modules)
|
||||
if referenced_model_info is not None:
|
||||
return referenced_model_info
|
||||
|
||||
|
||||
def parse_field_init_arguments_into_model_metadata(ctx: FunctionContext) -> None:
|
||||
api = cast(TypeChecker, ctx.api)
|
||||
outer_model = api.scope.active_class()
|
||||
outer_model = ctx.api.scope.active_class()
|
||||
if outer_model is None or not outer_model.has_base(fullnames.MODEL_CLASS_FULLNAME):
|
||||
# outside models.Model class, undetermined
|
||||
return
|
||||
|
||||
field_name = None
|
||||
for name_expr, stmt in helpers.iter_over_assignments(outer_model.defn):
|
||||
if stmt == ctx.context and isinstance(name_expr, NameExpr):
|
||||
field_name = name_expr.name
|
||||
# Determine name of the current field
|
||||
for attr_name, stmt in helpers.iter_over_class_level_assignments(outer_model.defn):
|
||||
if stmt == ctx.context:
|
||||
field_name = attr_name
|
||||
break
|
||||
if field_name is None:
|
||||
else:
|
||||
return
|
||||
|
||||
fields_metadata = metadata.get_fields_metadata(outer_model)
|
||||
model_fields_metadata = metadata.get_fields_metadata(outer_model)
|
||||
|
||||
# primary key
|
||||
is_primary_key = False
|
||||
primary_key_arg = helpers.get_argument_by_name(ctx, 'primary_key')
|
||||
primary_key_arg = helpers.get_call_argument_by_name(ctx, 'primary_key')
|
||||
if primary_key_arg:
|
||||
is_primary_key = helpers.parse_bool(primary_key_arg)
|
||||
fields_metadata[field_name] = {'primary_key': is_primary_key}
|
||||
model_fields_metadata[field_name] = {'primary_key': is_primary_key}
|
||||
|
||||
# choices
|
||||
choices_arg = helpers.get_argument_by_name(ctx, 'choices')
|
||||
if choices_arg and isinstance(choices_arg, (TupleExpr, ListExpr)):
|
||||
# iterable of 2 element tuples of two kinds
|
||||
_, analyzed_choices = api.analyze_iterable_item_type(choices_arg)
|
||||
if isinstance(analyzed_choices, TupleType):
|
||||
first_element_type = analyzed_choices.items[0]
|
||||
if isinstance(first_element_type, Instance):
|
||||
fields_metadata[field_name]['choices'] = first_element_type.type.fullname()
|
||||
choices_arg = helpers.get_call_argument_by_name(ctx, 'choices')
|
||||
if choices_arg:
|
||||
choices_type_fullname = _parse_choices_type(ctx.api, choices_arg)
|
||||
if choices_type_fullname:
|
||||
model_fields_metadata[field_name]['choices_type'] = choices_type_fullname
|
||||
|
||||
# nullability
|
||||
null_arg = helpers.get_argument_by_name(ctx, 'null')
|
||||
null_arg = helpers.get_call_argument_by_name(ctx, 'null')
|
||||
is_nullable = False
|
||||
if null_arg:
|
||||
is_nullable = helpers.parse_bool(null_arg)
|
||||
fields_metadata[field_name]['null'] = is_nullable
|
||||
model_fields_metadata[field_name]['null'] = is_nullable
|
||||
|
||||
# is_blankable
|
||||
blank_arg = helpers.get_argument_by_name(ctx, 'blank')
|
||||
blank_arg = helpers.get_call_argument_by_name(ctx, 'blank')
|
||||
is_blankable = False
|
||||
if blank_arg:
|
||||
is_blankable = helpers.parse_bool(blank_arg)
|
||||
fields_metadata[field_name]['blank'] = is_blankable
|
||||
model_fields_metadata[field_name]['blank'] = is_blankable
|
||||
|
||||
# default
|
||||
default_arg = helpers.get_argument_by_name(ctx, 'default')
|
||||
default_arg = helpers.get_call_argument_by_name(ctx, 'default')
|
||||
if default_arg and not helpers.is_none_expr(default_arg):
|
||||
fields_metadata[field_name]['default_specified'] = True
|
||||
model_fields_metadata[field_name]['default_specified'] = True
|
||||
|
||||
if helpers.has_any_of_bases(ctx.default_return_type.type, fullnames.RELATED_FIELDS_CLASSES):
|
||||
# to
|
||||
to_arg = helpers.get_call_argument_by_name(ctx, 'to')
|
||||
if to_arg:
|
||||
referenced_model = _parse_referenced_model(ctx, to_arg)
|
||||
if referenced_model is not None:
|
||||
model_fields_metadata[field_name]['to'] = referenced_model.fullname()
|
||||
else:
|
||||
model_fields_metadata[field_name]['to'] = to_arg.value
|
||||
# referenced_model = to_arg.value
|
||||
# raise helpers.IncompleteDefnException()
|
||||
|
||||
# model_fields_metadata[field_name]['to'] = referenced_model.fullname()
|
||||
# if referenced_model is not None:
|
||||
# model_fields_metadata[field_name]['to'] = referenced_model.fullname()
|
||||
# else:
|
||||
# assert isinstance(to_arg, StrExpr)
|
||||
# model_fields_metadata[field_name]['to'] = to_arg.value
|
||||
|
||||
# related_name
|
||||
related_name_arg = helpers.get_call_argument_by_name(ctx, 'related_name')
|
||||
if related_name_arg:
|
||||
if isinstance(related_name_arg, StrExpr):
|
||||
model_fields_metadata[field_name]['related_name'] = related_name_arg.value
|
||||
else:
|
||||
model_fields_metadata[field_name]['related_name'] = outer_model.name().lower() + '_set'
|
||||
|
||||
Reference in New Issue
Block a user