-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Carl Meyer <[email protected]>
- Loading branch information
1 parent
d4ee6ab
commit 0caab81
Showing
8 changed files
with
253 additions
and
47 deletions.
There are no files selected for viewing
118 changes: 118 additions & 0 deletions
118
crates/red_knot_python_semantic/resources/mdtest/suppressions/no_type_check.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
# `@no_type_check` | ||
|
||
> If a type checker supports the `no_type_check` decorator for functions, it should suppress all | ||
> type errors for the def statement and its body including any nested functions or classes. It | ||
> should also ignore all parameter and return type annotations and treat the function as if it were | ||
> unannotated. [source](https://typing.readthedocs.io/en/latest/spec/directives.html#no-type-check) | ||
## Error in the function body | ||
|
||
```py | ||
from typing import no_type_check | ||
|
||
@no_type_check | ||
def test() -> int: | ||
return a + 5 | ||
``` | ||
|
||
## Error in nested function | ||
|
||
```py | ||
from typing import no_type_check | ||
|
||
@no_type_check | ||
def test() -> int: | ||
def nested(): | ||
return a + 5 | ||
``` | ||
|
||
## Error in nested class | ||
|
||
```py | ||
from typing import no_type_check | ||
|
||
@no_type_check | ||
def test() -> int: | ||
class Nested: | ||
def inner(self): | ||
return a + 5 | ||
``` | ||
|
||
## Error in preceding decorator | ||
|
||
Don't suppress diagnostics for decorators appearing before the `no_type_check` decorator. | ||
|
||
```py | ||
from typing import no_type_check | ||
|
||
@unknown_decorator # error: [unresolved-reference] | ||
@no_type_check | ||
def test() -> int: | ||
return a + 5 | ||
``` | ||
|
||
## Error in following decorator | ||
|
||
Unlike Pyright and mypy, suppress diagnostics appearing after the `no_type_check` decorator. We do | ||
this because it more closely matches Python's runtime semantics of decorators. For more details, see | ||
the discussion on the | ||
[PR adding `@no_type_check` support](https://github.com/astral-sh/ruff/pull/15122#discussion_r1896869411). | ||
|
||
```py | ||
from typing import no_type_check | ||
|
||
@no_type_check | ||
@unknown_decorator | ||
def test() -> int: | ||
return a + 5 | ||
``` | ||
|
||
## Error in default value | ||
|
||
```py | ||
from typing import no_type_check | ||
|
||
@no_type_check | ||
def test(a: int = "test"): | ||
return x + 5 | ||
``` | ||
|
||
## Error in return value position | ||
|
||
```py | ||
from typing import no_type_check | ||
|
||
@no_type_check | ||
def test() -> Undefined: | ||
return x + 5 | ||
``` | ||
|
||
## `no_type_check` on classes isn't supported | ||
|
||
Red Knot does not support decorating classes with `no_type_check`. The behaviour of `no_type_check` | ||
when applied to classes is | ||
[not specified currently](https://typing.readthedocs.io/en/latest/spec/directives.html#no-type-check), | ||
and is not supported by Pyright or mypy. | ||
|
||
A future improvement might be to emit a diagnostic if a `no_type_check` annotation is applied to a | ||
class. | ||
|
||
```py | ||
from typing import no_type_check | ||
|
||
@no_type_check | ||
class Test: | ||
def test(self): | ||
return a + 5 # error: [unresolved-reference] | ||
``` | ||
|
||
## `type: ignore` comments in `@no_type_check` blocks | ||
|
||
```py | ||
from typing import no_type_check | ||
|
||
@no_type_check | ||
def test(): | ||
# error: [unused-ignore-comment] "Unused `knot: ignore` directive: 'unresolved-reference'" | ||
return x + 5 # knot: ignore[unresolved-reference] | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.