-
-
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
Infer variable type if initializer is an empty collection #1055
Comments
FWIW, this can get slow really quickly: http://crystal-lang.org/2013/11/14/good-bye-ruby-thursday.html#compile-times-were-too-big |
We'll use a simpler form of type inference that doesn't slow things much (if at all). We aren't going to do whole-program type inference. |
Here's the general idea for how this would work:
|
Cases (1), (2) and list append now work. The remaining cases are less important. |
Awesome! This got rid of 18 out of 31 occurrences of this message. The
On Sun, Dec 6, 2015 at 5:00 PM, Jukka Lehtosalo [email protected]
--Guido van Rossum (python.org/~guido) |
None shouldn't be hard. Mypy probably won't infer instance variables, except maybe for cases where you populate the collection in the same same place where initialization happens. There is minimal set support but not sure how far it's practical to take it. |
Do you support discard() for sets? That's one of the cases. On Sun, Dec 6, 2015 at 6:19 PM, Jukka Lehtosalo [email protected]
--Guido van Rossum (python.org/~guido) |
discard() is not supported but it would be pretty straightforward to add. However, I'd rather try to first implement a more general solution that would work with arbitrary methods instead of special casing additional methods. |
|
This form currently isn't inferred, can it be?
|
@pjenvey That's correct, this doesn't work for instance attributes. |
This statement is valid; since the list is empty it doesn't really matter what type of list it is -- update_filesystems is only every going to try to iterate over it anyway (which will yield no entries). However, mypy doesn't understand it. Unfortunately I can't find either a reference to the canonical solution or bug; the nearest I get is point (8) at python/mypy#1055.
Code like this no longer requires a type annotation: ``` a = [] if foo(): a = [1] ``` Work towards #1055.
This example no longer needs a type annotation: ``` from collections import OrderedDict x = OrderedDict() x[1] = 'x' ``` Work towards #1055.
Work towards #1055 Currently partial types are supported for local variables. However, only partial `None` types are supported for `self` attributes. This PR adds the same level of support to generic partial types. They follow mostly the same rules: * A partial type can be refined in the _same_ method where it is defined. * But a partial type from class body can not be refined in a method, as if `local_partial_types = True`. The logic is pretty simple: the `.node` attribute for `self.attr` expressions is set to `None`, so I added a little helper to get it from the class symbol table instead.
I think that this works pretty well now after the latest improvements, including #8167. There are some possible refinements but they are nice to haves. |
Code like this is one of the most common cases where we need a variable annotation (DONE):
Mypy could do a better job of inferring types in cases like these:
These cases would still require an annotation:
__init__
and mutated in another method.This is related to #254.
More common examples where this should work:
(1) DONE
(2) DONE
Additional, less common examples (not done):
(3) [postponed -- too rare and complicated]
(4) DONE
(5) [rejected -- too rare]
(6) DONE (#8036)
(7) DONE (#8167 and #8039)
Similar to above but with a user-defined generic type or a standard library generic type that is not list, dict or set.
Update: After some more analysis, only
defaultdict
andOrderedDict
seem common enough to support.Not sure about this:
(8) [rejected idea]
The text was updated successfully, but these errors were encountered: