Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5c78458

Browse files
authored
Correct check_default_args(), its relates and error messages (#20964)
Fix: #20963
1 parent ea8c8f9 commit 5c78458

11 files changed

Lines changed: 65 additions & 65 deletions

docs/source/stubs.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ For example:
126126
"""Some docstring"""
127127
pass
128128
129-
# Error: Incompatible default for argument "foo" (default has
130-
# type "ellipsis", argument has type "list[str]")
129+
# Error: Incompatible default for parameter "foo" (default has
130+
# type "ellipsis", parameter has type "list[str]")
131131
def not_ok(self, foo: list[str] = ...) -> None:
132132
print(foo)

mypy/checker.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1387,7 +1387,7 @@ def check_func_def(
13871387
# Type check initialization expressions.
13881388
body_is_trivial = is_trivial_body(defn.body)
13891389
if not self.can_skip_diagnostics:
1390-
self.check_default_args(item, body_is_trivial)
1390+
self.check_default_params(item, body_is_trivial)
13911391

13921392
# Type check body in a new scope.
13931393
with self.binder.top_frame_context():
@@ -1710,22 +1710,22 @@ def check_unbound_return_typevar(self, typ: CallableType) -> None:
17101710
context=typ.ret_type,
17111711
)
17121712

1713-
def check_default_args(self, item: FuncItem, body_is_trivial: bool) -> None:
1714-
for arg in item.arguments:
1715-
if arg.initializer is None:
1713+
def check_default_params(self, item: FuncItem, body_is_trivial: bool) -> None:
1714+
for param in item.arguments:
1715+
if param.initializer is None:
17161716
continue
1717-
if body_is_trivial and isinstance(arg.initializer, EllipsisExpr):
1717+
if body_is_trivial and isinstance(param.initializer, EllipsisExpr):
17181718
continue
1719-
name = arg.variable.name
1719+
name = param.variable.name
17201720
msg = "Incompatible default for "
1721-
if name.startswith("__tuple_arg_"):
1722-
msg += f"tuple argument {name[12:]}"
1721+
if name.startswith("__tuple_param_"):
1722+
msg += f"tuple parameter {name[12:]}"
17231723
else:
1724-
msg += f'argument "{name}"'
1724+
msg += f'parameter "{name}"'
17251725
if (
17261726
not self.options.implicit_optional
1727-
and isinstance(arg.initializer, NameExpr)
1728-
and arg.initializer.fullname == "builtins.None"
1727+
and isinstance(param.initializer, NameExpr)
1728+
and param.initializer.fullname == "builtins.None"
17291729
):
17301730
notes = [
17311731
"PEP 484 prohibits implicit Optional. "
@@ -1736,11 +1736,11 @@ def check_default_args(self, item: FuncItem, body_is_trivial: bool) -> None:
17361736
else:
17371737
notes = None
17381738
self.check_simple_assignment(
1739-
arg.variable.type,
1740-
arg.initializer,
1741-
context=arg.initializer,
1739+
param.variable.type,
1740+
param.initializer,
1741+
context=param.initializer,
17421742
msg=ErrorMessage(msg, code=codes.ASSIGNMENT),
1743-
lvalue_name="argument",
1743+
lvalue_name="parameter",
17441744
rvalue_name="default",
17451745
notes=notes,
17461746
)

mypy/checkexpr.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5478,7 +5478,7 @@ def visit_lambda_expr(self, e: LambdaExpr) -> Type:
54785478
"""Type check lambda expression."""
54795479
old_in_lambda = self.in_lambda_expr
54805480
self.in_lambda_expr = True
5481-
self.chk.check_default_args(e, body_is_trivial=False)
5481+
self.chk.check_default_params(e, body_is_trivial=False)
54825482
inferred_type, type_override = self.infer_lambda_type_using_context(e)
54835483
if not inferred_type:
54845484
self.chk.return_types.append(AnyType(TypeOfAny.special_form))

test-data/unit/check-columns.test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ if int():
271271

272272
[case testColumnIncompatibleDefault]
273273
if int():
274-
def f(x: int = '') -> None: # E:20: Incompatible default for argument "x" (default has type "str", argument has type "int")
274+
def f(x: int = '') -> None: # E:20: Incompatible default for parameter "x" (default has type "str", parameter has type "int")
275275
pass
276276

277277
[case testColumnMissingProtocolMember]

test-data/unit/check-errorcodes.test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,7 @@ def f() -> int:
342342
[case testErrorCodeAssignment]
343343
x: str = 0 # E: Incompatible types in assignment (expression has type "int", variable has type "str") [assignment]
344344

345-
def f(x: str = 0) -> None: # E: Incompatible default for argument "x" (default has type "int", argument has type "str") [assignment]
345+
def f(x: str = 0) -> None: # E: Incompatible default for parameter "x" (default has type "int", parameter has type "str") [assignment]
346346
pass
347347

348348
class A:

test-data/unit/check-functions.test

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -514,7 +514,7 @@ if int():
514514

515515
[builtins fixtures/tuple.pyi]
516516

517-
[case testDefaultArgumentExpressions]
517+
[case testDefaultParameterExpressions]
518518
import typing
519519
class B: pass
520520
class A: pass
@@ -524,52 +524,52 @@ def f(x: 'A' = A()) -> None:
524524
a = x # type: A
525525
[out]
526526

527-
[case testDefaultArgumentExpressions2]
527+
[case testDefaultParameterExpressions2]
528528
import typing
529529
class B: pass
530530
class A: pass
531531

532-
def f(x: 'A' = B()) -> None: # E: Incompatible default for argument "x" (default has type "B", argument has type "A")
532+
def f(x: 'A' = B()) -> None: # E: Incompatible default for parameter "x" (default has type "B", parameter has type "A")
533533
b = x # type: B # E: Incompatible types in assignment (expression has type "A", variable has type "B")
534534
a = x # type: A
535-
[case testDefaultArgumentExpressionsGeneric]
535+
[case testDefaultParameterExpressionsGeneric]
536536
from typing import TypeVar
537537
T = TypeVar('T', bound='A')
538538

539539
class B: pass
540540
class A: pass
541541

542-
def f(x: T = B()) -> None: # E: Incompatible default for argument "x" (default has type "B", argument has type "T")
542+
def f(x: T = B()) -> None: # E: Incompatible default for parameter "x" (default has type "B", parameter has type "T")
543543
b = x # type: B # E: Incompatible types in assignment (expression has type "T", variable has type "B")
544544
a = x # type: A
545-
[case testDefaultArgumentsWithSubtypes]
545+
[case testDefaultParametersWithSubtypes]
546546
import typing
547547
class A: pass
548548
class B(A): pass
549549

550-
def f(x: 'B' = A()) -> None: # E: Incompatible default for argument "x" (default has type "A", argument has type "B")
550+
def f(x: 'B' = A()) -> None: # E: Incompatible default for parameter "x" (default has type "A", parameter has type "B")
551551
pass
552552
def g(x: 'A' = B()) -> None:
553553
pass
554554
[out]
555555

556-
[case testMultipleDefaultArgumentExpressions]
556+
[case testMultipleDefaultParameterExpressions]
557557
import typing
558558
class A: pass
559559
class B: pass
560560

561-
def f(x: 'A' = B(), y: 'B' = B()) -> None: # E: Incompatible default for argument "x" (default has type "B", argument has type "A")
561+
def f(x: 'A' = B(), y: 'B' = B()) -> None: # E: Incompatible default for parameter "x" (default has type "B", parameter has type "A")
562562
pass
563563
def h(x: 'A' = A(), y: 'B' = B()) -> None:
564564
pass
565565
[out]
566566

567-
[case testMultipleDefaultArgumentExpressions2]
567+
[case testMultipleDefaultParameterExpressions2]
568568
import typing
569569
class A: pass
570570
class B: pass
571571

572-
def g(x: 'A' = A(), y: 'B' = A()) -> None: # E: Incompatible default for argument "y" (default has type "A", argument has type "B")
572+
def g(x: 'A' = A(), y: 'B' = A()) -> None: # E: Incompatible default for parameter "y" (default has type "A", parameter has type "B")
573573
pass
574574
[out]
575575

test-data/unit/check-inference.test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1435,7 +1435,7 @@ from typing import Callable
14351435
def f(a: Callable[..., None] = lambda *a, **k: None):
14361436
pass
14371437

1438-
def g(a: Callable[..., None] = lambda *a, **k: 1): # E: Incompatible default for argument "a" (default has type "def (*a: Any, **k: Any) -> int", argument has type "Callable[..., None]")
1438+
def g(a: Callable[..., None] = lambda *a, **k: 1): # E: Incompatible default for parameter "a" (default has type "def (*a: Any, **k: Any) -> int", parameter has type "Callable[..., None]")
14391439
pass
14401440
[builtins fixtures/dict.pyi]
14411441

test-data/unit/check-modules.test

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -495,19 +495,19 @@ m.f('') # E: Argument 1 to "f" has incompatible type "str"; expected "int"
495495
[file m.pyi]
496496
def f(x: int = ...) -> None: pass
497497

498-
[case testEllipsisDefaultArgValueInStub2]
498+
[case testEllipsisDefaultParamValueInStub2]
499499
import m
500500
def f1(x: int = ...) -> int: return 1
501501
def f2(x: int = '') -> int: return 1
502502
[file m.pyi]
503503
def g1(x: int = ...) -> int: pass
504504
def g2(x: int = '') -> int: pass
505505
[out]
506-
tmp/m.pyi:2: error: Incompatible default for argument "x" (default has type "str", argument has type "int")
507-
main:2: error: Incompatible default for argument "x" (default has type "ellipsis", argument has type "int")
508-
main:3: error: Incompatible default for argument "x" (default has type "str", argument has type "int")
506+
tmp/m.pyi:2: error: Incompatible default for parameter "x" (default has type "str", parameter has type "int")
507+
main:2: error: Incompatible default for parameter "x" (default has type "ellipsis", parameter has type "int")
508+
main:3: error: Incompatible default for parameter "x" (default has type "str", parameter has type "int")
509509

510-
[case testEllipsisDefaultArgValueInNonStub]
510+
[case testEllipsisDefaultParamValueInNonStub]
511511
def ok_1(x: int = ...) -> None: pass
512512
def ok_2(x: int = ...) -> None: ...
513513
def ok_3(x: int = ...) -> None: raise NotImplementedError
@@ -516,16 +516,16 @@ def ok_5(x: int = ...) -> None:
516516
"""Docstring here"""
517517
pass
518518

519-
def bad_1(x: int = ...) -> None: 1 # E: Incompatible default for argument "x" (default has type "ellipsis", argument has type "int")
520-
def bad_2(x: int = ...) -> None: # E: Incompatible default for argument "x" (default has type "ellipsis", argument has type "int")
519+
def bad_1(x: int = ...) -> None: 1 # E: Incompatible default for parameter "x" (default has type "ellipsis", parameter has type "int")
520+
def bad_2(x: int = ...) -> None: # E: Incompatible default for parameter "x" (default has type "ellipsis", parameter has type "int")
521521
"""Docstring here"""
522522
ok_1()
523-
def bad_3(x: int = ...) -> None: # E: Incompatible default for argument "x" (default has type "ellipsis", argument has type "int")
523+
def bad_3(x: int = ...) -> None: # E: Incompatible default for parameter "x" (default has type "ellipsis", parameter has type "int")
524524
raise Exception("Some other exception")
525525
[builtins fixtures/exception.pyi]
526526
[out]
527527

528-
[case testEllipsisDefaultArgValueInNonStubsOverload]
528+
[case testEllipsisDefaultParamValueInNonStubsOverload]
529529
from typing import overload, Union
530530

531531
Both = Union[int, str]
@@ -534,7 +534,7 @@ Both = Union[int, str]
534534
def foo(x: int, y: int = ...) -> int: ...
535535
@overload
536536
def foo(x: str, y: str = ...) -> str: ...
537-
def foo(x: Both, y: Both = ...) -> Both: # E: Incompatible default for argument "y" (default has type "ellipsis", argument has type "int | str")
537+
def foo(x: Both, y: Both = ...) -> Both: # E: Incompatible default for parameter "y" (default has type "ellipsis", parameter has type "int | str")
538538
return x
539539

540540
@overload
@@ -546,7 +546,7 @@ def bar(x: Both, y: Both = ...) -> Both:
546546
[builtins fixtures/exception.pyi]
547547
[out]
548548

549-
[case testEllipsisDefaultArgValueInNonStubsMethods]
549+
[case testEllipsisDefaultParamValueInNonStubsMethods]
550550
from typing import Generic, Protocol, TypeVar
551551
from abc import abstractmethod
552552

@@ -555,14 +555,14 @@ class Wrap(Generic[T]): ...
555555

556556
class MyProtocol(Protocol):
557557
def no_impl(self, x: Wrap[int] = ...) -> int: ...
558-
def default_impl(self, x: Wrap[int] = ...) -> int: return 3 # E: Incompatible default for argument "x" (default has type "ellipsis", argument has type "Wrap[int]")
558+
def default_impl(self, x: Wrap[int] = ...) -> int: return 3 # E: Incompatible default for parameter "x" (default has type "ellipsis", parameter has type "Wrap[int]")
559559

560560
class MyAbstractClass:
561561
@abstractmethod
562562
def no_impl(self, x: Wrap[int] = ...) -> int: raise NotImplementedError
563563

564564
@abstractmethod
565-
def default_impl(self, x: Wrap[int] = ...) -> int: return 3 # E: Incompatible default for argument "x" (default has type "ellipsis", argument has type "Wrap[int]")
565+
def default_impl(self, x: Wrap[int] = ...) -> int: return 3 # E: Incompatible default for parameter "x" (default has type "ellipsis", parameter has type "Wrap[int]")
566566
[builtins fixtures/exception.pyi]
567567
[out]
568568

test-data/unit/check-optional.test

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ f(None)
136136

137137
[case testNoInferOptionalFromDefaultNone]
138138
# flags: --no-implicit-optional
139-
def f(x: int = None) -> None: # E: Incompatible default for argument "x" (default has type "None", argument has type "int") \
139+
def f(x: int = None) -> None: # E: Incompatible default for parameter "x" (default has type "None", parameter has type "int") \
140140
# N: PEP 484 prohibits implicit Optional. Accordingly, mypy has changed its default to no_implicit_optional=True \
141141
# N: Use https://github.com/hauntsaninja/no_implicit_optional to automatically upgrade your codebase
142142
pass
@@ -153,7 +153,7 @@ f(None)
153153

154154
[case testNoInferOptionalFromDefaultNoneComment]
155155
# flags: --no-implicit-optional
156-
def f(x=None): # E: Incompatible default for argument "x" (default has type "None", argument has type "int") \
156+
def f(x=None): # E: Incompatible default for parameter "x" (default has type "None", parameter has type "int") \
157157
# N: PEP 484 prohibits implicit Optional. Accordingly, mypy has changed its default to no_implicit_optional=True \
158158
# N: Use https://github.com/hauntsaninja/no_implicit_optional to automatically upgrade your codebase
159159
# type: (int) -> None

test-data/unit/check-python312.test

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1138,13 +1138,13 @@ class C[T]:
11381138
x = '' # E: Incompatible types in assignment (expression has type "str", variable has type "int")
11391139
return a
11401140

1141-
[case testPEP695ArgumentDefault]
1141+
[case testPEP695ParameterDefault]
11421142
from typing import cast
11431143

11441144
def f[T](
11451145
x: T =
11461146
T # E: Name "T" is not defined \
1147-
# E: Incompatible default for argument "x" (default has type "TypeVar", argument has type "T")
1147+
# E: Incompatible default for parameter "x" (default has type "TypeVar", parameter has type "T")
11481148
) -> T:
11491149
return x
11501150

0 commit comments

Comments
 (0)