Skip to content

Commit 87a4c0b

Browse files
committed
clarify evolve of _what_
1 parent 34c8c94 commit 87a4c0b

File tree

2 files changed

+12
-10
lines changed

2 files changed

+12
-10
lines changed

mypy/plugins/attrs.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import mypy.plugin # To avoid circular imports.
99
from mypy.checker import TypeChecker
1010
from mypy.exprtotype import TypeTranslationError, expr_to_unanalyzed_type
11+
from mypy.messages import format_type_bare
1112
from mypy.nodes import (
1213
ARG_NAMED,
1314
ARG_NAMED_OPT,
@@ -926,13 +927,14 @@ def evolve_function_sig_callback(ctx: mypy.plugin.FunctionSigContext) -> Callabl
926927
inst_type = get_proper_type(inst_type)
927928
if isinstance(inst_type, AnyType):
928929
return ctx.default_signature
930+
inst_type_str = format_type_bare(inst_type)
929931

930932
# In practice, we're taking the initializer generated by _add_init and tweaking it
931933
# so that (a) its arguments are kw-only & optional, and (b) its return type is the instance's.
932934
attrs_init_type = _get_attrs_init_type(inst_type)
933935
if not attrs_init_type:
934936
ctx.api.fail(
935-
f'Argument 1 to "evolve" has incompatible type "{inst_type}"; expected an attrs class',
937+
f'Argument 1 to "evolve" has incompatible type "{inst_type_str}"; expected an attrs class',
936938
ctx.context,
937939
)
938940
return ctx.default_signature
@@ -941,5 +943,5 @@ def evolve_function_sig_callback(ctx: mypy.plugin.FunctionSigContext) -> Callabl
941943
arg_names=["inst"] + attrs_init_type.arg_names[1:],
942944
arg_kinds=[ARG_POS] + [ARG_NAMED_OPT for _ in attrs_init_type.arg_kinds[1:]],
943945
ret_type=inst_type,
944-
name=ctx.default_signature.name,
946+
name=f"{ctx.default_signature.name} of {inst_type_str}",
945947
)

test-data/unit/check-attr.test

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1889,12 +1889,12 @@ class C:
18891889
c = C(name='foo', b=Derived())
18901890
c = attr.evolve(c)
18911891
c = attr.evolve(c, name='foo')
1892-
c = attr.evolve(c, 'foo') # E: Too many positional arguments for "evolve"
1892+
c = attr.evolve(c, 'foo') # E: Too many positional arguments for "evolve" of "C"
18931893
c = attr.evolve(c, b=Derived())
18941894
c = attr.evolve(c, b=Base())
1895-
c = attr.evolve(c, b=Other()) # E: Argument "b" to "evolve" has incompatible type "Other"; expected "Base"
1896-
c = attr.evolve(c, name=42) # E: Argument "name" to "evolve" has incompatible type "int"; expected "str"
1897-
c = attr.evolve(c, foobar=42) # E: Unexpected keyword argument "foobar" for "evolve"
1895+
c = attr.evolve(c, b=Other()) # E: Argument "b" to "evolve" of "C" has incompatible type "Other"; expected "Base"
1896+
c = attr.evolve(c, name=42) # E: Argument "name" to "evolve" of "C" has incompatible type "int"; expected "str"
1897+
c = attr.evolve(c, foobar=42) # E: Unexpected keyword argument "foobar" for "evolve" of "C"
18981898

18991899
# test passing instance as 'inst' kw
19001900
c = attr.evolve(inst=c, name='foo')
@@ -1907,7 +1907,7 @@ def f() -> C:
19071907
c = attr.evolve(f(), name='foo')
19081908

19091909
# test 'inst' arg type check
1910-
attr.evolve(42, name='foo') # E: Argument 1 to "evolve" has incompatible type "Literal[42]?"; expected an attrs class
1910+
attr.evolve(42, name='foo') # E: Argument 1 to "evolve" has incompatible type "int"; expected an attrs class
19111911
attr.evolve(None, name='foo') # E: Argument 1 to "evolve" has incompatible type "None"; expected an attrs class
19121912

19131913
# test that all bets are off for 'Any'
@@ -1931,13 +1931,13 @@ class C:
19311931
c = C(name='foo')
19321932

19331933
c = attr.assoc(c, name='test')
1934-
c = attr.assoc(c, name=42) # E: Argument "name" to "assoc" has incompatible type "int"; expected "str"
1934+
c = attr.assoc(c, name=42) # E: Argument "name" to "assoc" of "C" has incompatible type "int"; expected "str"
19351935

19361936
c = attrs.evolve(c, name='test')
1937-
c = attrs.evolve(c, name=42) # E: Argument "name" to "evolve" has incompatible type "int"; expected "str"
1937+
c = attrs.evolve(c, name=42) # E: Argument "name" to "evolve" of "C" has incompatible type "int"; expected "str"
19381938

19391939
c = attrs.assoc(c, name='test')
1940-
c = attrs.assoc(c, name=42) # E: Argument "name" to "assoc" has incompatible type "int"; expected "str"
1940+
c = attrs.assoc(c, name=42) # E: Argument "name" to "assoc" of "C" has incompatible type "int"; expected "str"
19411941

19421942
[builtins fixtures/attr.pyi]
19431943
[typing fixtures/typing-medium.pyi]

0 commit comments

Comments
 (0)