8000 Improve error message for partial None with `--local-partial-types` by pranavrajpal · Pull Request #12822 · python/mypy · GitHub
[go: up one dir, main page]

Skip to content

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

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 5 commits into from
Sep 2, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Suggest PEP 604 annotation when on Python 3.10
Code running on Python 3.10 can use PEP 604 annotations, so suggest
those instead.
  • Loading branch information
pranavrajpal committed May 20, 2022
commit c46956733460f0fbc8452860827ecfb807a1b328
6 changes: 5 additions & 1 deletion mypy/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -1193,14 +1193,18 @@ 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)
pep604_supported = python_version and python_version >= (3, 10)
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: feels like this should mirror the check for has_variable_annotations when python_version is None

# 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):
type_dec = '<type>'
if not node.type.type:
# partial None
recommended_type = f'Optional[{type_dec}]'
if pep604_supported:
recommended_type = f'{type_dec} | None'
else:
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]
Expand Down
4 changes: 4 additions & 0 deletions test-data/unit/check-inference.test
Original file line number Diff line number Diff line change
Expand Up @@ -3255,3 +3255,7 @@ reveal_type(x) # N: Revealed type is "builtins.bytes"
if x:
reveal_type(x) # N: Revealed type is "builtins.bytes"
[builtins fixtures/dict.pyi]

[case testSuggestPep604AnnotationForPartialNone]
# flags: --local-partial-types --python-version 3.10
x = None # E: Need type annotation for "x" (hint: "x: <type> | None = ...")
0