Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit2c0510c

Browse files
tungolKevinRK29
authored andcommitted
stubtest: additional guidance on errors when runtime is object.__init__ (#19733)
Fixes#19732This is a simple check to point users in the right direction when theyget errors because their class uses `__new__` but they wrote stubs for`__init__`. I don't feel strongly about the exact wording used here. Ialso considered "Maybe you meant to define `__new__` instead of`__init__`?".(cherry picked from commit530bdc5)
1 parent2f3f03c commit2c0510c

File tree

1 file changed

+23
-4
lines changed

1 file changed

+23
-4
lines changed

‎mypy/stubtest.py‎

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1053,7 +1053,10 @@ def get_kind(arg_name: str) -> nodes.ArgKind:
10531053

10541054

10551055
def_verify_signature(
1056-
stub:Signature[nodes.Argument],runtime:Signature[inspect.Parameter],function_name:str
1056+
stub:Signature[nodes.Argument],
1057+
runtime:Signature[inspect.Parameter],
1058+
function_name:str,
1059+
warn_runtime_is_object_init:bool=False,
10571060
)->Iterator[str]:
10581061
# Check positional arguments match up
10591062
forstub_arg,runtime_arginzip(stub.pos,runtime.pos):
@@ -1098,6 +1101,8 @@ def _verify_signature(
10981101
msg=f'runtime does not have parameter "{stub_arg.variable.name}"'
10991102
ifruntime.varkwisnotNone:
11001103
msg+=". Maybe you forgot to make it keyword-only in the stub?"
1104+
elifwarn_runtime_is_object_init:
1105+
msg+=". You may need to write stubs for __new__ instead of __init__."
11011106
yieldmsg
11021107
else:
11031108
yieldf'stub parameter "{stub_arg.variable.name}" is not keyword-only'
@@ -1137,7 +1142,11 @@ def _verify_signature(
11371142
ifargnotin {runtime_arg.nameforruntime_arginruntime.pos[len(stub.pos) :]}:
11381143
yieldf'runtime parameter "{arg}" is not keyword-only'
11391144
else:
1140-
yieldf'runtime does not have parameter "{arg}"'
1145+
msg=f'runtime does not have parameter "{arg}"'
1146+
ifwarn_runtime_is_object_init:
1147+
msg+=". You may need to write stubs for __new__ instead of __init__."
1148+
yieldmsg
1149+
11411150
forarginsorted(set(runtime.kwonly)-set(stub.kwonly)):
11421151
ifargin {stub_arg.variable.nameforstub_arginstub.pos}:
11431152
# Don't report this if we've reported it before
@@ -1223,7 +1232,12 @@ def verify_funcitem(
12231232
ifnotsignature:
12241233
return
12251234

1226-
formessagein_verify_signature(stub_sig,runtime_sig,function_name=stub.name):
1235+
formessagein_verify_signature(
1236+
stub_sig,
1237+
runtime_sig,
1238+
function_name=stub.name,
1239+
warn_runtime_is_object_init=runtimeisobject.__init__,
1240+
):
12271241
yieldError(
12281242
object_path,
12291243
"is inconsistent, "+message,
@@ -1333,7 +1347,12 @@ def verify_overloadedfuncdef(
13331347
stub_sig=Signature.from_overloadedfuncdef(stub)
13341348
runtime_sig=Signature.from_inspect_signature(signature)
13351349

1336-
formessagein_verify_signature(stub_sig,runtime_sig,function_name=stub.name):
1350+
formessagein_verify_signature(
1351+
stub_sig,
1352+
runtime_sig,
1353+
function_name=stub.name,
1354+
warn_runtime_is_object_init=runtimeisobject.__init__,
1355+
):
13371356
# TODO: This is a little hacky, but the addition here is super useful
13381357
if"has a default value of type"inmessage:
13391358
message+= (

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp