-
-
Notifications
You must be signed in to change notification settings - Fork 3k
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
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
6beec4d
Set the type of `x = 0` and similar in the semantic analyzer.
29bf552
Skip this business if weak_opts is non-empty.
c28fc25
Disable lightweight type check if build target < TYPE_CHECK.
2c86207
Make tests pass.
e23030f
Fix testtypegen
b9234c6
Fix lint error
eeba23c
Only do the lightweight type inferencing thing outside functions. Rol…
84a67ad
Add an actual unit test for the lightweight type inference.
ce3b0af
Add a symmetric test.
564fbb4
Add tests for all supported literal types.
5d58439
More tests.
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -486,13 +486,12 @@ x = 1 | |
x = 1 | ||
|
||
[case testAssignToFuncDefViaImport] | ||
from m import * | ||
# TODO: This is bad, we don't see what's coming from m. | ||
from m import * # E: Incompatible import of "x" (imported name has type "int", local name has type "str") | ||
f = None # E: Need type annotation for variable | ||
x = '' | ||
[file m.py] | ||
def f(): pass | ||
x = 1 | ||
x = 1+0 | ||
[out] | ||
|
||
|
||
|
@@ -816,9 +815,11 @@ x = 0 | |
-- Test stability under import cycles | ||
-- ---------------------------------- | ||
|
||
-- The two tests are identical except one main has 'import x' and the other 'import y'. | ||
-- Previously (before build.order_ascc() was added) one of these would fail because the | ||
-- imports were processed in the (reverse) order in which the files were encountered. | ||
-- The first two tests are identical except one main has 'import x' | ||
-- and the other 'import y'. Previously (before build.order_ascc() | ||
-- was added) one of these would fail because the imports were | ||
-- processed in the (reverse) order in which the files were | ||
-- encountered. | ||
|
||
[case testImportCycleStability1] | ||
import x | ||
|
@@ -847,3 +848,114 @@ import x | |
class Sub(x.Base): | ||
attr = x.Base.attr | ||
[out] | ||
|
||
-- This case isn't fixed by order_ascc(), but is fixed by the | ||
-- lightweight type inference added to semanal.py | ||
-- (analyze_simple_literal_type()). | ||
|
||
[case testImportCycleStability3] | ||
import y | ||
[file x.py] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd also suggest testing some additional things:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a test case that checks something like this:
|
||
class Base: | ||
pass | ||
def foo() -> int: | ||
import y | ||
reveal_type(y.Sub.attr) | ||
return y.Sub.attr | ||
[file y.py] | ||
import x | ||
class Sub(x.Base): | ||
attr = 0 | ||
[out] | ||
tmp/y.py:1: note: In module imported here, | ||
main:1: note: ... from here: | ||
tmp/x.py: note: In function "foo": | ||
tmp/x.py:5: error: Revealed type is 'builtins.int' | ||
|
||
-- This case has a symmetrical cycle, so it doesn't matter in what | ||
-- order the files are processed. It depends on the lightweight type | ||
-- interference. | ||
|
||
[case testImportCycleStability4] | ||
import x | ||
[file x.py] | ||
import y | ||
class C: | ||
attr = '' | ||
def foo() -> int: | ||
return y.D.attr | ||
[file y.py] | ||
import x | ||
class D: | ||
attr = 0 | ||
def bar() -> str: | ||
return x.C.attr | ||
|
||
-- These cases test all supported literal types. | ||
|
||
[case testImportCycleStability5] | ||
import y | ||
[file x.py] | ||
class Base: | ||
pass | ||
def foo() -> None: | ||
import y | ||
i = y.Sub.iattr # type: int | ||
f = y.Sub.fattr # type: float | ||
s = y.Sub.sattr # type: str | ||
b = y.Sub.battr # type: bytes | ||
[file y.py] | ||
import x | ||
class Sub(x.Base): | ||
iattr = 0 | ||
fattr = 0.0 | ||
sattr = '' | ||
battr = b'' | ||
[out] | ||
|
||
[case testImportCycleStability6_python2] | ||
import y | ||
[file x.py] | ||
class Base: | ||
pass | ||
def foo() -> None: | ||
import y | ||
i = y.Sub.iattr # type: int | ||
f = y.Sub.fattr # type: float | ||
s = y.Sub.sattr # type: str | ||
u = y.Sub.uattr # type: unicode | ||
[file y.py] | ||
import x | ||
class Sub(x.Base): | ||
iattr = 0 | ||
fattr = 0.0 | ||
sattr = '' | ||
uattr = u'' | ||
[out] | ||
|
||
-- This case tests module-level variables. | ||
|
||
[case testImportCycleStability7] | ||
import x | ||
[file x.py] | ||
def foo() -> int: | ||
import y | ||
reveal_type(y.value) | ||
return y.value | ||
[file y.py] | ||
import x | ||
value = 12 | ||
[out] | ||
main:1: note: In module imported here: | ||
tmp/x.py: note: In function "foo": | ||
tmp/x.py:3: error: Revealed type is 'builtins.int' | ||
|
||
-- This is not really cycle-related but still about the lightweight | ||
-- type checker. | ||
|
||
[case testImportCycleStability8] | ||
x = 1 # type: str | ||
reveal_type(x) | ||
[out] | ||
main:1: error: Incompatible types in assignment (expression has type "int", variable has type "str") | ||
main:2: error: Revealed type is 'builtins.str' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.