Skip to content

Commit cbd71ee

Browse files
committed
streamline
1 parent 8a1a54c commit cbd71ee

File tree

2 files changed

+15
-28
lines changed

2 files changed

+15
-28
lines changed

mypy/plugins/attrs.py

Lines changed: 11 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -967,35 +967,18 @@ 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-
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?
970+
attrs_type = inst_type.upper_bound if isinstance(inst_type, TypeVarType) else inst_type
971+
attrs_init_type = None
972+
if isinstance(attrs_type, Instance):
978973
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?
974+
if attrs_init_type is None:
975+
ctx.api.fail(
976+
f'Argument 1 to "evolve" has a variable type "{inst_type_str}" not bound to an attrs class'
977+
if isinstance(inst_type, TypeVarType)
978+
else f'Argument 1 to "evolve" has incompatible type "{inst_type_str}"; expected an attrs class',
979+
ctx.context,
980+
)
981+
return ctx.default_signature
999982

1000983
attrs_init_type = expand_type_by_instance(attrs_init_type, attrs_type)
1001984

test-data/unit/check-attr.test

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2009,6 +2009,7 @@ class B(A):
20092009
TA = TypeVar('TA', bound=A)
20102010
TInt = TypeVar('TInt', bound=int)
20112011
TAny = TypeVar('TAny')
2012+
TNone = TypeVar('TNone', bound=None)
20122013

20132014

20142015
def f(t: TA) -> TA:
@@ -2026,6 +2027,9 @@ def g(t: TInt) -> None:
20262027
def h(t: TAny) -> None:
20272028
_ = attrs.evolve(t, x=42) # E: Argument 1 to "evolve" has a variable type "TAny" not bound to an attrs class
20282029

2030+
def q(t: TNone) -> None:
2031+
_ = attrs.evolve(t, x=42) # E: Argument 1 to "evolve" has a variable type "TNone" not bound to an attrs class
2032+
20292033
[builtins fixtures/attr.pyi]
20302034
[typing fixtures/typing-medium.pyi]
20312035

0 commit comments

Comments
 (0)