[ty] Add diagnosis for function with no return statement but with return type annotation (#18359)

## Summary

Partially implement https://github.com/astral-sh/ty/issues/538, 
```py
from pathlib import Path

def setup_test_project(registry_name: str, registry_url: str, project_dir: str) -> Path:
    pyproject_file = Path(project_dir) / "pyproject.toml"
    pyproject_file.write_text("...", encoding="utf-8")
```
As no return statement is defined in the function `setup_test_project`
with annotated return type `Path`, we provide the following diagnosis :

- error[invalid-return-type]: Function **always** implicitly returns
`None`, which is not assignable to return type `Path`

with a subdiagnostic : 
- note: Consider changing your return annotation to `-> None` or adding a `return` statement
 
## Test Plan

mdtests with snapshots to capture the subdiagnostic. I have to mention
that existing snapshots were modified since they now fall in this
category.

---------

Co-authored-by: Carl Meyer <carl@astral.sh>
This commit is contained in:
lipefree 2025-05-30 01:17:18 +02:00 committed by GitHub
parent 3445d1322d
commit 695de4f27f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 80 additions and 10 deletions

View file

@ -278,6 +278,20 @@ def f(cond: bool) -> int:
return 2
```
## Invalid implicit return type always None
<!-- snapshot-diagnostics -->
If the function has no `return` statement or if it has only bare `return` statement (no variable in
the return statement), then we show a diagnostic hint that the return annotation should be `-> None`
or a `return` statement should be added.
```py
# error: [invalid-return-type]
def f() -> int:
print("hello")
```
## NotImplemented
### Default Python version