Skip to content

Commit

Permalink
Remove warning about unsupported expression types (typeddjango#266)
Browse files Browse the repository at this point in the history
* remove warning about unsupported expression for _meta.get_field()

* lint
  • Loading branch information
mkurnikov authored Dec 12, 2019
1 parent 31e7950 commit 5832605
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 12 deletions.
5 changes: 1 addition & 4 deletions mypy_django_plugin/lib/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,8 +259,7 @@ def make_typeddict(api: CheckerPluginInterface, fields: 'OrderedDict[str, MypyTy
return typed_dict_type


def resolve_string_attribute_value(attr_expr: Expression, ctx: Union[FunctionContext, MethodContext],
django_context: 'DjangoContext') -> Optional[str]:
def resolve_string_attribute_value(attr_expr: Expression, django_context: 'DjangoContext') -> Optional[str]:
if isinstance(attr_expr, StrExpr):
return attr_expr.value

Expand All @@ -270,8 +269,6 @@ def resolve_string_attribute_value(attr_expr: Expression, ctx: Union[FunctionCon
if isinstance(attr_expr.expr, NameExpr) and attr_expr.expr.fullname == 'django.conf.settings':
if hasattr(django_context.settings, member_name):
return getattr(django_context.settings, member_name)

ctx.api.fail(f'Expression of type {type(attr_expr).__name__!r} is not supported', ctx.context)
return None


Expand Down
2 changes: 1 addition & 1 deletion mypy_django_plugin/transformers/meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def return_proper_field_type_from_get_field(ctx: MethodContext, django_context:
if field_name_expr is None:
return ctx.default_return_type

field_name = helpers.resolve_string_attribute_value(field_name_expr, ctx, django_context)
field_name = helpers.resolve_string_attribute_value(field_name_expr, django_context)
if field_name is None:
return ctx.default_return_type

Expand Down
11 changes: 5 additions & 6 deletions mypy_django_plugin/transformers/querysets.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from collections import OrderedDict
from typing import List, Optional, Sequence, Type, Union
from typing import List, Optional, Sequence, Type

from django.core.exceptions import FieldError
from django.db.models.base import Model
Expand Down Expand Up @@ -62,7 +62,7 @@ def get_field_type_from_lookup(ctx: MethodContext, django_context: DjangoContext

def get_values_list_row_type(ctx: MethodContext, django_context: DjangoContext, model_cls: Type[Model],
flat: bool, named: bool) -> MypyType:
field_lookups = resolve_field_lookups(ctx.args[0], ctx, django_context)
field_lookups = resolve_field_lookups(ctx.args[0], django_context)
if field_lookups is None:
return AnyType(TypeOfAny.from_error)

Expand Down Expand Up @@ -148,11 +148,10 @@ def extract_proper_type_queryset_values_list(ctx: MethodContext, django_context:
return helpers.reparametrize_instance(ctx.default_return_type, [model_type, row_type])


def resolve_field_lookups(lookup_exprs: Sequence[Expression], ctx: Union[FunctionContext, MethodContext],
django_context: DjangoContext) -> Optional[List[str]]:
def resolve_field_lookups(lookup_exprs: Sequence[Expression], django_context: DjangoContext) -> Optional[List[str]]:
field_lookups = []
for field_lookup_expr in lookup_exprs:
field_lookup = helpers.resolve_string_attribute_value(field_lookup_expr, ctx, django_context)
field_lookup = helpers.resolve_string_attribute_value(field_lookup_expr, django_context)
if field_lookup is None:
return None
field_lookups.append(field_lookup)
Expand All @@ -172,7 +171,7 @@ def extract_proper_type_queryset_values(ctx: MethodContext, django_context: Djan
if model_cls is None:
return ctx.default_return_type

field_lookups = resolve_field_lookups(ctx.args[0], ctx, django_context)
field_lookups = resolve_field_lookups(ctx.args[0], django_context)
if field_lookups is None:
return AnyType(TypeOfAny.from_error)

Expand Down
1 change: 0 additions & 1 deletion scripts/enabled_test_modules.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
re.compile(r'has no attribute ("|\')_[a-zA-Z_]+("|\')'),
"'Settings' object has no attribute",
'**Dict',
re.compile(r"Expression of type '.*' is not supported"),
'has incompatible type "object"',
'undefined in superclass',
'Argument after ** must be a mapping',
Expand Down

0 comments on commit 5832605

Please sign in to comment.