Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: astral-sh/ruff
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v0.3.1
Choose a base ref
...
head repository: astral-sh/ruff
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: v0.3.2
Choose a head ref
  • 14 commits
  • 188 files changed
  • 6 contributors

Commits on Mar 7, 2024

  1. Avoid repeating function calls in f-string conversions (#10265)

    ## Summary
    
    Given a format string like `"{x} {x}".format(x=foo())`, we should avoid
    converting to an f-string, since doing so would require repeating the
    function call (`f"{foo()} {foo()}"`), which could introduce side
    effects.
    
    Closes #10258.
    charliermarsh authored Mar 7, 2024
    Configuration menu
    Copy the full SHA
    461cdad View commit details
    Browse the repository at this point in the history
  2. [pyupgrade] Allow fixes for f-string rule regardless of line length…

    … (`UP032`) (#10263)
    
    ## Summary
    
    This is a follow-up to #10238 to
    offer fixes for the f-string rule regardless of the line length of the
    resulting fix. To quote Alex in the linked PR:
    
    > Yes, from the user's perspective I'd rather have a fix that may lead
    to line length issues than have to fix them myself :-) Cleaning up line
    lengths is easier than changing from `"".format()` to `f""`
    
    I agree with this position, which is that if we're going to offer a
    diagnostic, we should really be offering the user the ability to fix it
    -- otherwise, we're just inconveniencing them.
    charliermarsh authored Mar 7, 2024
    Configuration menu
    Copy the full SHA
    91af5a4 View commit details
    Browse the repository at this point in the history
  3. Fix E203 false positive for slices in format strings (#10280)

    ## Summary
    
    The code later in this file that checks for slices relies on the stack
    of brackets to determine the position. I'm not sure why format strings
    were being excluded from this, but the tests still pass with these match
    guards removed.
    
    Closes #10278
    
    ## Test Plan
    
    ~Still needs a test.~ Test case added for this example.
    sciyoshi authored Mar 7, 2024
    Configuration menu
    Copy the full SHA
    7b4a73d View commit details
    Browse the repository at this point in the history

Commits on Mar 8, 2024

  1. Remove Maturin pin (#10284)

    ## Summary
    
    As of
    https://github.com/pypa/gh-action-pypi-publish/releases/tag/v1.8.13, all
    relevant dependencies have been updated to support Metadata 2.2, so we
    can remove our Maturin pin.
    charliermarsh authored Mar 8, 2024
    Configuration menu
    Copy the full SHA
    7a675cd View commit details
    Browse the repository at this point in the history
  2. Treat typing.Annotated subscripts as type definitions (#10285)

    ## Summary
    
    I think this code has existed since the start of `typing.Annotated`
    support (#333), and was then
    overlooked over a series of refactors.
    
    Closes #10279.
    charliermarsh authored Mar 8, 2024
    Configuration menu
    Copy the full SHA
    57be3fc View commit details
    Browse the repository at this point in the history
  3. Include actual conditions in E712 diagnostics (#10254)

    ## Summary
    
    Changes the generic recommendation to replace
    
    ```python
    if foo == True: ...
    ```
    
    with `if cond:` to `if foo:`.
    
    Still uses a generic message for compound comparisons as a specific
    message starts to become confusing. For example,
    
    ```python
    if foo == True != False: ...
    ```
    
    produces two recommendations, one of which would recommend `if True:`,
    which is confusing.
    
    Resolves [recommendation in a previous
    PR](https://github.com/astral-sh/ruff/pull/8613/files#r1514915070).
    
    ## Test Plan
    
    `cargo nextest run`
    tjkuson authored Mar 8, 2024
    Configuration menu
    Copy the full SHA
    72c9f7e View commit details
    Browse the repository at this point in the history
  4. Configuration menu
    Copy the full SHA
    c504d7a View commit details
    Browse the repository at this point in the history
  5. Fix trailing kwargs end of line comment after slash (#10297)

    ## Summary
    
    Fixes the handling end of line comments that belong to `**kwargs` when
    the `**kwargs` come after a slash.
    
    The issue was that we missed to include the `**kwargs` start position
    when determining the start of the next node coming after the `/`.
    
    Fixes #10281
    
    ## Test Plan
    
    Added test
    MichaReiser authored Mar 8, 2024
    Configuration menu
    Copy the full SHA
    965adbe View commit details
    Browse the repository at this point in the history
  6. Start tracking quoting style in the AST (#10298)

    This PR modifies our AST so that nodes for string literals, bytes literals and f-strings all retain the following information:
    - The quoting style used (double or single quotes)
    - Whether the string is triple-quoted or not
    - Whether the string is raw or not
    
    This PR is a followup to #10256. Like with that PR, this PR does not, in itself, fix any bugs. However, it means that we will have the necessary information to preserve quoting style and rawness of strings in the `ExprGenerator` in a followup PR, which will allow us to provide a fix for #7799.
    
    The information is recorded on the AST nodes using a bitflag field on each node, similarly to how we recorded the information on `Tok::String`, `Tok::FStringStart` and `Tok::FStringMiddle` tokens in #10298. Rather than reusing the bitflag I used for the tokens, however, I decided to create a custom bitflag for each AST node.
    
    Using different bitflags for each node allows us to make invalid states unrepresentable: it is valid to set a `u` prefix on a string literal, but not on a bytes literal or an f-string. It also allows us to have better debug representations for each AST node modified in this PR.
    AlexWaygood authored Mar 8, 2024
    Configuration menu
    Copy the full SHA
    1d97f27 View commit details
    Browse the repository at this point in the history
  7. Refactor with statement formatting to have explicit layouts (#10296)

    ## Summary
    
    This PR refactors the with item formatting to use more explicit layouts
    to make it easier to understand the different formatting cases.
    
    The benefit of the explicit layout is that it makes it easier to reasons
    about layout transition between format runs. For example, today it's
    possible that `SingleWithTarget` or `ParenthesizeIfExpands` add
    parentheses around the with items for `with aaaaaaaaaa + bbbbbbbbbbbb:
    pass`, resulting in `with (aaaaaaaaaa + bbbbbbbbbbbb): pass`. The
    problem with this is that the next formatting pass uses the
    `SingleParenthesizedContextExpression` layout that uses
    `maybe_parenthesize_expression` which is different from
    `parenthesize_if_expands(&expr)` or `optional_parentheses(&expr)`.
    
    ## Test Plan
    
    `cargo test`
    
    I ran the ecosystem checks locally and there are no changes.
    MichaReiser authored Mar 8, 2024
    Configuration menu
    Copy the full SHA
    a56d42f View commit details
    Browse the repository at this point in the history
  8. Fix unstable with-items formatting (#10274)

    ## Summary
    
    Fixes #10267
    
    The issue with the current formatting is that the formatter flips
    between the `SingleParenthesizedContextManager` and
    `ParenthesizeIfExpands` or `SingleWithTarget` because the layouts use
    incompatible formatting ( `SingleParenthesizedContextManager`:
    `maybe_parenthesize_expression(context)` vs `ParenthesizeIfExpands`:
    `parenthesize_if_expands(item)`, `SingleWithTarget`:
    `optional_parentheses(item)`.
    
    The fix is to ensure that the layouts between which the formatter flips
    when adding or removing parentheses are the same. I do this by
    introducing a new `SingleWithoutTarget` layout that uses the same
    formatting as `SingleParenthesizedContextManager` if it has no target
    and prefer `SingleWithoutTarget` over using `ParenthesizeIfExpands` or
    `SingleWithTarget`.
    
    ## Formatting change
    
    The downside is that we now use `maybe_parenthesize_expression` over
    `parenthesize_if_expands` for expressions where
    `can_omit_optional_parentheses` returns `false`. This can lead to stable
    formatting changes. I only found one formatting change in our ecosystem
    check and, unfortunately, this is necessary to fix the instability (and
    instability fixes are okay to have as part of minor changes according to
    our versioning policy)
    
    The benefit of the change is that `with` items with a single context
    manager and without a target are now formatted identically to how the
    same expression would be formatted in other clause headers.
    
    ## Test Plan
    
    I ran the ecosystem check locally
    MichaReiser authored Mar 8, 2024
    Configuration menu
    Copy the full SHA
    4bce801 View commit details
    Browse the repository at this point in the history
  9. Formatter: Improve single-with item formatting for Python 3.8 or older (

    #10276)
    
    ## Summary
    
    This PR changes how we format `with` statements with a single with item
    for Python 3.8 or older. This change is not compatible with Black.
    
    This is how we format a single-item with statement today 
    
    ```python
    def run(data_path, model_uri):
        with pyspark.sql.SparkSession.builder.config(
            key="spark.python.worker.reuse", value=True
        ).config(key="spark.ui.enabled", value=False).master(
            "local-cluster[2, 1, 1024]"
        ).getOrCreate():
            # ignore spark log output
            spark.sparkContext.setLogLevel("OFF")
            print(score_model(spark, data_path, model_uri))
    ```
    
    This is different than how we would format the same expression if it is
    inside any other clause header (`while`, `if`, ...):
    
    ```python
    def run(data_path, model_uri):
        while (
            pyspark.sql.SparkSession.builder.config(
                key="spark.python.worker.reuse", value=True
            )
            .config(key="spark.ui.enabled", value=False)
            .master("local-cluster[2, 1, 1024]")
            .getOrCreate()
        ):
            # ignore spark log output
            spark.sparkContext.setLogLevel("OFF")
            print(score_model(spark, data_path, model_uri))
    
    ```
    
    Which seems inconsistent to me. 
    
    This PR changes the formatting of the single-item with Python 3.8 or
    older to match that of other clause headers.
    
    ```python
    def run(data_path, model_uri):
        with (
            pyspark.sql.SparkSession.builder.config(
                key="spark.python.worker.reuse", value=True
            )
            .config(key="spark.ui.enabled", value=False)
            .master("local-cluster[2, 1, 1024]")
            .getOrCreate()
        ):
            # ignore spark log output
            spark.sparkContext.setLogLevel("OFF")
            print(score_model(spark, data_path, model_uri))
    ```
    
    According to our versioning policy, this style change is gated behind a
    preview flag.
    
    ## Test Plan
    
    See added tests.
    
    Added
    MichaReiser authored Mar 8, 2024
    Configuration menu
    Copy the full SHA
    b64f2ea View commit details
    Browse the repository at this point in the history
  10. Fix incorrect Parameter range for *args and **kwargs (#10283)

    ## Summary
    
    Fix #10282 
    
    This PR updates the Python grammar to include the `*` character in
    `*args` `**kwargs` in the range of the `Parameter`
    ```
    def f(*args, **kwargs): pass
    #      ~~~~    ~~~~~~    <-- range before the PR
    #     ^^^^^  ^^^^^^^^    <-- range after
    ```
    
    The invalid syntax `def f(*, **kwargs): ...` is also now correctly
    reported.
    
    ## Test Plan
    
    Test cases were added to `function.rs`.
    GtrMo authored Mar 8, 2024
    Configuration menu
    Copy the full SHA
    a067d87 View commit details
    Browse the repository at this point in the history

Commits on Mar 9, 2024

  1. Configuration menu
    Copy the full SHA
    a892fc7 View commit details
    Browse the repository at this point in the history
Loading