8000 remove walrus operator, more tests, more verbose messages · python/mypy@8a1a54c · GitHub
[go: up one dir, main page]

Skip to content

Commit 8a1a54c

Browse files
committed
remove walrus operator, more tests, more verbose messages
1 parent 2ee391c commit 8a1a54c

File tree

2 files changed

+42
-15
lines changed

2 files changed

+42
-15
lines changed

mypy/plugins/attrs.py

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -967,18 +967,37 @@ def evolve_function_sig_callback(ctx: mypy.plugin.FunctionSigContext) -> Callabl
967967
return ctx.default_signature # evolve(Any, ....) -> Any
968968
# We stringify it first, so that TypeVars maintain their name.
969969
inst_type_str = format_type_bare(inst_type)
970-
upper_bound = inst_type.upper_bound if isinstance(inst_type, TypeVarType) else inst_type
971-
if not (
972-
isinstance(upper_bound, Instance)
973-
and (attrs_init_type := _get_attrs_init_type(upper_bound))
974-
):
975-
ctx.api.fail(
976-
f'Argument 1 to "evolve" has incompatible type "{inst_type_str}"; expected an attrs class',
977-
ctx.context,
978-
)
979-
return ctx.default_signature
970+
if isinstance(inst_type, TypeVarType):
971+
attrs_type = inst_type.upper_bound
972+
if not isinstance(attrs_type, Instance):
973+
ctx.api.fail(
974+
f'Argument 1 to "evolve" has a variable type "{inst_type_str}" with unexpected upper bounds',
975+
ctx.context,
976+
)
977+
return ctx.default_signature # TODO: is this possible?
978+
attrs_init_type = _get_attrs_init_type(attrs_type)
979+
if attrs_init_type is None:
980+
ctx.api.fail(
981+
f'Argument 1 to "evolve" has a variable type "{inst_type_str}" not bound to an attrs class',
982+
ctx.context,
983+
)
984+
return ctx.default_signature
985+
else:
986+
attrs_type = inst_type
987+
if not isinstance(attrs_type, Instance):
988+
ctx.api.fail(
989+
f'Argument 1 to "evolve" has incompatible type "{inst_type_str}"', ctx.context
990+
)
991+
return ctx.default_signature # TODO: is this possible?
992+
attrs_init_type = _get_attrs_init_type(attrs_type)
993+
if attrs_init_type is None:
994+
ctx.api.fail(
995+
f'Argument 1 to "evolve" has incompatible type "{inst_type_str}"; expected an attrs class',
996+
ctx.context,
997+
)
998+
return ctx.default_signature # TODO: is this possible?
980999

981-
attrs_init_type = expand_type_by_instance(attrs_init_type, upper_bound)
1000+
attrs_init_type = expand_type_by_instance(attrs_init_type, attrs_type)
9821001

9831002
# AttrClass.__init__ has the following signature (or similar, if having kw-only & defaults):
9841003
# def __init__(self, attr1: Type1, attr2: Type2) -> None:

test-data/unit/check-attr.test

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2006,18 +2006,26 @@ class B(A):
20062006
pass
20072007

20082008

2009-
T = TypeVar('T', bound=A)
2009+
TA = TypeVar('TA', bound=A)
2010+
TInt = TypeVar('TInt', bound=int)
2011+
TAny = TypeVar('TAny')
20102012

20112013

2012-
def f(t: T) -> T:
2014+
def f(t: TA) -> TA:
20132015
t2 = attrs.evolve(t, x=42)
2014-
reveal_type(t2) # N: Revealed type is "T`-1"
2015-
t3 = attrs.evolve(t, x='42') # E: Argument "x" to "evolve" of "T" has incompatible type "str"; expected "int"
2016+
reveal_type(t2) # N: Revealed type is "TA`-1"
2017+
t3 = attrs.evolve(t, x='42') # E: Argument "x" to "evolve" of "TA" has incompatible type "str"; expected "int"
20162018
return t2
20172019

20182020
f(A(x=42))
20192021
f(B(x=42))
20202022

2023+
def g(t: TInt) -> None:
2024+
_ = attrs.evolve(t, x=42) # E: Argument 1 to "evolve" has a variable type "TInt" not bound to an attrs class
2025+
2026+
def h(t: TAny) -> None:
2027+
_ = attrs.evolve(t, x=42) # E: Argument 1 to "evolve" has a variable type "TAny" not bound to an attrs class
2028+
20212029
[builtins fixtures/attr.pyi]
20222030
[typing fixtures/typing-medium.pyi]
20232031

0 commit comments

Comments
 (0)
0