check_shapes.error_contexts

Infrastructure for describing the context of an error.

The MessageBuilder is used to format / indent messages nicely.

The ErrorContext is a reusable bit of information about where/why an error occurred that can be written to a MessageBuilder.

ErrorContexts can be composed using the StackContext and ParallelContext.

This allows reusable error messages in a consistent format.

Module Contents

check_shapes.error_contexts._NO_VALUE

Sentinel to represent “no value” in places where None is a valid value.

class check_shapes.error_contexts.MessageBuilder(indent_str='')

Utility for formatting nested text.

Parameters:

indent_str (str) –

add_line(text)

Write a line, indented at the current level.

Input is converted to a str using str(text).

Parameters:

text (Any) –

Return type:

None

add_columned_line(*texts)

Write a line of several values, left-aligned within the current indentation level.

Inputs are converted to str`s using `str(text).

Parameters:

texts (Any) –

Return type:

None

indent()

Context manager for indenting text.

Return type:

Iterator[MessageBuilder]

build()

Compile all collected text into a single string.

Return type:

str

class check_shapes.error_contexts.ErrorContext

Bases: abc.ABC

A context in which an error can occur.

Contexts should be immutable, and implement __eq__() - so that they can be composed using StackContext and ParallelContext.

The contexts are often created even if an error doesn’t actually occur, so they should be cheap to create - prefer to do any slow computation in print(), rather than in __init__().

Maybe think of an ErrorContext as a factory of error messages.

abstract print(builder)

Print this context to the given MessageBuilder.

Parameters:

builder (MessageBuilder) –

Return type:

None

class check_shapes.error_contexts.StackContext

Bases: ErrorContext

Error context where one context is “inside” another one.

print(builder)

Print this context to the given MessageBuilder.

Parameters:

builder (MessageBuilder) –

Return type:

None

class check_shapes.error_contexts.ParallelContext

Bases: ErrorContext

Error context with many contexts in parallel.

print(builder)

Print this context to the given MessageBuilder.

Parameters:

builder (MessageBuilder) –

Return type:

None

class check_shapes.error_contexts.FunctionCallContext

Bases: ErrorContext

An error occured inside a function that was called.

Normally print should be called from within the called function: func. If that is impossible, first call precompute from within func.

print(builder)

Print this context to the given MessageBuilder.

Parameters:

builder (MessageBuilder) –

Return type:

None

precompute()

Precompute the values to print.

This is useful to capture the position of the stack of the relevant call, if this object is saved for later use.

Returns:

A new instance with precomptued values.

Return type:

FunctionCallContext

class check_shapes.error_contexts.FunctionDefinitionContext

Bases: ErrorContext

An error occured in the context of a function definition.

print(builder)

Print this context to the given MessageBuilder.

Parameters:

builder (MessageBuilder) –

Return type:

None

class check_shapes.error_contexts.VariableContext

Bases: ErrorContext

An error occurred in the context of a shape specification variable.

print(builder)

Print this context to the given MessageBuilder.

Parameters:

builder (MessageBuilder) –

Return type:

None

class check_shapes.error_contexts.TensorSpecContext

Bases: ErrorContext

An error occurred in the context of a tensor specification.

print(builder)

Print this context to the given MessageBuilder.

Parameters:

builder (MessageBuilder) –

Return type:

None

class check_shapes.error_contexts.ArgumentContext

Bases: ErrorContext

An error occurred in the context of an argument to a function.

print(builder)

Print this context to the given MessageBuilder.

Parameters:

builder (MessageBuilder) –

Return type:

None

__eq__(other)

Return self==value.

Parameters:

other (Any) –

Return type:

bool

__hash__()

Return hash(self).

Return type:

int

class check_shapes.error_contexts.AttributeContext

Bases: ErrorContext

An error occurred in the context of an attribute on an object.

print(builder)

Print this context to the given MessageBuilder.

Parameters:

builder (MessageBuilder) –

Return type:

None

__eq__(other)

Return self==value.

Parameters:

other (Any) –

Return type:

bool

__hash__()

Return hash(self).

Return type:

int

class check_shapes.error_contexts.IndexContext

Bases: ErrorContext

An error occurred in the context of an index in a sequence.

print(builder)

Print this context to the given MessageBuilder.

Parameters:

builder (MessageBuilder) –

Return type:

None

__eq__(other)

Return self==value.

Parameters:

other (Any) –

Return type:

bool

__hash__()

Return hash(self).

Return type:

int

class check_shapes.error_contexts.MappingKeyContext

Bases: ErrorContext

An error occurent in the context of a key in a map.

print(builder)

Print this context to the given MessageBuilder.

Parameters:

builder (MessageBuilder) –

Return type:

None

__eq__(other)

Return self==value.

Parameters:

other (Any) –

Return type:

bool

__hash__()

Return hash(self).

Return type:

int

class check_shapes.error_contexts.MappingValueContext

Bases: ErrorContext

An error occurent in the context of a value in a map.

print(builder)

Print this context to the given MessageBuilder.

Parameters:

builder (MessageBuilder) –

Return type:

None

__eq__(other)

Return self==value.

Parameters:

other (Any) –

Return type:

bool

__hash__()

Return hash(self).

Return type:

int

class check_shapes.error_contexts.ConditionContext

Bases: ErrorContext

An error occurred in a conditional context.

print(builder)

Print this context to the given MessageBuilder.

Parameters:

builder (MessageBuilder) –

Return type:

None

class check_shapes.error_contexts.ShapeContext

Bases: ErrorContext

An error occurred in the context of the shapes of function arguments.

print(builder)

Print this context to the given MessageBuilder.

Parameters:

builder (MessageBuilder) –

Return type:

None

class check_shapes.error_contexts.NoteContext

Bases: ErrorContext

An error occurred in a context where a user has added a note.

print(builder)

Print this context to the given MessageBuilder.

Parameters:

builder (MessageBuilder) –

Return type:

None

class check_shapes.error_contexts.ObjectValueContext

Bases: ErrorContext

An error was caused by the value of an object.

print(builder)

Print this context to the given MessageBuilder.

Parameters:

builder (MessageBuilder) –

Return type:

None

__eq__(other)

Return self==value.

Parameters:

other (Any) –

Return type:

bool

__hash__()

Return hash(self).

Return type:

int

class check_shapes.error_contexts.ObjectTypeContext

Bases: ErrorContext

An error was caused by the type of an object.

print(builder)

Print this context to the given MessageBuilder.

Parameters:

builder (MessageBuilder) –

Return type:

None

__eq__(other)

Return self==value.

Parameters:

other (Any) –

Return type:

bool

__hash__()

Return hash(self).

Return type:

int

class check_shapes.error_contexts.ParserInputContext

Bases: ErrorContext, abc.ABC

Abstract base class for contexts that relate to parser input.

class check_shapes.error_contexts.LarkUnexpectedInputContext

Bases: ParserInputContext

An error was caused by an UnexpectedInput error from Lark.

print(builder)

Print this context to the given MessageBuilder.

Parameters:

builder (MessageBuilder) –

Return type:

None

__hash__()

Return hash(self).

Return type:

int

class check_shapes.error_contexts.MultipleElementBoolContext

Bases: ParserInputContext

An error was caused by trying to use a multi-element argument specification as a bool.

print(builder)

Print this context to the given MessageBuilder.

Parameters:

builder (MessageBuilder) –

Return type:

None