pub trait SemanticSyntaxContext {
Show 16 methods
// Required methods
fn future_annotations_or_stub(&self) -> bool;
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 # herebut not for this case:
def f():
class C:
x # hereIn 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 scopesRequired Methods§
Sourcefn future_annotations_or_stub(&self) -> bool
fn future_annotations_or_stub(&self) -> bool
Returns true if __future__-style type annotations are enabled.
Sourcefn python_version(&self) -> PythonVersion
fn python_version(&self) -> PythonVersion
The target Python version for detecting backwards-incompatible syntax changes.
Sourcefn global(&self, name: &str) -> Option<TextRange>
fn global(&self, name: &str) -> Option<TextRange>
Return the TextRange at which a name is declared as global in the current scope.
Sourcefn has_nonlocal_binding(&self, name: &str) -> bool
fn has_nonlocal_binding(&self, name: &str) -> bool
Returns true if name has a binding in an enclosing scope.
Sourcefn in_async_context(&self) -> bool
fn in_async_context(&self) -> bool
Returns true if the visitor is currently in an async context, i.e. an async function.
Sourcefn in_await_allowed_context(&self) -> bool
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:
awaitis allowed in a lambda, despite it not being asyncawaitis 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 # errorSee the trait-level documentation for more details.
Sourcefn in_yield_allowed_context(&self) -> bool
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:
- Function definitions
- 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 functionSourcefn in_sync_comprehension(&self) -> bool
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.
Sourcefn in_module_scope(&self) -> bool
fn in_module_scope(&self) -> bool
Returns true if the visitor is at the top-level module scope.
Sourcefn in_function_scope(&self) -> bool
fn in_function_scope(&self) -> bool
Returns true if the visitor is in a function scope.
Sourcefn in_generator_context(&self) -> bool
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.
Sourcefn in_notebook(&self) -> bool
fn in_notebook(&self) -> bool
Returns true if the source file is a Jupyter notebook.
fn report_semantic_error(&self, error: SemanticSyntaxError)
Sourcefn in_loop_context(&self) -> bool
fn in_loop_context(&self) -> bool
Returns true if the visitor is inside a for or while loop.
Sourcefn is_bound_parameter(&self, name: &str) -> bool
fn is_bound_parameter(&self, name: &str) -> bool
Returns true if name is a bound parameter in the current function or lambda scope.