[flake8-pyi] Implement PYI018 (#6018)

## Summary

Check for unused private `TypeVar`. See [original
implementation](2a86db8271/pyi.py (L1958)).

```
$ flake8 --select Y018 crates/ruff/resources/test/fixtures/flake8_pyi/PYI018.pyi

crates/ruff/resources/test/fixtures/flake8_pyi/PYI018.pyi:4:1: Y018 TypeVar "_T" is not used
crates/ruff/resources/test/fixtures/flake8_pyi/PYI018.pyi:5:1: Y018 TypeVar "_P" is not used
```

```
$ ./target/debug/ruff --select PYI018 crates/ruff/resources/test/fixtures/flake8_pyi/PYI018.pyi --no-cache

crates/ruff/resources/test/fixtures/flake8_pyi/PYI018.pyi:4:1: PYI018 TypeVar `_T` is never used
crates/ruff/resources/test/fixtures/flake8_pyi/PYI018.pyi:5:1: PYI018 TypeVar `_P` is never used
Found 2 errors.
```
In the file `unused_private_type_declaration.rs`, I'm planning to add
other rules that are similar to `PYI018` like the `PYI046`, `PYI047` and
`PYI049`.

ref #848

## Test Plan

Snapshots and manual runs of flake8.
This commit is contained in:
Victor Hugo Gomes 2023-07-26 19:56:15 -03:00 committed by GitHub
parent 788643f718
commit c0dbcb3434
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 149 additions and 0 deletions

View file

@ -94,6 +94,12 @@ impl<'a> Binding<'a> {
)
}
/// Return `true` if this [`Binding`] represents an private variable
/// (e.g., `_x` in `_x = "private variable"`)
pub const fn is_private_variable(&self) -> bool {
self.flags.contains(BindingFlags::PRIVATE_DECLARATION)
}
/// Return `true` if this binding redefines the given binding.
pub fn redefines(&self, existing: &'a Binding) -> bool {
match &self.kind {
@ -264,6 +270,14 @@ bitflags! {
/// __all__ = [1]
/// ```
const INVALID_ALL_OBJECT = 1 << 6;
/// The binding represents a private declaration.
///
/// For example, the binding could be `_T` in:
/// ```python
/// _T = "This is a private variable"
/// ```
const PRIVATE_DECLARATION = 1 << 7;
}
}