Skip to content

Commit de59c87

Browse files
committed
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 <mayuriphad656@gmail.com>
1 parent 53e157e commit de59c87

3 files changed

Lines changed: 32 additions & 1 deletion

File tree

‎CHANGELOG.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ See [0Ver](https://0ver.org/).
1717

1818
### Bugfixes
1919

20+
- Fixes the `mypy` plugin crashing with `INTERNAL ERROR` when `dekind`
21+
is called with an argument that is not a `KindN` instance;
22+
now only the regular `arg-type` error is reported
2023
- Relaxes `future` and `future_safe` decorator argument types from
2124
`Coroutine` to `Awaitable`, so plain `async def` functions wrapping
2225
another awaitable (instead of being coroutine functions themselves)

‎returns/contrib/mypy/_features/kind.py‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,9 @@ def dekind(ctx: FunctionContext) -> MypyType:
8585
So, ``dekind(KindN[T, int])`` will fail.
8686
"""
8787
kind = get_proper_type(ctx.arg_types[0][0])
88-
assert isinstance(kind, Instance) # mypy requires these lines
88+
if not isinstance(kind, Instance) or not kind.args:
89+
# Not a `KindN` instance: mypy already reports the `arg-type` error.
90+
return ctx.default_return_type
8991

9092
kind_inst = get_proper_type(kind.args[0])
9193

‎typesafety/test_primitives/test_hkt/test_dekind/test_dekind.yml‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,29 @@
7070
out: |
7171
main:8: error: dekind must be used with Instance as the first type argument [misc]
7272
main:8: note: Revealed type is "Any"
73+
74+
75+
- case: dekind_wrong_non_kind_argument
76+
disable_cache: false
77+
main: |
78+
from returns.primitives.hkt import dekind
79+
80+
reveal_type(dekind(1))
81+
out: |
82+
main:3: note: Revealed type is "Never"
83+
main:3: error: Argument 1 to "dekind" has incompatible type "int"; expected "KindN[Never, Never, Never, Never]" [arg-type]
84+
85+
86+
- case: dekind_wrong_typevar_argument
87+
disable_cache: false
88+
main: |
89+
from typing import TypeVar
90+
from returns.primitives.hkt import dekind
91+
92+
T = TypeVar('T')
93+
94+
def some(arg: T) -> None:
95+
reveal_type(dekind(arg))
96+
out: |
97+
main:7: note: Revealed type is "Never"
98+
main:7: error: Argument 1 to "dekind" has incompatible type "T"; expected "KindN[Never, Never, Never, Never]" [arg-type]

0 commit comments

Comments
 (0)