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 can not 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

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.

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

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

CheckResult

alias of typing.Tuple

AnyNodes

alias of typing.Tuple

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.

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.