Skip to main content

UnsupportedSyntaxErrorKind

Enum UnsupportedSyntaxErrorKind 

Source
pub enum UnsupportedSyntaxErrorKind {
Show 20 variants Match, Walrus, ExceptStar, UnparenthesizedNamedExpr(UnparenthesizedNamedExprKind), ParenthesizedKeywordArgumentName, StarTuple(StarTupleKind), RelaxedDecorator(RelaxedDecoratorError), PositionalOnlyParameter, TypeParameterList, LazyImportStatement, TypeAliasStatement, TypeParamDefault, Pep701FString(FStringKind), ParenthesizedContextManager, StarExpressionInIndex, StarAnnotation, IterableUnpackingInListComprehension, UnparenthesizedUnpackInFor, UnparenthesizedExceptionTypes, TemplateStrings,
}

Variants§

§

Match

§

Walrus

§

ExceptStar

§

UnparenthesizedNamedExpr(UnparenthesizedNamedExprKind)

Represents the use of an unparenthesized named expression (:=) in a set literal, set comprehension, or sequence index before Python 3.10.

§Examples

These are allowed on Python 3.10:

{x := 1, 2, 3}                 # set literal
{last := x for x in range(3)}  # set comprehension
lst[x := 1]                    # sequence index

But on Python 3.9 the named expression needs to be parenthesized:

{(x := 1), 2, 3}                 # set literal
{(last := x) for x in range(3)}  # set comprehension
lst[(x := 1)]                    # sequence index

However, unparenthesized named expressions are never allowed in slices:

lst[x:=1:-1]      # syntax error
lst[1:x:=1]       # syntax error
lst[1:3:x:=1]     # syntax error

lst[(x:=1):-1]    # ok
lst[1:(x:=1)]     # ok
lst[1:3:(x:=1)]   # ok
§References
§

ParenthesizedKeywordArgumentName

Represents the use of a parenthesized keyword argument name after Python 3.8.

§Example

From BPO 34641 it sounds like this was only accidentally supported and was removed when noticed. Code like this used to be valid:

f((a)=1)

After Python 3.8, you have to omit the parentheses around a:

f(a=1)
§

StarTuple(StarTupleKind)

Represents the use of unparenthesized tuple unpacking in a return statement or yield expression before Python 3.8.

§Examples

Before Python 3.8, this syntax was allowed:

rest = (4, 5, 6)

def f():
    t = 1, 2, 3, *rest
    return t

def g():
    t = 1, 2, 3, *rest
    yield t

But this was not:

rest = (4, 5, 6)

def f():
    return 1, 2, 3, *rest

def g():
    yield 1, 2, 3, *rest

Instead, parentheses were required in the return and yield cases:

rest = (4, 5, 6)

def f():
    return (1, 2, 3, *rest)

def g():
    yield (1, 2, 3, *rest)

This was reported in BPO 32117 and updated in Python 3.8 to allow the unparenthesized form.

§

RelaxedDecorator(RelaxedDecoratorError)

Represents the use of a “relaxed” PEP 614 decorator before Python 3.9.

§Examples

Prior to Python 3.9, decorators were defined to be dotted_names, optionally followed by an argument list. For example:

@buttons.clicked.connect
def foo(): ...

@buttons.clicked.connect(1, 2, 3)
def foo(): ...

As pointed out in the PEP, this prevented reasonable extensions like subscripts:

buttons = [QPushButton(f'Button {i}') for i in range(10)]

@buttons[0].clicked.connect
def spam(): ...

Python 3.9 removed these restrictions and expanded the decorator grammar to include any assignment expression and include cases like the example above.

§

PositionalOnlyParameter

Represents the use of a PEP 570 positional-only parameter before Python 3.8.

§Examples

Python 3.8 added the / syntax for marking preceding parameters as positional-only:

def foo(a, b, /, c): ...

This means a and b in this case can only be provided by position, not by name. In other words, this code results in a TypeError at runtime:

>>> def foo(a, b, /, c): ...
...
>>> foo(a=1, b=2, c=3)
Traceback (most recent call last):
  File "<python-input-3>", line 1, in <module>
    foo(a=1, b=2, c=3)
    ~~~^^^^^^^^^^^^^^^
TypeError: foo() got some positional-only arguments passed as keyword arguments: 'a, b'
§

TypeParameterList

Represents the use of a type parameter list before Python 3.12.

§Examples

Before Python 3.12, generic parameters had to be declared separately using a class like typing.TypeVar, which could then be used in a function or class definition:

from typing import Generic, TypeVar

T = TypeVar("T")

def f(t: T): ...
class C(Generic[T]): ...

PEP 695, included in Python 3.12, introduced the new type parameter syntax, which allows these to be written more compactly and without a separate type variable:

def f[T](t: T): ...
class C[T]: ...
§

LazyImportStatement

§

TypeAliasStatement

§

TypeParamDefault

§

Pep701FString(FStringKind)

Represents the use of a PEP 701 f-string before Python 3.12.

§Examples

As described in the PEP, each of these cases were invalid before Python 3.12:

# nested quotes
f'Magic wand: { bag['wand'] }'

# escape characters
f"{'\n'.join(a)}"

# comments
f'''A complex trick: {
    bag['bag']  # recursive bags!
}'''

