8000 Fine-grained: Don't infer partial types from multiple targets by JukkaL · Pull Request #4553 · python/mypy · GitHub
[go: up one dir, main page]

Skip to content

Fine-grained: Don't infer partial types from multiple targets #4553

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 13 commits into from
Feb 20, 2018
Prev Previous commit
Update based on feedback
  • Loading branch information
JukkaL committed Feb 19, 2018
commit 3f3e4f06cd3cce12032cb6c8e62de7a65afc12f1
8 changes: 5 additions & 3 deletions mypy/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,11 @@
('is_upper_bound', bool), # False => precise type
])

# Keeps track of partial types in a single scope
PartialTypeScope = NamedTuple('PartialTypeMap', [('map', Dict[Var, Context]),
('is_function', bool)])
# Keeps track of partial types in a single scope. In fine-grained incremental
# mode partial types initially defined at the top level cannot be completed in
# a function, and we use the 'is_function' attribute to enforce this.
PartialTypeScope = NamedTuple('PartialTypeScope', [('map', Dict[Var, Context]),
('is_function', bool)])


class TypeChecker(NodeVisitor[None], CheckerPluginInterface):
Expand Down
17 changes: 17 additions & 0 deletions test-data/unit/check-inference.test
Original file line number Diff line number Diff line change
Expand Up @@ -2213,6 +2213,23 @@ class B(A):
class C(B):
y = None

[case testLocalPartialTypesInMultipleMroItems]
# flags: --local-partial-types --strict-optional
from typing import Optional

class A:
x: Optional[str]

class B(A):
x = None

class C(B):
x = None

# TODO: Inferring None below is unsafe (https://github.com/python/mypy/issues/3208)
reveal_type(B.x) # E: Revealed type is 'builtins.None'
reveal_type(C.x) # E: Revealed type is 'builtins.None'

[case testLocalPartialTypesWithInheritance2-skip]
# flags: --local-partial-types
# This is failing because of https://github.com/python/mypy/issues/4552
Expand Down
17 changes: 17 additions & 0 deletions test-data/unit/fine-grained.test
Original file line number Diff line number Diff line change
Expand Up @@ -1844,3 +1844,20 @@ def f() -> None:
[out]
==
a.py:1: error: Need type annotation for 'y'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just for completes I would add a tests where the error appears in the first run, but not in the second one (like if a user listened and actually added an annotation).

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a test case.


[case testNonePartialType4]
import a
[file a.py]
y = None
def f() -> None:
global y
y = ''
[file a.py.2]
from typing import Optional
y: Optional[str] = None
def f() -> None:
global y
y = ''
[out]
a.py:1: error: Need type annotation for 'y'
==
0