You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The mypy attrs plugin has custom support for attrs.evolve, which has different code branches for regular and generic types (starting here).
It admits attrs.has as a type guard for attrs.evolve if the original type is Any, but not if it's a generic type parameter T with no type bound.
To Reproduce
On python 3.12 (so lacking copy.replace):
defreplace[T](value: T, **kwargs: Any) ->T:
ifattrs.has(type(value)):
returnattrs.evolve(value, **kwargs)
else:
raiseNotImplementedError(f"replace is not implemented for {type(value)}")
Mypy reports, error: Argument 1 to "evolve" has a variable type "T" not bound to an attrs class [misc]
(In the actual code I'd do something else in other branches; this is intended as a replacement for copy.replace on python < 3.13, supporting different types besides attrs classes.)
Using the attr.AttrInstance protocol also doesn't work, which means I can't define my own type guard wrapping attrs.has. This code results in the same mypy error:
Perhaps relatedly, the attrs project used to have a TypeGuard[Type[attrs.AttrsInstance]] declaration on attrs.has but removed it at some point, so this doesn't work 'out of the box' as normal code. Presumably the mypy attrs plugin would override that type guard, but do all type checkers have custom attrs support? Why didn't a normal TypeGuard and protocol work (for evolve in particular, not for all of the attrs support)?
Bug Report
The mypy attrs plugin has custom support for
attrs.evolve
, which has different code branches for regular and generic types (starting here).It admits
attrs.has
as a type guard forattrs.evolve
if the original type isAny
, but not if it's a generic type parameterT
with no type bound.To Reproduce
On python 3.12 (so lacking
copy.replace
):Mypy reports,
error: Argument 1 to "evolve" has a variable type "T" not bound to an attrs class [misc]
(In the actual code I'd do something else in other branches; this is intended as a replacement for
copy.replace
on python < 3.13, supporting different types besides attrs classes.)Using the
attr.AttrInstance
protocol also doesn't work, which means I can't define my own type guard wrappingattrs.has
. This code results in the same mypy error:However, this code works (and is a viable workaround):
Expected Behavior
Mypy should honor
attrs.has
as a type guard forattrs.evolve
, whether the original type is generic or any other kind.The text was updated successfully, but these errors were encountered: