8000 Provide hint when Union type with None may not have been narrowed by angelawuuu · Pull Request #17178 · python/mypy · GitHub
[go: up one dir, main page]

Skip to content

Provide hint when Union type with None may not have been narrowed #17178

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

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
Open
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
fixed issue with getting variable name for None guard hint
  • Loading branch information
angelawuuu committed Apr 24, 2024
commit 2a2d50c4a5d7393e177b6f72226040577bccfb81
9 changes: 4 additions & 5 deletions mypy/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,15 @@
IndexExpr,
MypyFile,
NameExpr,
MemberExpr,
ReturnStmt,
StrExpr,
SymbolNode,
SymbolTable,
TypeInfo,
Var,
reverse_builtin_aliases,
get_member_expr_fullname,
)
from mypy.operators import op_methods, op_methods_to_symbols
from mypy.options import Options
Expand Down Expand Up @@ -530,11 +532,8 @@ def has_no_attr(
code=codes.UNION_ATTR,
)
if typ_format == '"None"':
if typ.source is None:
s = ""
else:
s = f' "{typ.source}"'
self.note(f'You can use "if{s} is not None" to guard against a None value')
var_name = f' {get_member_expr_fullname(context.expr)}'
self.note(f'You can use "if{var_name} is not None" to guard against a None value', context, code=codes.UNION_ATTR)
return codes.UNION_ATTR
elif isinstance(original_type, TypeVarType):
bound = get_proper_type(original_type.upper_bound)
Expand Down
0