[syntax-errors] Invalid syntax in annotations (#17101)

Summary
--

This PR detects the use of invalid syntax in annotation scopes,
including
`yield` and `yield from` expressions and named expressions. I combined a
few
different types of CPython errors here, but I think the resulting error
messages
still make sense and are even preferable to what CPython gives. For
example, we
report `yield expression cannot be used in a type annotation` for both
of these:

```pycon
>>> def f[T](x: (yield 1)): ...
  File "<python-input-26>", line 1
    def f[T](x: (yield 1)): ...
                 ^^^^^^^
SyntaxError: yield expression cannot be used within the definition of a generic
>>> def foo() -> (yield x): ...
  File "<python-input-28>", line 1
    def foo() -> (yield x): ...
                  ^^^^^^^
SyntaxError: 'yield' outside function
```

Fixes https://github.com/astral-sh/ruff/issues/11118.

Test Plan
--

New inline tests, along with some updates to existing tests.
This commit is contained in:
Brent Westbrook 2025-04-03 17:56:55 -04:00 committed by GitHub
parent 24b1b1d52c
commit c2b2e42ad3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
25 changed files with 2866 additions and 141 deletions

View file

@ -0,0 +1,7 @@
class F[T](y := list): ...
class G((yield 1)): ...
class H((yield from 1)): ...
class I[T]((yield 1)): ...
class J[T]((yield from 1)): ...
class K[T: (yield 1)]: ... # yield in TypeVar
class L[T: (x := 1)]: ... # named expr in TypeVar

View file

@ -0,0 +1,18 @@
def f[T]() -> (y := 3): ...
def g[T](arg: (x := 1)): ...
def h[T](x: (yield 1)): ...
def i(x: (yield 1)): ...
def j[T]() -> (yield 1): ...
def k() -> (yield 1): ...
def l[T](x: (yield from 1)): ...
def m(x: (yield from 1)): ...
def n[T]() -> (yield from 1): ...
def o() -> (yield from 1): ...
def p[T: (yield 1)](): ... # yield in TypeVar bound
def q[T = (yield 1)](): ... # yield in TypeVar default
def r[*Ts = (yield 1)](): ... # yield in TypeVarTuple default
def s[**Ts = (yield 1)](): ... # yield in ParamSpec default
def t[T: (x := 1)](): ... # named expr in TypeVar bound
def u[T = (x := 1)](): ... # named expr in TypeVar default
def v[*Ts = (x := 1)](): ... # named expr in TypeVarTuple default
def w[**Ts = (x := 1)](): ... # named expr in ParamSpec default

View file

@ -0,0 +1,6 @@
type X[T: (yield 1)] = int # TypeVar bound
type X[T = (yield 1)] = int # TypeVar default
type X[*Ts = (yield 1)] = int # TypeVarTuple default
type X[**Ts = (yield 1)] = int # ParamSpec default
type Y = (yield 1) # yield in value
type Y = (x := 1) # named expr in value

View file

@ -1,4 +1,3 @@
def foo() -> int | str: ...
def foo() -> lambda x: x: ...
def foo() -> (yield x): ...
def foo() -> int if True else str: ...

View file

@ -1,4 +1,3 @@
def foo(arg: int): ...
def foo(arg: lambda x: x): ...
def foo(arg: (yield x)): ...
def foo(arg: (x := int)): ...

View file

@ -0,0 +1 @@
class F(y := list): ...

View file

@ -0,0 +1,2 @@
def f() -> (y := 3): ...
def g(arg: (x := 1)): ...