[red-knot] detect invalid return type (#16540)

## Summary

This PR closes #16248.

If the return type of the function isn't assignable to the one
specified, an `invalid-return-type` error occurs.
I thought it would be better to report this as a different kind of error
than the `invalid-assignment` error, so I defined this as a new error.

## Test Plan

All type inconsistencies in the test cases have been replaced with
appropriate ones.

---------

Co-authored-by: Carl Meyer <carl@astral.sh>
This commit is contained in:
Shunsuke Shibayama
2025-03-12 10:58:59 +09:00
committed by GitHub
parent e17cd350b6
commit 78b5f0b165
43 changed files with 983 additions and 103 deletions

View File

@@ -49,5 +49,5 @@ def _(m: int, n: int):
def _(s: bytes) -> bytes:
byte_slice2 = s[0:5]
# TODO: Support overloads... Should be `bytes`
reveal_type(byte_slice2) # revealed: @Todo(return type of decorated function)
return reveal_type(byte_slice2) # revealed: @Todo(return type of decorated function)
```

View File

@@ -13,7 +13,7 @@ a = NotSubscriptable[0] # error: "Cannot subscript object of type `Literal[NotS
```py
class Identity:
def __class_getitem__(cls, item: int) -> str:
return item
return str(item)
reveal_type(Identity[0]) # revealed: str
```
@@ -25,7 +25,7 @@ def _(flag: bool):
class UnionClassGetItem:
if flag:
def __class_getitem__(cls, item: int) -> str:
return item
return str(item)
else:
def __class_getitem__(cls, item: int) -> int:
return item
@@ -39,7 +39,7 @@ def _(flag: bool):
def _(flag: bool):
class A:
def __class_getitem__(cls, item: int) -> str:
return item
return str(item)
class B:
def __class_getitem__(cls, item: int) -> int: