Skip to content

Commit fb89910

Browse files
committed
Suggest codemod for --no-implicit-optional
1 parent aa947e2 commit fb89910

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

mypy/checker.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1262,13 +1262,25 @@ def check_default_args(self, item: FuncItem, body_is_trivial: bool) -> None:
12621262
msg += f"tuple argument {name[12:]}"
12631263
else:
12641264
msg += f'argument "{name}"'
1265+
if (
1266+
not self.options.implicit_optional
1267+
and isinstance(arg.initializer, NameExpr)
1268+
and arg.initializer.fullname == "builtins.None"
1269+
):
1270+
notes = [
1271+
"PEP 484 prohibits implicit Optional. "
1272+
"Accordingly, mypy has changed its default to no_implicit_optional=True.",
1273+
"Use https://github.com/hauntsaninja/no_implicit_optional to automatically "
1274+
"upgrade your codebase"
1275+
]
12651276
self.check_simple_assignment(
12661277
arg.variable.type,
12671278
arg.initializer,
12681279
context=arg.initializer,
12691280
msg=ErrorMessage(msg, code=codes.ASSIGNMENT),
12701281
lvalue_name="argument",
12711282
rvalue_name="default",
1283+
notes=notes,
12721284
)
12731285

12741286
def is_forward_op_method(self, method_name: str) -> bool:
@@ -3739,6 +3751,8 @@ def check_simple_assignment(
37393751
msg: ErrorMessage = message_registry.INCOMPATIBLE_TYPES_IN_ASSIGNMENT,
37403752
lvalue_name: str = "variable",
37413753
rvalue_name: str = "expression",
3754+
*,
3755+
notes: list[str] | None = None,
37423756
) -> Type:
37433757
if self.is_stub and isinstance(rvalue, EllipsisExpr):
37443758
# '...' is always a valid initializer in a stub.
@@ -3763,6 +3777,7 @@ def check_simple_assignment(
37633777
msg,
37643778
f"{rvalue_name} has type",
37653779
f"{lvalue_name} has type",
3780+
notes=notes,
37663781
)
37673782
return rvalue_type
37683783

@@ -5666,6 +5681,7 @@ def check_subtype(
56665681
subtype_label: str | None = None,
56675682
supertype_label: str | None = None,
56685683
*,
5684+
notes: list[str] | None = None,
56695685
code: ErrorCode | None = None,
56705686
outer_context: Context | None = None,
56715687
) -> bool:
@@ -5681,6 +5697,7 @@ def check_subtype(
56815697
subtype_label: str | None = None,
56825698
supertype_label: str | None = None,
56835699
*,
5700+
notes: list[str] | None = None,
56845701
outer_context: Context | None = None,
56855702
) -> bool:
56865703
...
@@ -5694,6 +5711,7 @@ def check_subtype(
56945711
subtype_label: str | None = None,
56955712
supertype_label: str | None = None,
56965713
*,
5714+
notes: list[str] | None = None,
56975715
code: ErrorCode | None = None,
56985716
outer_context: Context | None = None,
56995717
) -> bool:
@@ -5714,7 +5732,7 @@ def check_subtype(
57145732
return False
57155733
extra_info: list[str] = []
57165734
note_msg = ""
5717-
notes: list[str] = []
5735+
notes = notes or []
57185736
if subtype_label is not None or supertype_label is not None:
57195737
subtype_str, supertype_str = format_type_distinctly(orig_subtype, orig_supertype)
57205738
if subtype_label is not None:
@@ -5725,7 +5743,7 @@ def check_subtype(
57255743
outer_context or context, subtype, supertype, supertype_str
57265744
)
57275745
if isinstance(subtype, Instance) and isinstance(supertype, Instance):
5728-
notes = append_invariance_notes([], subtype, supertype)
5746+
notes = append_invariance_notes(notes, subtype, supertype)
57295747
if extra_info:
57305748
msg = msg.with_additional_msg(" (" + ", ".join(extra_info) + ")")
57315749

0 commit comments

Comments
 (0)