mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-03 18:28:24 +00:00
[flake8-pyi
]: add rules for unrecognized platform check (PYI007, PYI008) (#2805)
Add two [flake8-pyi](https://github.com/PyCQA/flake8-pyi) rules (Y007, Y008). ref: #848 The specifications are described in [PEP 484 - Version and platform checking](https://peps.python.org/pep-0484/#version-and-platform-checking) The original implementation in flake8-pyi is shown below. - Implemention:66f28a4407/pyi.py (L1429-L1443)
- Tests:66f28a4407/tests/sysplatform.pyi
This commit is contained in:
parent
ca8a122889
commit
fc465cc2af
18 changed files with 358 additions and 0 deletions
33
docs/rules/unrecognized-platform-check.md
Normal file
33
docs/rules/unrecognized-platform-check.md
Normal file
|
@ -0,0 +1,33 @@
|
|||
# unrecognized-platform-check (PYI007)
|
||||
|
||||
Derived from the **flake8-pyi** linter.
|
||||
|
||||
## What it does
|
||||
Check for unrecognized `sys.platform` checks. Platform checks should be
|
||||
simple string comparisons.
|
||||
|
||||
**Note**: this rule is only enabled in `.pyi` stub files.
|
||||
|
||||
## Why is this bad?
|
||||
Some `sys.platform` checks are too complex for type checkers to
|
||||
understand, and thus result in false positives. `sys.platform` checks
|
||||
should be simple string comparisons, like `sys.platform == "linux"`.
|
||||
|
||||
## Example
|
||||
```python
|
||||
if sys.platform.startswith("linux"):
|
||||
# Linux specific definitions
|
||||
else:
|
||||
# Posix specific definitions
|
||||
```
|
||||
|
||||
Instead, use a simple string comparison, such as `==` or `!=`:
|
||||
```python
|
||||
if sys.platform == "linux":
|
||||
# Linux specific definitions
|
||||
else:
|
||||
# Posix specific definitions
|
||||
```
|
||||
|
||||
## References
|
||||
- [PEP 484](https://peps.python.org/pep-0484/#version-and-platform-checking)
|
30
docs/rules/unrecognized-platform-name.md
Normal file
30
docs/rules/unrecognized-platform-name.md
Normal file
|
@ -0,0 +1,30 @@
|
|||
# unrecognized-platform-name (PYI008)
|
||||
|
||||
Derived from the **flake8-pyi** linter.
|
||||
|
||||
## What it does
|
||||
Check for unrecognized platform names in `sys.platform` checks.
|
||||
|
||||
**Note**: this rule is only enabled in `.pyi` stub files.
|
||||
|
||||
## Why is this bad?
|
||||
If a `sys.platform` check compares to a platform name outside of a
|
||||
small set of known platforms (e.g. "linux", "win32", etc.), it's likely
|
||||
a typo or a platform name that is not recognized by type checkers.
|
||||
|
||||
The list of known platforms is: "linux", "win32", "cygwin", "darwin".
|
||||
|
||||
## Example
|
||||
```python
|
||||
if sys.platform == "linus":
|
||||
...
|
||||
```
|
||||
|
||||
Use instead:
|
||||
```python
|
||||
if sys.platform == "linux":
|
||||
...
|
||||
```
|
||||
|
||||
## References
|
||||
- [PEP 484](https://peps.python.org/pep-0484/#version-and-platform-checking)
|
Loading…
Add table
Add a link
Reference in a new issue