Types

This module contains knowledge about the most important types that we use.

There are also different visitor specific types that are defined and use exclusively in that file.

Policy

If any of the following statements is true, move the type to this file:

  • if type is used in multiple files

  • if type is complex enough it has to be documented

  • if type is very important for the public API

final

As you can see in the source code almost everything is marked as @final or Final.

It means that this value cannot be subclassed or reassigned. This it only a mypy feature, it does not affect python runtime.

We do this, because we value composition over inheritance. And this @final decorators help you to define readable and clear APIs for cases when inheritance is used.

See also

My guide about @final type in python: https://sobolevn.me/2018/07/real-python-contants

Reference

AnyText = typing.Union[_ast.Str, _ast.Bytes]

We use this type to represent all string-like nodes.

AnyImport = typing.Union[_ast.Import, _ast.ImportFrom]

In cases we need to work with both import types.

AnyFunctionDef = typing.Union[_ast.FunctionDef, _ast.AsyncFunctionDef]

In cases we need to work with both function definitions.

AnyFunctionDefAndLambda = typing.Union[_ast.FunctionDef, _ast.AsyncFunctionDef, _ast.Lambda]

In cases we need to work with all function definitions (including lambdas).

AnyIf = typing.Union[_ast.If, _ast.IfExp]

In cases we need to work with both forms of if functions.

AnyFor = typing.Union[_ast.For, _ast.AsyncFor]

In cases we need to work with both sync and async loops.

AnyLoop = typing.Union[_ast.For, _ast.AsyncFor, _ast.While]

sync, async, and while.

Type

In case we need to work with any loop

AnyComprehension = typing.Union[_ast.ListComp, _ast.DictComp, _ast.SetComp, _ast.GeneratorExp]

All different comprehension types in one place.

AnyWith = typing.Union[_ast.With, _ast.AsyncWith]

In cases we need to work with both sync and async context managers.

AnyNodes = typing.Tuple[typing.Type[_ast.AST], ...]

Tuple of AST node types for declarative syntax.

AnyAssign = typing.Union[_ast.Assign, _ast.AnnAssign]

When we search for assign elements, we also need typed assign.

ContextNodes = typing.Union[_ast.Module, _ast.ClassDef, _ast.FunctionDef, _ast.AsyncFunctionDef]

That’s how we define context of operations.

AnyAccess = typing.Union[_ast.Attribute, _ast.Subscript]

In cases we need to work with both access types.

AnyTextPrimitive = typing.Union[str, bytes]

We use this type to work with any text-like values. Related to AnyText.

CheckResult = typing.Tuple[int, int, str, type]

Flake8 API format to return error messages.

class ConfigurationOptions(*args, **kwargs)[source]

Bases: typing_extensions.Protocol

Provides structure for the options we use in our checker and visitors.

Then this protocol is passed to each individual visitor. It uses structural sub-typing, and does not represent any kind of a real class or structure.

We use @property decorator here instread of regular attributes, because we need to explicitly mark these atrtibutes as read-only.