Skip to content

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

Merged
merged 11 commits into from
Jun 30, 2016
Merged

Conversation

gvanrossum
Copy link
Member

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:

def foo(s: AnyStr) -> AnyStr:
    if isinstance(s, str):
        x = '/'
    else:
        x = b'/'
    return x

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.

@gvanrossum
Copy link
Member Author

@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. :-(

@gvanrossum
Copy link
Member Author

gvanrossum commented Jun 27, 2016

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.

@gvanrossum
Copy link
Member Author

I guess I must understand what 5797c39 does -- that's where the two tests I broke in that file were added.

@gvanrossum
Copy link
Member Author

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".)

@gvanrossum
Copy link
Member Author

(FYI I rolled back the changes to posixpath.py using git manipulations.)

@gvanrossum
Copy link
Member Author

OK, I feel better about this again.

@JukkaL
Copy link
Collaborator

JukkaL commented Jun 28, 2016

Hmm, it might be useful to run the type inference at least inside __init__?

@gvanrossum
Copy link
Member Author

Maybe, but what if __init__ is generic? Is there a way to identify a generic function in the function_stack?

@rwbarton
Copy link
Contributor

This situation could arise if either __init__ itself is a generic function (its CallableType has nonempty variables), or the class that it is the __init__ of is a generic class.

(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.)

@gvanrossum
Copy link
Member Author

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]
Copy link
Collaborator

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.

@JukkaL
Copy link
Collaborator

JukkaL commented Jun 29, 2016

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-__init__ methods) this likely wouldn't be useful.

@gvanrossum
Copy link
Member Author

OK, I've got all the tests requested. Feel free to merge.

@JukkaL JukkaL merged commit 9c40e56 into master Jun 30, 2016
@gvanrossum gvanrossum deleted the class_attrs branch June 30, 2016 18:38
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

Successfully merging this pull request may close these issues.

3 participants