djangorestframework-stubs icon indicating copy to clipboard operation
djangorestframework-stubs copied to clipboard

Installing stubs breaks many mypy rules

Open dms-cat opened this issue 5 years ago • 3 comments

How to reproduce:

  1. Configure mypy like this:

    [mypy]
    check_untyped_defs = true
    disallow_untyped_defs = true
    ignore_missing_imports = true
    no_implicit_optional = true
    warn_redundant_casts = true
    warn_return_any = true
    warn_unused_ignores = true
    
  2. Ensure that mypy produces no errors

  3. Install djangorestframework-stubs

  4. Run mypy again

Expected results

No change in mypy output.

Actual results

Hundreds of errors; grep -o 'error:.*' errors.log | sort -u | wc -l prints "258". A quick categorization:

  • Argument [N] of "[method name]" incompatible with supertype "[class, for example 'CreateModelMixin' or 'UpdateModelMixin']"
  • "[App model class]" has no attribute "[attribute name, including 'id']"
  • Invalid index type "str" for "str"; expected type "Union[int, slice]"
  • "LazySettings" has no attribute "[every setting variable, for example AUTH_USER_MODEL, DEBUG]"
  • "Model" has no attribute "objects"
  • "ModelViewSet" has no attribute "action"
  • Need type annotation for '[field name]'
  • Too many arguments for "as_view" of "ViewSetMixin"
  • Unsupported operand types for + ("object" and "timedelta")

Are these stubs generated from the DRF code? Maybe it would be possible to do so, or at least get the IDEA Python plugin to stop nagging about this.

dms-cat avatar Mar 13 '19 03:03 dms-cat

"LazySettings" has no attribute "[every setting variable, for example AUTH_USER_MODEL, DEBUG]"

djangorestframework-stubs install django-stubs too as a dependency (I should mention it the README). So, you need to install that package properly too,

plugins =
     mypy_django_plugin.main

and create config file / set environment variable. See https://github.com/mkurnikov/django-stubs

"ModelViewSet" has no attribute "action" Too many arguments for "as_view" of "ViewSetMixin"

This looks like a bug.

mkurnikov avatar Mar 13 '19 10:03 mkurnikov

Thanks for the quick reply! I changed setup.cfg to include both plugins:

plugins =
    mypy_django_plugin.main,
    mypy_drf_plugin.main

The count has now gone down to 106:

  • "[App model class]" has no attribute "[attribute name, including 'id']"
  • Argument "choices" to "TextField" has incompatible type "List[str]"; expected "Optional[Iterable[Union[Tuple[Any, Any], Tuple[str, Iterable[Tuple[Any, Any]]]]]]"
  • Argument [N] of "[method name]" incompatible with supertype "[class, for example 'CreateModelMixin' or 'UpdateModelMixin']"
  • Incompatible return value type (got "HttpResponse", expected "Response")
  • Incompatible return value type (got "Optional[Any]", expected "Response")
  • Incompatible types in assignment (expression has type "[some type]", variable has type "[other type]")
  • Invalid index type "str" for "str"; expected type "Union[int, slice]"
  • Return type of "get_paginated_response" incompatible with supertype "BasePagination"
  • Signature of "[method name such as 'save' or 'delete']" incompatible with supertype "Model"
  • Too many arguments for "as_view" of "ViewSetMixin"
  • TypedDict "Dict[str, Any]" has no key '[key name]'
  • Unsupported operand types for + ("None" and "timedelta")
  • Unsupported operand types for + ("object" and "timedelta")
  • Value of type "[class name such as 'object']" is not indexable

Ping?

dms-cat avatar Mar 13 '19 20:03 dms-cat

@mkurnikov @kalekseev do you want help with solving this issue? I think I could fix some of these issues.

jmolinski avatar Aug 06 '19 19:08 jmolinski