-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
A story: Getting started with mypy on a large existing codebase #2096
Comments
http://mypy.readthedocs.io/en/latest/common_issues.html#types-of-empty-collections
Try putting |
Thank you for taking the time to respond and provide references. Particularly on a holiday.
That topic ("The mypy command line") is at position 15/18 in the reference documentation and I did not see it before. Under the assumption that running the mypy tool would be a topic of primary interest when reading the mypy documentation, it might make more sense to feature this topic earlier in the documentation to increase visibility.
Although it may not be possible to deduce the specific type of an empty list literal beyond
Interesting. Is this limitation of dicts intentional or something that is candidate to amend in the future? (That is, if I or someone were to submit a patch to support such dicts, would it be rejected for some philosophical reason?) This leads to a separate philosophical question as to what kinds of things the type checker is intended to flag. For example is it the goal that the type checker not issue any errors when run on an unannotated program that has 100% code coverage? Another way of phrasing the question: should the type checker accept all programs that execute properly at runtime even if they're doing something "weird" in the opinion of the type system designer? |
This is something we've been considering for a long time. The reason why mypy doesn't do this right now is that then it would be easy to accidentally leave partially unchecked parts of code. Besides, annotating these variables usually isn't a lot of work. We've been thinking about a "strict mode" that would require all functions to be fully annotated, among other things. It could enforce that no implicit
I've thought about this a bit, and this would be a pretty complex feature to implement. It would likely require changes to PEP 484 and/or the
Well that would be nice, but it's not likely to happen, due to various technical difficulties. Basically Python is such a dynamic language that a static checker has to make some simplifying assumptions about how programs behave, and sometimes these assumptions are not right. The actual goal is to keep the number of errors low (enough) for most Python code and to generally make it easy to work around the errors. The way I think about this is actually pretty simple: adopting static typing will take some work anyway, since writing function annotations takes some effort, and without function annotations mypy won't be terribly useful. We try to reduce the total amount of work needed to annotate a program, and fixing useless errors generated when checking unannotated programs is just one thing among many that we are working on to improve, and with limited resources we have to prioritize tasks. |
@davidfstr I'm going through a similar exercise now, one example I've found to be super useful is the https://github.com/zulip/zulip codebase. It's mostly annotated with PEP 484 annotations so it's a great real world example and they've got a useful script for invoking MyPy with a Blacklist - https://github.com/zulip/zulip/blob/master/tools/run-mypy @gnprice @JukkaL Would it be possible to get the Zulip folks at Dropbox to do a blog post or a doc on their adoption of MyPy? I'd love to have more insight into how the transition went. |
Thanks for the insights. I'll close this thread for now, until I have more time to apply mypy against my Django codebase again. |
I managed to minimally annotate all existing code so that mypy gave no errors. The kinds of issues I needed to fix were:
All of these error classes I've mentioned earlier in this thread. Edit 1: Reflect that not all empty list literals needed annotation. |
All in all not a bad result! I'm curious -- are you sure you had to annotate all empty list literals? I'm saying because mypy typically doesn't need this when you write e.g. # inside a function
xs = []
for x in ...:
xs.append(x) However it does need it in some cases where the initialization code is more complex, and AFAIK in all cases where it's a class attribute or global. So that's probably what you're seeing. |
Ah you're correct. Only a few |
One item I forgot to mention:
|
This evening I decided to try out mypy on a large existing Django codebase. Hundreds of .py files.
Surprisingly, I found no actual tutorial on the documentation on how to use mypy on a large existing project as opposed to single file. Therefore I will enumerate a number of issues that I have encountered so far. Perhaps such an enumeration will be useful in writing new documentation, fixing issues, or other improvements.
I tried invoking mypy on a directory rather than a single .py file. This appears to work, however the documentation does not appear to mention this usage as a possiblity.
error: Cannot find module named 'FOO.BAR.BAZ'
I receive the following error when there certainly exists a file
./FOO/BAR/BAZ.py
. Therefore this error makes no sense to me. I invoked mypy withMYPYPATH=. mypy .
.error: Need type annotation for variable
Why? This points to a line that looks like
dependencies = []
. Error message should be improved to indicate why it is demanding a type annotation.error: Method must have at least one argument
Well done. This was flagging a number of unittest methods marked with
@skip
that took no arguments. Such as the following:It was expecting
(self)
as the signature, which seems reasonable.error: Result type of + incompatible in assignment
Appears to be a mypy bug. Was complaining about the following construction:
error: Value of type "object" is not indexable
Appears to be a mypy bug. Was complaining about the following construction:
error: Callable[[Any, Any, Any], Any] has no attribute "short_description"
Appears to be a mypy bug. Was complaining about the following construction:
It is legal (albeit advanced) to annotate a function object (including a lambda) with additional attributes.
error: Name 'Foo' already defined
Common to receive this kind of error when using star-imports. For example:
In the above scenario mypy will complain that C is already defined in A.
Recommend altering there to be an error only if Foo is defined more than once and is defined to be something different.
That's all folks. I suppose I'll wait to add my own type annotations until mypy gives my current unannotated code a clean bill of health.
The text was updated successfully, but these errors were encountered: