Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement PYI047 #6134

Merged
merged 12 commits into from
Jul 29, 2023
Merged

Implement PYI047 #6134

merged 12 commits into from
Jul 29, 2023

Commits on Jul 27, 2023

  1. Implement PYI047

    /// ## What it does
    /// Checks for the presence of unused private `typing.TypeAlias` definitions.
    ///
    /// ## Why is this bad?
    /// A private `typing.TypeAlias` that is defined but not used is likely a
    /// mistake, and should either be used, made public, or removed to avoid
    /// confusion.
    ///
    /// ## Example
    /// ```python
    /// import typing
    ///
    /// _UnusedTypeAlias: typing.TypeAlias = int
    /// ```
    ///
    /// Use instead:
    /// ```python
    /// import typing
    ///
    /// _UsedTypeAlias: typing.TypeAlias = int
    ///
    /// def func(arg: _UsedTypeAlias) -> _UsedTypeAlias:
    ///     ...
    /// ```
    LaBatata101 committed Jul 27, 2023
    Configuration menu
    Copy the full SHA
    3d80969 View commit details
    Browse the repository at this point in the history
  2. fix doc formatting

    LaBatata101 committed Jul 27, 2023
    Configuration menu
    Copy the full SHA
    b99a560 View commit details
    Browse the repository at this point in the history
  3. fix formatting

    LaBatata101 committed Jul 27, 2023
    Configuration menu
    Copy the full SHA
    1fe05f6 View commit details
    Browse the repository at this point in the history

Commits on Jul 28, 2023

  1. Configuration menu
    Copy the full SHA
    0a61d87 View commit details
    Browse the repository at this point in the history
  2. Only run unused private type rules over finalized bindings (astral-sh…

    …#6142)
    
    In astral-sh#6134 and astral-sh#6136, we see some false positives for "shadowed" class
    definitions. For example, here, the first definition is flagged as
    unused, since from the perspective of the semantic model (which doesn't
    understand branching), it appears to be immediately shadowed in the
    `else`, and thus never used:
    
    ```python
    if sys.version_info >= (3, 11):
        class _RootLoggerConfiguration(TypedDict, total=False):
            level: _Level
            filters: Sequence[str | _FilterType]
            handlers: Sequence[str]
    
    else:
        class _RootLoggerConfiguration(TypedDict, total=False):
            level: _Level
            filters: Sequence[str]
            handlers: Sequence[str]
    ```
    
    Instead of looking at _all_ bindings, we should instead look at the
    "live" bindings, which is similar to how other rules (like unused
    variables detection) is structured. We thus move the rule from
    `bindings.rs` (which iterates over _all_ bindings, regardless of whether
    they're shadowed) to `deferred_scopes.rs`, which iterates over all
    "live" bindings once a scope has been fully analyzed.
    
    `cargo test`
    charliermarsh authored and LaBatata101 committed Jul 28, 2023
    Configuration menu
    Copy the full SHA
    ad3081d View commit details
    Browse the repository at this point in the history
  3. Configuration menu
    Copy the full SHA
    74cdfdb View commit details
    Browse the repository at this point in the history
  4. Avoid refactoring x[:1]-like slices in RUF015 (astral-sh#6150)

    ## Summary
    
    Right now, `RUF015` will try to rewrite `x[:1]` as `[next(x)]`. This
    isn't equivalent if `x`, for example, is empty, where slicing like
    `x[:1]` is forgiving, but `next` raises `StopIteration`. For me this is
    a little too much of a deviation to be comfortable with, and most of the
    value in this rule is the `x[0]` to `next(x)` conversion anyway.
    
    Closes astral-sh#6148.
    charliermarsh authored and LaBatata101 committed Jul 28, 2023
    Configuration menu
    Copy the full SHA
    c93bbbf View commit details
    Browse the repository at this point in the history
  5. Implement PYI047

    /// ## What it does
    /// Checks for the presence of unused private `typing.TypeAlias` definitions.
    ///
    /// ## Why is this bad?
    /// A private `typing.TypeAlias` that is defined but not used is likely a
    /// mistake, and should either be used, made public, or removed to avoid
    /// confusion.
    ///
    /// ## Example
    /// ```python
    /// import typing
    ///
    /// _UnusedTypeAlias: typing.TypeAlias = int
    /// ```
    ///
    /// Use instead:
    /// ```python
    /// import typing
    ///
    /// _UsedTypeAlias: typing.TypeAlias = int
    ///
    /// def func(arg: _UsedTypeAlias) -> _UsedTypeAlias:
    ///     ...
    /// ```
    LaBatata101 committed Jul 28, 2023
    Configuration menu
    Copy the full SHA
    926562a View commit details
    Browse the repository at this point in the history
  6. fix doc formatting

    LaBatata101 committed Jul 28, 2023
    Configuration menu
    Copy the full SHA
    b5a3e85 View commit details
    Browse the repository at this point in the history
  7. Avoid false positives

    These changes are based on astral-sh#6142
    LaBatata101 committed Jul 28, 2023
    Configuration menu
    Copy the full SHA
    99c0347 View commit details
    Browse the repository at this point in the history

Commits on Jul 29, 2023

  1. Configuration menu
    Copy the full SHA
    f82dc02 View commit details
    Browse the repository at this point in the history
  2. Format, etc.

    charliermarsh committed Jul 29, 2023
    Configuration menu
    Copy the full SHA
    b8943f9 View commit details
    Browse the repository at this point in the history