Skip to content

Type parameters: Incorrect text in SyntaxError for disallowed expression #119933

Closed
@JelleZijlstra

Description

@JelleZijlstra

Bug report

Bug description:

We disallow certain expressions (e.g., yield) in type parameters bounds, constraints, and defaults. But the error message always says it's a bound:

>>> def f[T=(yield)](): pass
  File "<python-input-0>", line 1
SyntaxError: yield expression cannot be used within a TypeVar bound
>>> def f[T: (int, (yield))](): pass
  File "<python-input-2>", line 1
SyntaxError: yield expression cannot be used within a TypeVar bound

We could either add some machinery in the symbol table so it knows whether we're in a bound, constraints, or default, or just change the error message to something like "within a TypeVar bound, constraints, or default".

CPython versions tested on:

CPython main branch

Operating systems tested on:

No response

Linked PRs

Metadata

Metadata

Assignees

Labels

3.12only security fixes3.13bugs and security fixes3.14bugs and security fixesinterpreter-core(Objects, Python, Grammar, and Parser dirs)topic-typingtype-bugAn unexpected behavior, bug, or error

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions