Skip to content

Ruff fails to reject invalid syntax in annotation scopes #11118

Closed
@JelleZijlstra

Description

@JelleZijlstra

Ruff produces no errors for these three lines:

type X[T: (yield 1)] = int
type Y = (yield 1)
def _f[T](x: (yield 1)) -> (y := 3): return x

But they are syntax errors in Python:

>>> type X[T: (yield 1)] = int
  File "<stdin>-0", line 1
SyntaxError: yield expression cannot be used within a TypeVar bound
>>> type Y = (yield 1)
  File "<stdin>-1", line 1
SyntaxError: yield expression cannot be used within a type alias
>>> def _f[T](x: (yield 1)) -> (y := 3): return x
... 
  File "<stdin>-2", line 1
SyntaxError: yield expression cannot be used within the definition of a generic

Producing errors for these cases feels low priority, because I don't know why anyone would do this other than to be difficult; I noticed it while looking at the parser. Still, I'd expect Ruff to tell me about all SyntaxErrors in my Python code.

Ruff has a few analogous rules for constructs that produce syntax errors at runtime:

Metadata

Metadata

Assignees

No one assigned

    Labels

    linterRelated to the linterruleImplementing or modifying a lint rule

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions