Skip to content

KeyError running mypy for a django project #1410

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
caulagi opened this issue Apr 20, 2016 · 4 comments
Closed

KeyError running mypy for a django project #1410

caulagi opened this issue Apr 20, 2016 · 4 comments

Comments

@caulagi
Copy link

caulagi commented Apr 20, 2016

I was trying to run latest mypy (cloned from github) on my test django project code but I get the following error -

$ mypy --use-python-path polls/views.py
Traceback (most recent call last):
  File "/Users/pcaulagi/.venv/dj-mypy/bin/mypy", line 6, in <module>
    main(__file__)
  File "/Users/pcaulagi/.venv/dj-mypy/lib/python3.5/site-packages/mypy/main.py", line 52, in main
    res = type_check_only(sources, bin_dir, options)
  File "/Users/pcaulagi/.venv/dj-mypy/lib/python3.5/site-packages/mypy/main.py", line 100, in type_check_only
    python_path=options.python_path)
  File "/Users/pcaulagi/.venv/dj-mypy/lib/python3.5/site-packages/mypy/build.py", line 208, in build
    dispatch(sources, manager)
  File "/Users/pcaulagi/.venv/dj-mypy/lib/python3.5/site-packages/mypy/build.py", line 1251, in dispatch
    process_graph(graph, manager)
  File "/Users/pcaulagi/.venv/dj-mypy/lib/python3.5/site-packages/mypy/build.py", line 1382, in process_graph
    process_stale_scc(graph, scc)
  File "/Users/pcaulagi/.venv/dj-mypy/lib/python3.5/site-packages/mypy/build.py", line 1412, in process_stale_scc
    graph[id].type_check()
  File "/Users/pcaulagi/.venv/dj-mypy/lib/python3.5/site-packages/mypy/build.py", line 1231, in type_check
    manager.type_checker.visit_file(self.tree, self.xpath)
  File "/Users/pcaulagi/.venv/dj-mypy/lib/python3.5/site-packages/mypy/checker.py", line 412, in visit_file
    self.accept(d)
  File "/Users/pcaulagi/.venv/dj-mypy/lib/python3.5/site-packages/mypy/checker.py", line 453, in accept
    typ = node.accept(self)
  File "/Users/pcaulagi/.venv/dj-mypy/lib/python3.5/site-packages/mypy/nodes.py", line 650, in accept
    return visitor.visit_class_def(self)
  File "/Users/pcaulagi/.venv/dj-mypy/lib/python3.5/site-packages/mypy/checker.py", line 1033, in visit_class_def
    self.check_multiple_inheritance(typ)
  File "/Users/pcaulagi/.venv/dj-mypy/lib/python3.5/site-packages/mypy/checker.py", line 1052, in check_multiple_inheritance
    self.check_compatibility(name, base, base2, typ)
  File "/Users/pcaulagi/.venv/dj-mypy/lib/python3.5/site-packages/mypy/checker.py", line 1073, in check_compatibility
    second = base2[name]
  File "/Users/pcaulagi/.venv/dj-mypy/lib/python3.5/site-packages/mypy/nodes.py", line 1793, in __getitem__
    raise KeyError(name)
KeyError: 'subwidgets'

The other thing confusing me is that I get different keyerrors if I run the same command again

$ mypy --use-python-path polls/views.py
Traceback (most recent call last):
  File "/Users/pcaulagi/.venv/dj-mypy/bin/mypy", line 6, in <module>
    main(__file__)
  File "/Users/pcaulagi/.venv/dj-mypy/lib/python3.5/site-packages/mypy/main.py", line 52, in main
    res = type_check_only(sources, bin_dir, options)
  File "/Users/pcaulagi/.venv/dj-mypy/lib/python3.5/site-packages/mypy/main.py", line 100, in type_check_only
    python_path=options.python_path)
  File "/Users/pcaulagi/.venv/dj-mypy/lib/python3.5/site-packages/mypy/build.py", line 208, in build
    dispatch(sources, manager)
  File "/Users/pcaulagi/.venv/dj-mypy/lib/python3.5/site-packages/mypy/build.py", line 1251, in dispatch
    process_graph(graph, manager)
  File "/Users/pcaulagi/.venv/dj-mypy/lib/python3.5/site-packages/mypy/build.py", line 1382, in process_graph
    process_stale_scc(graph, scc)
  File "/Users/pcaulagi/.venv/dj-mypy/lib/python3.5/site-packages/mypy/build.py", line 1412, in process_stale_scc
    graph[id].type_check()
  File "/Users/pcaulagi/.venv/dj-mypy/lib/python3.5/site-packages/mypy/build.py", line 1231, in type_check
    manager.type_checker.visit_file(self.tree, self.xpath)
  File "/Users/pcaulagi/.venv/dj-mypy/lib/python3.5/site-packages/mypy/checker.py", line 412, in visit_file
    self.accept(d)
  File "/Users/pcaulagi/.venv/dj-mypy/lib/python3.5/site-packages/mypy/checker.py", line 453, in accept
    typ = node.accept(self)
  File "/Users/pcaulagi/.venv/dj-mypy/lib/python3.5/site-packages/mypy/nodes.py", line 650, in accept
    return visitor.visit_class_def(self)
  File "/Users/pcaulagi/.venv/dj-mypy/lib/python3.5/site-packages/mypy/checker.py", line 1033, in visit_class_def
    self.check_multiple_inheritance(typ)
  File "/Users/pcaulagi/.venv/dj-mypy/lib/python3.5/site-packages/mypy/checker.py", line 1052, in check_multiple_inheritance
    self.check_compatibility(name, base, base2, typ)
  File "/Users/pcaulagi/.venv/dj-mypy/lib/python3.5/site-packages/mypy/checker.py", line 1073, in check_compatibility
    second = base2[name]
  File "/Users/pcaulagi/.venv/dj-mypy/lib/python3.5/site-packages/mypy/nodes.py", line 1793, in __getitem__
    raise KeyError(name)
KeyError: 'id_for_label'

What am I missing? I am using Django==1.9.5 and this is my polls/views - https://github.com/caulagi/django-mypy/blob/master/polls/views.py

@gvanrossum
Copy link
Member

The --use-python-path flag is really a bug factory; Django is not annotated and uses many dynamic constructs that mypy doesn't understand. Unfortunately it also causes the occasional crash, like this one. It would be much better to run a stub generator over Django and point MYPYPATH at those stubs. (And please contribute those stubs back to the typeshed repo, assuming the Django developers are okay with it.)

@caulagi
Copy link
Author

caulagi commented Apr 20, 2016

@gvanrossum thanks for the clarification. I could get it working -

$ MYPYPATH=`pwd`/stubs mypy polls/views.py
polls/views.py: note: In function "detail":
polls/views.py:23: error: Incompatible return value type: expected django.http.response.HttpResponse, got builtins.str

I had to edit the files generated by stubgen though. Is this expected or is it a bug with stubgen? In some cases, it seemed like the pyi files generated by stubgen was not what mypy expected. Should I list the problems here?

@gvanrossum
Copy link
Member

Yes, the stub generator is far from perfect. Most people manually edit the stubs until they work well enough and then submit then to the typeshed repo. Do you want to give that a try? If you need more help figuring out how to edit your stubs for very basic things please do show some details here. (I'm not going to be able to try and run stubgen on django myself, so please do copy code snippets if you're using this option.)

@caulagi
Copy link
Author

caulagi commented Apr 20, 2016

Thanks for the explanation. The stub files I have so far are here - https://github.com/caulagi/django-mypy/tree/master/stubs/django. I will take this over to django-developers now and see what the opinion is.

@caulagi caulagi closed this as completed Apr 20, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants