From de59c8752225ea9a2c4eb2b232cf7b10a4a2ee05 Mon Sep 17 00:00:00 2001 From: mayuriphad Date: Sat, 24 Oct 2026 10:25:49 +0530 Subject: [PATCH] Fix mypy plugin crash when dekind gets a non-KindN argument The dekind plugin hook asserted that the argument type is an Instance and then read its first type argument. For an argument like 1 or an unbound TypeVar the assert or the index lookup raised, which mypy reports as an INTERNAL ERROR. Fall back to the default return type instead, so only the regular arg-type error is shown. Closes #2555 Signed-off-by: mayuriphad --- CHANGELOG.md | 3 +++ returns/contrib/mypy/_features/kind.py | 4 ++- .../test_hkt/test_dekind/test_dekind.yml | 26 +++++++++++++++++++ 3 files changed, 32 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 448b1b7e4..2d1523bbb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,9 @@ See [0Ver](https://0ver.org/). ### Bugfixes +- Fixes the `mypy` plugin crashing with `INTERNAL ERROR` when `dekind` + is called with an argument that is not a `KindN` instance; + now only the regular `arg-type` error is reported - Relaxes `future` and `future_safe` decorator argument types from `Coroutine` to `Awaitable`, so plain `async def` functions wrapping another awaitable (instead of being coroutine functions themselves) diff --git a/returns/contrib/mypy/_features/kind.py b/returns/contrib/mypy/_features/kind.py index 9e6f5ae2d..d06cf08e0 100644 --- a/returns/contrib/mypy/_features/kind.py +++ b/returns/contrib/mypy/_features/kind.py @@ -85,7 +85,9 @@ def dekind(ctx: FunctionContext) -> MypyType: So, ``dekind(KindN[T, int])`` will fail. """ kind = get_proper_type(ctx.arg_types[0][0]) - assert isinstance(kind, Instance) # mypy requires these lines + if not isinstance(kind, Instance) or not kind.args: + # Not a `KindN` instance: mypy already reports the `arg-type` error. + return ctx.default_return_type kind_inst = get_proper_type(kind.args[0]) diff --git a/typesafety/test_primitives/test_hkt/test_dekind/test_dekind.yml b/typesafety/test_primitives/test_hkt/test_dekind/test_dekind.yml index f1ce5e2d2..54d8b2fc4 100644 --- a/typesafety/test_primitives/test_hkt/test_dekind/test_dekind.yml +++ b/typesafety/test_primitives/test_hkt/test_dekind/test_dekind.yml @@ -70,3 +70,29 @@ out: | main:8: error: dekind must be used with Instance as the first type argument [misc] main:8: note: Revealed type is "Any" + + +- case: dekind_wrong_non_kind_argument + disable_cache: false + main: | + from returns.primitives.hkt import dekind + + reveal_type(dekind(1)) + out: | + main:3: note: Revealed type is "Never" + main:3: error: Argument 1 to "dekind" has incompatible type "int"; expected "KindN[Never, Never, Never, Never]" [arg-type] + + +- case: dekind_wrong_typevar_argument + disable_cache: false + main: | + from typing import TypeVar + from returns.primitives.hkt import dekind + + T = TypeVar('T') + + def some(arg: T) -> None: + reveal_type(dekind(arg)) + out: | + main:7: note: Revealed type is "Never" + main:7: error: Argument 1 to "dekind" has incompatible type "T"; expected "KindN[Never, Never, Never, Never]" [arg-type]