# arbitrary nesting
f"{f"{f"{f"{f"{f"{1+1}"}"}"}"}"}"

These restrictions were lifted in Python 3.12, meaning that all of these examples are now valid.

§

ParenthesizedContextManager

Represents the use of a parenthesized with item before Python 3.9.

§Examples

As described in BPO 12782, with uses like this were not allowed on Python 3.8:

with (open("a_really_long_foo") as foo,
      open("a_really_long_bar") as bar):
    pass

because parentheses were not allowed within the with statement itself (see this comment in particular). However, parenthesized expressions were still allowed, including the cases below, so the issue can be pretty subtle and relates specifically to parenthesized items with as bindings.

with (foo, bar): ...  # okay
with (
  open('foo.txt')) as foo: ...  # also okay
with (
  foo,
  bar,
  baz,
): ...  # also okay, just a tuple
with (
  foo,
  bar,
  baz,
) as tup: ...  # also okay, binding the tuple

This restriction was lifted in 3.9 but formally included in the release notes for 3.10.

§

StarExpressionInIndex

Represents the use of a PEP 646 star expression in an index.

§Examples

Before Python 3.11, star expressions were not allowed in index/subscript operations (within square brackets). This restriction was lifted in PEP 646 to allow for star-unpacking of typing.TypeVarTuples, also added in Python 3.11. As such, this is the primary motivating example from the PEP:

from typing import TypeVar, TypeVarTuple

DType = TypeVar('DType')
Shape = TypeVarTuple('Shape')

class Array(Generic[DType, *Shape]): ...

But it applies to simple indexing as well:

vector[*x]
array[a, *b]
§

StarAnnotation

Represents the use of a PEP 646 star annotations in a function definition.

§Examples

Before Python 3.11, star annotations were not allowed in function definitions. This restriction was lifted in PEP 646 to allow type annotations for typing.TypeVarTuple, also added in Python 3.11:

from typing import TypeVarTuple

Ts = TypeVarTuple('Ts')

def foo(*args: *Ts): ...

Unlike UnsupportedSyntaxErrorKind::StarExpressionInIndex, this does not include any other annotation positions:

x: *Ts                # Syntax error
def foo(x: *Ts): ...  # Syntax error
§

IterableUnpackingInListComprehension

Represents the use of iterable unpacking inside a list comprehension before Python 3.15.

§Examples

Before Python 3.15, list comprehensions could not use iterable unpacking in their element expression:

[*x for x in y]  # SyntaxError

Starting with Python 3.15, PEP 798 allows iterable unpacking within list comprehensions:

[*x for x in y]
§

UnparenthesizedUnpackInFor

Represents the use of tuple unpacking in a for statement iterator clause before Python 3.9.

§Examples

Like UnsupportedSyntaxErrorKind::StarTuple in return and yield statements, prior to Python 3.9, tuple unpacking in the iterator clause of a for statement required parentheses:

# valid on Python 3.8 and earlier
for i in (*a, *b): ...

Omitting the parentheses was invalid:

for i in *a, *b: ...  # SyntaxError

This was changed as part of the PEG parser rewrite included in Python 3.9 but not documented directly until the Python 3.11 release.

§

UnparenthesizedExceptionTypes

Represents the use of multiple exception names in an except clause without an as binding, before Python 3.14.

§Examples

Before Python 3.14, catching multiple exceptions required parentheses like so:

try:
    ...
except (ExceptionA, ExceptionB, ExceptionC):
    ...

Starting with Python 3.14, thanks to PEP 758, it was permitted to omit the parentheses:

try:
    ...
except ExceptionA, ExceptionB, ExceptionC:
    ...

However, parentheses are still required in the presence of an as:

try:
    ...
except (ExceptionA, ExceptionB, ExceptionC) as e:
    ...
§

TemplateStrings

Represents the use of a template string (t-string) literal prior to the implementation of PEP 750 in Python 3.14.

Trait Implementations§

Source§

impl Clone for UnsupportedSyntaxErrorKind

Source§

fn clone(&self) -> UnsupportedSyntaxErrorKind

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for UnsupportedSyntaxErrorKind

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl GetSize for UnsupportedSyntaxErrorKind

Source§

fn get_heap_size(&self) -> usize

Determines how many bytes this object occupies inside the heap. Read more
Source§

fn get_heap_size_with_tracker<TRACKER: GetSizeTracker>( &self, tracker: TRACKER, ) -> (usize, TRACKER)

Determines how many bytes this object occupies inside the heap while using a tracker. Read more
Source§

fn get_stack_size() -> usize

Determines how may bytes this object occupies inside the stack. Read more
Source§

fn get_size(&self) -> usize

Determines the total size of the object. Read more
Source§

fn get_size_with_tracker<T>(&self, tracker: T) -> (usize, T)
where T: GetSizeTracker,

Determines the total size of the object while using a tracker. Read more
Source§

impl Hash for UnsupportedSyntaxErrorKind

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl PartialEq for UnsupportedSyntaxErrorKind

Source§

fn eq(&self, other: &UnsupportedSyntaxErrorKind) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Copy for UnsupportedSyntaxErrorKind

Source§

impl Eq for UnsupportedSyntaxErrorKind

Source§

impl StructuralPartialEq for UnsupportedSyntaxErrorKind

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.