Skip to content
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

Improve error message for partial None with --local-partial-types #12822

Merged
merged 5 commits into from
Sep 2, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 16 additions & 8 deletions mypy/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -1193,18 +1193,26 @@ def need_annotation_for_var(self, node: SymbolNode, context: Context,
python_version: Optional[Tuple[int, int]] = None) -> None:
hint = ''
has_variable_annotations = not python_version or python_version >= (3, 6)
# type to recommend the user adds
recommended_type = None
# Only gives hint if it's a variable declaration and the partial type is a builtin type
if (python_version and isinstance(node, Var) and isinstance(node.type, PartialType) and
node.type.type and node.type.type.fullname in reverse_builtin_aliases):
alias = reverse_builtin_aliases[node.type.type.fullname]
alias = alias.split('.')[-1]
if python_version and isinstance(node, Var) and isinstance(node.type, PartialType):
type_dec = '<type>'
if alias == 'Dict':
type_dec = f'{type_dec}, {type_dec}'
if not node.type.type:
# partial None
recommended_type = f'Optional[{type_dec}]'
elif node.type.type.fullname in reverse_builtin_aliases:
# partial types other than partial None
alias = reverse_builtin_aliases[node.type.type.fullname]
alias = alias.split('.')[-1]
if alias == 'Dict':
type_dec = f'{type_dec}, {type_dec}'
recommended_type = f'{alias}[{type_dec}]'
if recommended_type is not None:
if has_variable_annotations:
hint = f' (hint: "{node.name}: {alias}[{type_dec}] = ...")'
hint = f' (hint: "{node.name}: {recommended_type} = ...")'
else:
hint = f' (hint: "{node.name} = ... # type: {alias}[{type_dec}]")'
hint = f' (hint: "{node.name} = ... # type: {recommended_type}")'

if has_variable_annotations:
needed = 'annotation'
Expand Down
12 changes: 6 additions & 6 deletions test-data/unit/check-inference.test
Original file line number Diff line number Diff line change
Expand Up @@ -2394,7 +2394,7 @@ if bool():

[case testLocalPartialTypesWithGlobalInitializedToNone]
# flags: --local-partial-types
x = None # E: Need type annotation for "x"
x = None # E: Need type annotation for "x" (hint: "x: Optional[<type>] = ...")
pranavrajpal marked this conversation as resolved.
Show resolved Hide resolved

def f() -> None:
global x
Expand All @@ -2405,7 +2405,7 @@ reveal_type(x) # N: Revealed type is "None"

[case testLocalPartialTypesWithGlobalInitializedToNone2]
# flags: --local-partial-types
x = None # E: Need type annotation for "x"
x = None # E: Need type annotation for "x" (hint: "x: Optional[<type>] = ...")

def f():
global x
Expand Down Expand Up @@ -2454,7 +2454,7 @@ reveal_type(a) # N: Revealed type is "builtins.str"
[case testLocalPartialTypesWithClassAttributeInitializedToNone]
# flags: --local-partial-types
class A:
x = None # E: Need type annotation for "x"
x = None # E: Need type annotation for "x" (hint: "x: Optional[<type>] = ...")

def f(self) -> None:
self.x = 1
Expand Down Expand Up @@ -2637,7 +2637,7 @@ from typing import List
def f(x): pass

class A:
x = None # E: Need type annotation for "x"
x = None # E: Need type annotation for "x" (hint: "x: Optional[<type>] = ...")

def f(self, p: List[str]) -> None:
self.x = f(p)
Expand All @@ -2647,15 +2647,15 @@ class A:
[case testLocalPartialTypesAccessPartialNoneAttribute]
# flags: --local-partial-types
class C:
a = None # E: Need type annotation for "a"
a = None # E: Need type annotation for "a" (hint: "a: Optional[<type>] = ...")

def f(self, x) -> None:
C.a.y # E: Item "None" of "Optional[Any]" has no attribute "y"

[case testLocalPartialTypesAccessPartialNoneAttribute2]
# flags: --local-partial-types
class C:
a = None # E: Need type annotation for "a"
a = None # E: Need type annotation for "a" (hint: "a: Optional[<type>] = ...")

def f(self, x) -> None:
self.a.y # E: Item "None" of "Optional[Any]" has no attribute "y"
Expand Down
12 changes: 6 additions & 6 deletions test-data/unit/fine-grained.test
Original file line number Diff line number Diff line change
Expand Up @@ -4165,9 +4165,9 @@ y = 0
[file a.py.2]
y = ''
[out]
main:4: error: Need type annotation for "x"
main:4: error: Need type annotation for "x" (hint: "x: Optional[<type>] = ...")
==
main:4: error: Need type annotation for "x"
main:4: error: Need type annotation for "x" (hint: "x: Optional[<type>] = ...")

[case testNonePartialType2]
import a
Expand All @@ -4183,9 +4183,9 @@ y = 0
[file a.py.2]
y = ''
[out]
main:4: error: Need type annotation for "x"
main:4: error: Need type annotation for "x" (hint: "x: Optional[<type>] = ...")
==
main:4: error: Need type annotation for "x"
main:4: error: Need type annotation for "x" (hint: "x: Optional[<type>] = ...")

[case testNonePartialType3]
import a
Expand All @@ -4197,7 +4197,7 @@ def f() -> None:
y = ''
[out]
==
a.py:1: error: Need type annotation for "y"
a.py:1: error: Need type annotation for "y" (hint: "y: Optional[<type>] = ...")

[case testNonePartialType4]
import a
Expand All @@ -4213,7 +4213,7 @@ def f() -> None:
global y
y = ''
[out]
a.py:1: error: Need type annotation for "y"
a.py:1: error: Need type annotation for "y" (hint: "y: Optional[<type>] = ...")
==

[case testSkippedClass1]
Expand Down