Skip to main content

SemanticSyntaxContext

Trait SemanticSyntaxContext 

Source
pub trait SemanticSyntaxContext {
Show 17 methods // Required methods fn future_annotations_or_stub(&self) -> bool; fn lazy_import_context(&self) -> Option<LazyImportContext>; fn python_version(&self) -> PythonVersion; fn source(&self) -> &str; fn global(&self, name: &str) -> Option<TextRange>; fn has_nonlocal_binding(&self, name: &str) -> bool; fn in_async_context(&self) -> bool; fn in_await_allowed_context(&self) -> bool; fn in_yield_allowed_context(&self) -> bool; fn in_sync_comprehension(&self) -> bool; fn in_module_scope(&self) -> bool; fn in_function_scope(&self) -> bool; fn in_generator_context(&self) -> bool; fn in_notebook(&self) -> bool; fn report_semantic_error(&self, error: SemanticSyntaxError); fn in_loop_context(&self) -> bool; fn is_bound_parameter(&self, name: &str) -> bool;
}
Expand description

Information needed from a parent visitor to emit semantic syntax errors.

Note that the in_*_scope methods should refer to the immediately-enclosing scope. For example, in_function_scope should return true for this case:

def f():
    x  # here

but not for this case:

def f():
    class C:
        x  # here

In contrast, the in_*_context methods should traverse parent scopes. For example, in_function_context should return true for this case:

def f():
    [x  # here
        for x in range(3)]

but not here:

def f():
    class C:
        x  # here, classes break function scopes

Required Methods§

Source

fn future_annotations_or_stub(&self) -> bool

Returns true if __future__-style type annotations are enabled.

Source

fn lazy_import_context(&self) -> Option<LazyImportContext>

Returns the nearest invalid context for a lazy import statement, if any.

This should return the innermost relevant restriction in order of precedence: function, class, then try/except.

Source

fn python_version(&self) -> PythonVersion

The target Python version for detecting backwards-incompatible syntax changes.

Source

fn source(&self) -> &str

Returns the source text under analysis.

Source

fn global(&self, name: &str) -> Option<TextRange>

Return the TextRange at which a name is declared as global in the current scope.

Source

fn has_nonlocal_binding(&self, name: &str) -> bool

Returns true if name has a binding in an enclosing scope.

Source

fn in_async_context(&self) -> bool

Returns true if the visitor is currently in an async context, i.e. an async function.

Source

fn in_await_allowed_context(&self) -> bool

Returns true if the visitor is currently in a context where the await keyword is allowed.

Note that this is method is primarily used to report YieldOutsideFunction errors for await outside function scopes, irrespective of their async status. As such, this differs from in_async_context in two ways:

  1. await is allowed in a lambda, despite it not being async
  2. await is allowed in any function, regardless of its async status

In short, only nested class definitions should cause this method to return false, for example:

def f():
    await 1  # okay, in a function
    class C:
        await 1  # error

See the trait-level documentation for more details.

Source

fn in_yield_allowed_context(&self) -> bool

Returns true if the visitor is currently in a context where yield and yield from expressions are allowed.

Yield expressions are allowed only in:

  1. Function definitions
  2. Lambda expressions

Unlike await, yield is not allowed in:

  • Comprehensions (list, set, dict)
  • Generator expressions
  • Class definitions

This method should traverse parent scopes to check if the closest relevant scope is a function or lambda, and that no disallowed context (class, comprehension, generator) intervenes. For example:

def f():
    yield 1  # okay, in a function
    lambda: (yield 1)  # okay, in a lambda

    [(yield 1) for x in range(3)]  # error, in a comprehension
    ((yield 1) for x in range(3))  # error, in a generator expression
    class C:
        yield 1  # error, in a class within a function
Source

fn in_sync_comprehension(&self) -> bool

Returns true if the visitor is currently inside of a synchronous comprehension.

This method is necessary because in_async_context only checks for the nearest, enclosing function to determine the (a)sync context. Instead, this method will search all enclosing scopes until it finds a sync comprehension. As a result, the two methods will typically be used together.

Source

fn in_module_scope(&self) -> bool

Returns true if the visitor is at the top-level module scope.

Source

fn in_function_scope(&self) -> bool

Returns true if the visitor is in a function scope.

Source

fn in_generator_context(&self) -> bool

Returns true if the visitor is within a generator scope.

Note that this refers to an Expr::Generator precisely, not to comprehensions more generally.

Source

fn in_notebook(&self) -> bool

Returns true if the source file is a Jupyter notebook.

Source

fn report_semantic_error(&self, error: SemanticSyntaxError)

Source

fn in_loop_context(&self) -> bool

Returns true if the visitor is inside a for or while loop.

Source

fn is_bound_parameter(&self, name: &str) -> bool

Returns true if name is a bound parameter in the current function or lambda scope.

Implementors§