-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Infer variable types from simple literals in semanal.py #1756
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
Conversation
@JukkaL: I'm pretty sure I broke something -- it can't be a coincidence that the foo() I gave above used to type-check but now requires an annotation. But my little brain can't explain it. :-( |
I was going to say "and why didn't this break any tests?" -- but then I realized that the changes I had to make to unbreak check-typevar-values.test probably provide a clue. |
I guess I must understand what 5797c39 does -- that's where the two tests I broke in that file were added. |
I think I can fix that by doing this only when it occurs outside functions. (In the future we could tighten that to "outside generic functions".) |
(FYI I rolled back the changes to posixpath.py using git manipulations.) |
OK, I feel better about this again. |
Hmm, it might be useful to run the type inference at least inside |
Maybe, but what if |
This situation could arise if either (In principle it could happen for any function, but then there is code which is never reachable (according to mypy) and that's probably a bug anyways.) |
In any case I still need to add test cases for this feature. |
@@ -847,3 +849,17 @@ import x | |||
class Sub(x.Base): | |||
attr = x.Base.attr | |||
[out] | |||
|
|||
[case testImportCycleStability3] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Another potential test case is one where two files that form a cycle symmetrically refer to a attribute defined in the other file. This would remain a valid test case no matter in which order we'll end up type checking the modules.
Looks good, other than the ideas for additional test cases. Maybe implement simple type inference within methods in a separate PR, as this PR already implements a self-contained and useful new feature. For top-level functions and non-instance methods (maybe even non- |
This thwarts certain kinds of errors due to circular dependencies (see #1530).
…l back changes to check-typevar-values.test.
OK, I've got all the tests requested. Feel free to merge. |
This implements another partial approach to #1530. The cases in our internal repo where my previous PR (#1736) caused regressions are all fixed by this.
However I'm not fully convinced that this is the right way to go about it. Some of the tests I had to fix make me wonder. Example:
If I understand what I had to fix in posixpath.py correctly, this used to pass silently while now I have to add
# type: AnyStr
to the first assignment to x.