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 diffrent 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.

property min_name_length
Return type

int

property i_control_code
Return type

bool

property max_name_length
Return type

int

property max_noqa_comments
Return type

int

property nested_classes_whitelist
Return type

Tuple[str, …]

property allowed_domain_names
Return type

Tuple[str, …]

property forbidden_domain_names
Return type

Tuple[str, …]

property max_arguments
Return type

int

property max_local_variables
Return type

int

property max_returns
Return type

int

property max_expressions
Return type

int

property max_module_members
Return type

int

property max_methods
Return type

int

property max_line_complexity
Return type

int

property max_jones_score
Return type

int

property max_imports
Return type

int

property max_imported_names
Return type

int

property max_base_classes
Return type

int

property max_decorators
Return type

int

property max_string_usages
Return type

int

property max_awaits
Return type

int

property max_try_body_length
Return type

int

property max_module_expressions
Return type

int

property max_function_expressions
Return type

int

property max_asserts
Return type

int

property max_access_level
Return type

int

property max_attributes
Return type

int

property max_cognitive_score
Return type

int

property max_cognitive_average
Return type

int

property max_call_level
Return type

int

property max_annotation_complexity
Return type

int

property max_import_from_members
Return type

int