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: 0.11.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: 0.11.2
Choose a head ref
  • 9 commits
  • 32 files changed
  • 7 contributors

Commits on Mar 20, 2025

  1. [red-knot] add test cases result in false positive errors (#16856)

    ## Summary
    
    From #16641
    
    The previous PR attempted to fix the errors presented in this PR, but as
    discussed in the conversation, it was concluded that the approach was
    undesirable and that further work would be needed to fix the errors with
    a correct general solution.
    
    In this PR, I instead add the test cases from the previous PR as TODOs,
    as a starting point for future work.
    
    ## Test Plan
    
    ---------
    
    Co-authored-by: Carl Meyer <carl@oddbird.net>
    mtshiba and carljm authored Mar 20, 2025
    Copy the full SHA
    23382f5 View commit details
  2. Recognize SyntaxError: as an error code for ecosystem checks (#16879)

    Summary
    --
    
    This updates the regex in `ruff-ecosystem` to catch syntax errors in an
    effort to prevent bugs like #16874. This should catch `ParseError`s,
    `UnsupportedSyntaxError`s, and the upcoming `SemanticSyntaxError`s.
    
    Test Plan
    --
    
    I ran the ecosystem check locally comparing v0.11.0 and v0.11.1 and saw
    a large number (2757!) of new syntax errors. I also manually tested the
    regex on a few lines before that.
    
    If we merge this before #16878, I'd expect to see that number decrease
    substantially in that PR too, as another test.
    ntBre authored Mar 20, 2025
    Copy the full SHA
    6760251 View commit details
  3. [syntax-errors] Fix star annotation before Python 3.11 (#16878)

    Summary
    --
    
    Fixes #16874. I previously emitted a syntax error when starred
    annotations were _allowed_ rather than when they were actually used.
    This caused false positives for any starred parameter name because these
    are allowed to have starred annotations but not required to. The fix is
    to check if the annotation is actually starred after parsing it.
    
    Test Plan
    --
    
    New inline parser tests derived from the initial report and more
    examples from the comments, although I think the first case should cover
    them all.
    ntBre authored Mar 20, 2025
    Copy the full SHA
    42cbce5 View commit details
  4. Special-case value-expression inference of special form subscriptions (

    …#16877)
    
    ## Summary
    
    Currently for something like `X = typing.Tuple[str, str]`, we infer the
    value of `X` as `object`. That's because `Tuple` (like many of the
    symbols in the typing module) is annotated as a `_SpecialForm` instance
    in typeshed's stubs:
    
    
    https://github.com/astral-sh/ruff/blob/23382f5f8c7b4e356368cdeb1049b8c1910baff3/crates/red_knot_vendored/vendor/typeshed/stdlib/typing.pyi#L215
    
    and we don't understand implicit type aliases yet, and the stub for
    `_SpecialForm.__getitem__` says it always returns `object`:
    
    
    https://github.com/astral-sh/ruff/blob/23382f5f8c7b4e356368cdeb1049b8c1910baff3/crates/red_knot_vendored/vendor/typeshed/stdlib/typing.pyi#L198-L200
    
    We have existing false positives in our test suite due to this:
    
    
    https://github.com/astral-sh/ruff/blob/23382f5f8c7b4e356368cdeb1049b8c1910baff3/crates/red_knot_python_semantic/resources/mdtest/annotations/annotated.md?plain=1#L76-L78
    
    and it's causing _many_ new false positives in #16872, which tries to
    make our annotation-expression parsing stricter in some ways.
    
    This PR therefore adds some small special casing for `KnownInstanceType`
    variants that fallback to `_SpecialForm`, so that these false positives
    can be avoided.
    
    ## Test Plan
    
    Existing mdtest altered.
    
    Cc. @MatthewMckee4
    AlexWaygood authored Mar 20, 2025
    Copy the full SHA
    296d67a View commit details
  5. [red-knot] Ban most Type::Instance types in type expressions (#16872)

    ## Summary
    
    Catch some Instances, but raise type error for the rest of them
    Fixes #16851 
    
    ## Test Plan
    
    Extend invalid.md in annotations
    
    ---------
    
    Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
    MatthewMckee4 and AlexWaygood authored Mar 20, 2025
    Copy the full SHA
    63e78b4 View commit details

Commits on Mar 21, 2025

  1. [red-knot] Check whether two callable types are equivalent (#16698)

    ## Summary
    
    This PR checks whether two callable types are equivalent or not.
    
    This is required because for an equivalence relationship, the default
    value does not necessarily need to be the same but if the parameter in
    one of the callable has a default value then the corresponding parameter
    in the other callable should also have a default value. This is the main
    reason a manual implementation is required.
    
    And, as per https://typing.python.org/en/latest/spec/callables.html#id4,
    the default _type_ doesn't participate in a subtype relationship, only
    the optionality (required or not) participates. This means that the
    following two callable types are equivalent:
    
    ```py
    def f1(a: int = 1) -> None: ...
    def f2(a: int = 2) -> None: ...
    ```
    
    Additionally, the name of positional-only, variadic and keyword-variadic
    are not required to be the same for an equivalence relation.
    
    A potential solution to avoid the manual implementation would be to only
    store whether a parameter has a default value or not but the type is
    currently required to check for assignability.
    
    ## Test plan
    
    Add tests for callable types in `is_equivalent_to.md`
    dhruvmanila authored Mar 21, 2025
    Copy the full SHA
    193c381 View commit details
  2. [red-knot] Check subtype relation between callable types (#16804)

    ## Summary
    
    Part of #15382
    
    This PR adds support for checking the subtype relationship between the
    two callable types.
    
    The main source of reference used for implementation is
    https://typing.python.org/en/latest/spec/callables.html#assignability-rules-for-callables.
    
    The implementation is split into two phases:
    1. Check all the positional parameters which includes positional-only,
    standard (positional or keyword) and variadic kind
    2. Collect all the keywords in a `HashMap` to do the keyword parameters
    check via name lookup
    
    For (1), there's a helper struct which is similar to `.zip_longest`
    (from `itertools`) except that it allows control over one of the
    iterator as that's required when processing a variadic parameter. This
    is required because positional parameters needs to be checked as per
    their position between the two callable types. The struct also keeps
    track of the current iteration element because when the loop is exited
    (to move on to the phase 2) the current iteration element would be
    carried over to the phase 2 check.
    
    This struct is internal to the `is_subtype_of` method as I don't think
    it makes sense to expose it outside. It also allows me to use "self" and
    "other" suffixed field names as that's only relevant in that context.
    
    ## Test Plan
    
    Add extensive tests in markdown.
    
    Converted all of the code snippets from
    https://typing.python.org/en/latest/spec/callables.html#assignability-rules-for-callables
    to use `knot_extensions.is_subtype_of` and verified the result.
    dhruvmanila authored Mar 21, 2025
    Copy the full SHA
    04a8756 View commit details
  3. Use the common OperatorPrecedence for the parser (#16747)

    ## Summary
    
    This change continues to resolve #16071 (and continues the work started
    in #16162). Specifically, this PR changes the code in the parser so that
    it uses the `OperatorPrecedence` struct from `ruff_python_ast` instead
    of its own version. This is part of an effort to get rid of the
    redundant definitions of `OperatorPrecedence` throughout the codebase.
    
    Note that this PR only makes this change for `ruff_python_parser` -- we
    still want to make a similar change for the formatter (namely the
    `OperatorPrecedence` defined in the expression part of the formatter,
    the pattern one is different). I separated the work to keep the PRs
    small and easily reviewable.
    
    ## Test Plan
    
    Because this is an internal change, I didn't add any additional tests.
    Existing tests do pass.
    junhsonjb authored Mar 21, 2025
    Copy the full SHA
    2a4d835 View commit details
  4. Bump 0.11.2 (#16896)

    Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
    ntBre and AlexWaygood authored Mar 21, 2025
    Copy the full SHA
    4773878 View commit details
Loading