Skip to content

Commit 6e77e1b

Browse files
[flake8-pyi] Make example error out-of-the-box (PYI007, PYI008) (#19103)
## Summary Part of #18972 Both in one PR since they are in the same file No playground links since the playground does not support rules that only apply to PYI files PYI007 --- This PR makes [unrecognized-platform-check (PYI007)](https://docs.astral.sh/ruff/rules/unrecognized-platform-check/#unrecognized-platform-check-pyi007)'s example error out-of-the-box Old example: ``` PS ~\Desktop\New_folder\ruff>echo @" ``` ```py if sys.platform.startswith("linux"): # Linux specific definitions ... else: # Posix specific definitions ... ``` ``` "@ | uvx ruff check --isolated --preview --select PYI007 --stdin-filename "test.pyi" - ``` ``` All checks passed! ``` New example: ``` PS ~\Desktop\New_folder\ruff>echo @" ``` ```py import sys if sys.platform is "linux": # Linux specific definitions ... else: # Posix specific definitions ... ``` ``` "@ | uvx ruff check --isolated --preview --select PYI007 --stdin-filename "test.pyi" - ``` ```snap test.pyi:3:4: PYI007 Unrecognized `sys.platform` check | 1 | import sys 2 | 3 | if sys.platform is "linux": | ^^^^^^^^^^^^^^^^^^^^^^^ PYI007 4 | # Linux specific definitions 5 | ... | Found 1 error. ``` Imports were also added to the "use instead" section > [!NOTE] > `PYI007` is really hard to trigger, it's only specifically in the case of a comparison where the operator is not `!=` or `==`. The original example raises [complex-if-statement-in-stub (PYI002)](https://docs.astral.sh/ruff/rules/complex-if-statement-in-stub/#complex-if-statement-in-stub-pyi002) with or without the `import sys` PYI008 --- This PR makes [unrecognized-platform-name (PYI008)](https://docs.astral.sh/ruff/rules/unrecognized-platform-name/#unrecognized-platform-name-pyi008)'s example error out-of-the-box Old example: ``` PS ~\Desktop\New_folder\ruff>echo @" ``` ```py if sys.platform == "linus": ... ``` ``` "@ | uvx ruff check --isolated --preview --select PYI008 --stdin-filename "test.pyi" - ``` ``` All checks passed! ``` New example: ``` PS ~\Desktop\New_folder\ruff>echo @" ``` ```py import sys if sys.platform == "linus": ... ``` ``` "@ | uvx ruff check --isolated --preview --select PYI008 --stdin-filename "test.pyi" - ``` ```snap test.pyi:3:20: PYI008 Unrecognized platform `linus` | 1 | import sys 2 | 3 | if sys.platform == "linus": ... | ^^^^^^^ PYI008 | Found 1 error. ``` Imports were also added to the "use instead" section > [!NOTE] > The original example raises `PYI002` instead ## Test Plan <!-- How was it tested? --> N/A, no functionality/tests affected --------- Co-authored-by: Alex Waygood <[email protected]>
1 parent 845a1ee commit 6e77e1b

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ use crate::registry::Rule;
2121
///
2222
/// ## Example
2323
/// ```pyi
24-
/// if sys.platform.startswith("linux"):
24+
/// import sys
25+
///
26+
/// if sys.platform == "xunil"[::-1]:
2527
/// # Linux specific definitions
2628
/// ...
2729
/// else:
@@ -31,6 +33,8 @@ use crate::registry::Rule;
3133
///
3234
/// Instead, use a simple string comparison, such as `==` or `!=`:
3335
/// ```pyi
36+
/// import sys
37+
///
3438
/// if sys.platform == "linux":
3539
/// # Linux specific definitions
3640
/// ...
@@ -65,11 +69,15 @@ impl Violation for UnrecognizedPlatformCheck {
6569
///
6670
/// ## Example
6771
/// ```pyi
72+
/// import sys
73+
///
6874
/// if sys.platform == "linus": ...
6975
/// ```
7076
///
7177
/// Use instead:
7278
/// ```pyi
79+
/// import sys
80+
///
7381
/// if sys.platform == "linux": ...
7482
/// ```
7583
///

0 commit comments

Comments
 (0)