Skip to content

Commit 2e75cfb

Browse files
authored
Format PYI examples in docs as .pyi-file snippets (#13116)
1 parent cfafaa7 commit 2e75cfb

37 files changed

+125
-148
lines changed

crates/ruff_linter/src/rules/flake8_pyi/rules/any_eq_ne_annotation.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,14 @@ use crate::checkers::ast::Checker;
2727
///
2828
/// ## Example
2929
///
30-
/// ```python
30+
/// ```pyi
3131
/// class Foo:
3232
/// def __eq__(self, obj: typing.Any) -> bool: ...
3333
/// ```
3434
///
3535
/// Use instead:
3636
///
37-
/// ```python
37+
/// ```pyi
3838
/// class Foo:
3939
/// def __eq__(self, obj: object) -> bool: ...
4040
/// ```

crates/ruff_linter/src/rules/flake8_pyi/rules/bad_version_info_comparison.rs

+6-12
Original file line numberDiff line numberDiff line change
@@ -34,19 +34,17 @@ use crate::registry::Rule;
3434
/// ```
3535
///
3636
/// ## Example
37-
/// ```python
37+
/// ```pyi
3838
/// import sys
3939
///
40-
/// if sys.version_info > (3, 8):
41-
/// ...
40+
/// if sys.version_info > (3, 8): ...
4241
/// ```
4342
///
4443
/// Use instead:
45-
/// ```python
44+
/// ```pyi
4645
/// import sys
4746
///
48-
/// if sys.version_info >= (3, 9):
49-
/// ...
47+
/// if sys.version_info >= (3, 9): ...
5048
/// ```
5149
#[violation]
5250
pub struct BadVersionInfoComparison;
@@ -70,27 +68,23 @@ impl Violation for BadVersionInfoComparison {
7068
///
7169
/// ## Example
7270
///
73-
/// ```python
71+
/// ```pyi
7472
/// import sys
7573
///
7674
/// if sys.version_info < (3, 10):
77-
///
7875
/// def read_data(x, *, preserve_order=True): ...
7976
///
8077
/// else:
81-
///
8278
/// def read_data(x): ...
8379
/// ```
8480
///
8581
/// Use instead:
8682
///
87-
/// ```python
83+
/// ```pyi
8884
/// if sys.version_info >= (3, 10):
89-
///
9085
/// def read_data(x): ...
9186
///
9287
/// else:
93-
///
9488
/// def read_data(x, *, preserve_order=True): ...
9589
/// ```
9690
#[violation]

crates/ruff_linter/src/rules/flake8_pyi/rules/collections_named_tuple.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,16 @@ use crate::checkers::ast::Checker;
2121
/// precisely.
2222
///
2323
/// ## Example
24-
/// ```python
24+
/// ```pyi
2525
/// from collections import namedtuple
2626
///
2727
/// person = namedtuple("Person", ["name", "age"])
2828
/// ```
2929
///
3030
/// Use instead:
31-
/// ```python
31+
/// ```pyi
3232
/// from typing import NamedTuple
3333
///
34-
///
3534
/// class Person(NamedTuple):
3635
/// name: str
3736
/// age: int

crates/ruff_linter/src/rules/flake8_pyi/rules/complex_assignment_in_stub.rs

+2-5
Original file line numberDiff line numberDiff line change
@@ -20,27 +20,24 @@ use crate::checkers::ast::Checker;
2020
///
2121
/// ## Example
2222
///
23-
/// ```python
23+
/// ```pyi
2424
/// from typing import TypeAlias
2525
///
2626
/// a = b = int
2727
///
28-
///
2928
/// class Klass: ...
3029
///
31-
///
3230
/// Klass.X: TypeAlias = int
3331
/// ```
3432
///
3533
/// Use instead:
3634
///
37-
/// ```python
35+
/// ```pyi
3836
/// from typing import TypeAlias
3937
///
4038
/// a: TypeAlias = int
4139
/// b: TypeAlias = int
4240
///
43-
///
4441
/// class Klass:
4542
/// X: TypeAlias = int
4643
/// ```

crates/ruff_linter/src/rules/flake8_pyi/rules/complex_if_statement_in_stub.rs

+4-6
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,17 @@ use crate::checkers::ast::Checker;
1616
/// analyze your code.
1717
///
1818
/// ## Example
19-
/// ```python
19+
/// ```pyi
2020
/// import sys
2121
///
22-
/// if (3, 10) <= sys.version_info < (3, 12):
23-
/// ...
22+
/// if (3, 10) <= sys.version_info < (3, 12): ...
2423
/// ```
2524
///
2625
/// Use instead:
27-
/// ```python
26+
/// ```pyi
2827
/// import sys
2928
///
30-
/// if sys.version_info >= (3, 10) and sys.version_info < (3, 12):
31-
/// ...
29+
/// if sys.version_info >= (3, 10) and sys.version_info < (3, 12): ...
3230
/// ```
3331
///
3432
/// ## References

crates/ruff_linter/src/rules/flake8_pyi/rules/custom_type_var_return_type.rs

+2-7
Original file line numberDiff line numberDiff line change
@@ -25,27 +25,22 @@ use crate::checkers::ast::Checker;
2525
///
2626
/// ## Example
2727
///
28-
/// ```python
28+
/// ```pyi
2929
/// class Foo:
3030
/// def __new__(cls: type[_S], *args: str, **kwargs: int) -> _S: ...
31-
///
3231
/// def foo(self: _S, arg: bytes) -> _S: ...
33-
///
3432
/// @classmethod
3533
/// def bar(cls: type[_S], arg: int) -> _S: ...
3634
/// ```
3735
///
3836
/// Use instead:
3937
///
40-
/// ```python
38+
/// ```pyi
4139
/// from typing import Self
4240
///
43-
///
4441
/// class Foo:
4542
/// def __new__(cls, *args: str, **kwargs: int) -> Self: ...
46-
///
4743
/// def foo(self, arg: bytes) -> Self: ...
48-
///
4944
/// @classmethod
5045
/// def bar(cls, arg: int) -> Self: ...
5146
/// ```

crates/ruff_linter/src/rules/flake8_pyi/rules/docstring_in_stubs.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,15 @@ use crate::checkers::ast::Checker;
1515
///
1616
/// ## Example
1717
///
18-
/// ```python
18+
/// ```pyi
1919
/// def func(param: int) -> str:
2020
/// """This is a docstring."""
2121
/// ...
2222
/// ```
2323
///
2424
/// Use instead:
2525
///
26-
/// ```python
26+
/// ```pyi
2727
/// def func(param: int) -> str: ...
2828
/// ```
2929
#[violation]

crates/ruff_linter/src/rules/flake8_pyi/rules/ellipsis_in_non_empty_class_body.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@ use crate::fix;
1515
/// is redundant.
1616
///
1717
/// ## Example
18-
/// ```python
18+
/// ```pyi
1919
/// class Foo:
2020
/// ...
2121
/// value: int
2222
/// ```
2323
///
2424
/// Use instead:
25-
/// ```python
25+
/// ```pyi
2626
/// class Foo:
2727
/// value: int
2828
/// ```

crates/ruff_linter/src/rules/flake8_pyi/rules/exit_annotations.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,9 @@ use crate::checkers::ast::Checker;
2424
///
2525
/// ## Example
2626
///
27-
/// ```python
27+
/// ```pyi
2828
/// from types import TracebackType
2929
///
30-
///
3130
/// class Foo:
3231
/// def __exit__(
3332
/// self, typ: BaseException, exc: BaseException, tb: TracebackType
@@ -36,10 +35,9 @@ use crate::checkers::ast::Checker;
3635
///
3736
/// Use instead:
3837
///
39-
/// ```python
38+
/// ```pyi
4039
/// from types import TracebackType
4140
///
42-
///
4341
/// class Foo:
4442
/// def __exit__(
4543
/// self,

crates/ruff_linter/src/rules/flake8_pyi/rules/no_return_argument_annotation.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,14 @@ use crate::settings::types::PythonVersion::Py311;
2222
/// members).
2323
///
2424
/// ## Example
25-
/// ```python
25+
/// ```pyi
2626
/// from typing import NoReturn
2727
///
2828
/// def foo(x: NoReturn): ...
2929
/// ```
3030
///
3131
/// Use instead:
32-
/// ```python
32+
/// ```pyi
3333
/// from typing import Never
3434
///
3535
/// def foo(x: Never): ...

crates/ruff_linter/src/rules/flake8_pyi/rules/non_empty_stub_body.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@ use crate::checkers::ast::Checker;
1515
/// for this purpose.
1616
///
1717
/// ## Example
18-
/// ```python
18+
/// ```pyi
1919
/// def double(x: int) -> int:
2020
/// return x * 2
2121
/// ```
2222
///
2323
/// Use instead:
24-
/// ```python
24+
/// ```pyi
2525
/// def double(x: int) -> int: ...
2626
/// ```
2727
///

crates/ruff_linter/src/rules/flake8_pyi/rules/non_self_return_type.rs

+2-9
Original file line numberDiff line numberDiff line change
@@ -51,30 +51,23 @@ use crate::checkers::ast::Checker;
5151
///
5252
/// ## Example
5353
///
54-
/// ```python
54+
/// ```pyi
5555
/// class Foo:
5656
/// def __new__(cls, *args: Any, **kwargs: Any) -> Foo: ...
57-
///
5857
/// def __enter__(self) -> Foo: ...
59-
///
6058
/// async def __aenter__(self) -> Foo: ...
61-
///
6259
/// def __iadd__(self, other: Foo) -> Foo: ...
6360
/// ```
6461
///
6562
/// Use instead:
6663
///
67-
/// ```python
64+
/// ```pyi
6865
/// from typing_extensions import Self
6966
///
70-
///
7167
/// class Foo:
7268
/// def __new__(cls, *args: Any, **kwargs: Any) -> Self: ...
73-
///
7469
/// def __enter__(self) -> Self: ...
75-
///
7670
/// async def __aenter__(self) -> Self: ...
77-
///
7871
/// def __iadd__(self, other: Foo) -> Self: ...
7972
/// ```
8073
/// ## References

crates/ruff_linter/src/rules/flake8_pyi/rules/numeric_literal_too_long.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@ use crate::checkers::ast::Checker;
2020
///
2121
/// ## Example
2222
///
23-
/// ```python
23+
/// ```pyi
2424
/// def foo(arg: int = 693568516352839939918568862861217771399698285293568) -> None: ...
2525
/// ```
2626
///
2727
/// Use instead:
2828
///
29-
/// ```python
29+
/// ```pyi
3030
/// def foo(arg: int = ...) -> None: ...
3131
/// ```
3232
#[violation]

crates/ruff_linter/src/rules/flake8_pyi/rules/pass_in_class_body.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@ use crate::fix;
1515
/// stubs.
1616
///
1717
/// ## Example
18-
/// ```python
18+
/// ```pyi
1919
/// class MyClass:
2020
/// x: int
2121
/// pass
2222
/// ```
2323
///
2424
/// Use instead:
25-
/// ```python
25+
/// ```pyi
2626
/// class MyClass:
2727
/// x: int
2828
/// ```

crates/ruff_linter/src/rules/flake8_pyi/rules/pass_statement_stub_body.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@ use crate::checkers::ast::Checker;
1313
/// in stub files.
1414
///
1515
/// ## Example
16-
/// ```python
16+
/// ```pyi
1717
/// def foo(bar: int) -> list[int]: pass
1818
/// ```
1919
///
2020
/// Use instead:
21-
/// ```python
21+
/// ```pyi
2222
/// def foo(bar: int) -> list[int]: ...
2323
/// ```
2424
///

crates/ruff_linter/src/rules/flake8_pyi/rules/pre_pep570_positional_argument.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@ use crate::settings::types::PythonVersion;
1717
///
1818
/// ## Example
1919
///
20-
/// ```python
20+
/// ```pyi
2121
/// def foo(__x: int) -> None: ...
2222
/// ```
2323
///
2424
/// Use instead:
2525
///
26-
/// ```python
26+
/// ```pyi
2727
/// def foo(x: int, /) -> None: ...
2828
/// ```
2929
///

crates/ruff_linter/src/rules/flake8_pyi/rules/prefix_type_params.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,14 @@ impl fmt::Display for VarKind {
3333
/// internal to the stub.
3434
///
3535
/// ## Example
36-
/// ```python
36+
/// ```pyi
3737
/// from typing import TypeVar
3838
///
3939
/// T = TypeVar("T")
4040
/// ```
4141
///
4242
/// Use instead:
43-
/// ```python
43+
/// ```pyi
4444
/// from typing import TypeVar
4545
///
4646
/// _T = TypeVar("_T")

crates/ruff_linter/src/rules/flake8_pyi/rules/quoted_annotation_in_stub.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@ use crate::checkers::ast::Checker;
1616
///
1717
/// ## Example
1818
///
19-
/// ```python
19+
/// ```pyi
2020
/// def function() -> "int": ...
2121
/// ```
2222
///
2323
/// Use instead:
2424
///
25-
/// ```python
25+
/// ```pyi
2626
/// def function() -> int: ...
2727
/// ```
2828
///

0 commit comments

Comments
 (0)