API Documentation#

Attention

This documentation was autogenerated using sphinx-apidoc from source docstrings, which could be incomplete. Contribute by creating a PR!

Capture and report three different conditions for boolean traps.

Currently detects three conditions:

class flake8_boolean_trap.BooleanTrapReason(value)[source]#

Bases: Enum

Defines boolean trap detection reasons.

Enum values should contain a string template to be formatted

DEFAULT_VALUE = 'FBT002 do not set boolean defaults for positional args. Hint: in `def {func}(...)`, define `{arg}` as kw-only'#

Function definition contains boolean default in positional arg.

FUNCTION_CALL = 'FBT003 do not use boolean positional args. Hint: in `{func}(..)`, refactor positional arg #{position} to include its argument name'#

Function call uses boolean positional arg.

TYPE_HINT = 'FBT001 do not define boolean positional args. Hint: in `def {func}(...)` , define `{arg}` as kw-only'#

Function definition contains boolean type hint in positional arg.

class flake8_boolean_trap.Plugin(tree: AST)[source]#

Bases: object

Flake8 plugin implementation.

Just reports the violations stored in Visitor.registry

Initialize flake8 plugin by storing the ast tree at instance level.

Parameters:

tree – the whole module ast tree to parse.

__init__(tree: AST) None[source]#

Initialize flake8 plugin by storing the ast tree at instance level.

Parameters:

tree – the whole module ast tree to parse.

name = 'flake8_boolean_trap'#
run() Generator[Tuple[int, int, str, Type[Any]], None, None][source]#

Report lint errors as parsed from Visitor.

Yields:

Boolean trap detections with their location and reason.

version = '0.1.0'#
class flake8_boolean_trap.Visitor[source]#

Bases: NodeVisitor

Ast visitor to group violations in a registry.

Currently visits only function definitions and calls, where boolean traps might be located. See Visitor.visit_FunctionDef and Visitor.visit_Call, respectively.

Initialize the node visitor with an empty registry.

__init__() None[source]#

Initialize the node visitor with an empty registry.

visit_Call(call: Call) None[source]#

Capture boolean traps in function calls.

Parameters:

call – the call ast to inspect.

See Also

BooleanTrapReason.FUNCTION_CALL

visit_FunctionDef(func_node: FunctionDef) None[source]#

Capture boolean traps in function definitions.

Parameters:

func_node – the function ast to inspect.

See Also
flake8_boolean_trap.default_values(func_node: FunctionDef) Generator[Tuple[expr, int, int, str], None, None][source]#

Retrieve function args and respective default values.

Parameters:

func_node – The functions ast to parse its args. Args are iterated in reversed order because sizes could differ.

“defaults is a list of default values for arguments that can be passed positionally. If there are fewer defaults, they correspond to the last n arguments.”

Yields:

Candidates for args with default values.

flake8_boolean_trap.is_boolean_default(default: expr) bool[source]#

Detect whether a default argument value is a boolean.

Parameters:

default – Default parameter value ast.

Returns:

True if the default value is a boolean

flake8_boolean_trap.is_boolean_typehint(hint: expr) bool[source]#

Detect whether a type hint corresponds to a boolean.

Parameters:

hint – Type hint ast.

Returns:

True if the type hint corresponds to a boolean

flake8_boolean_trap.positional_hints(func_node: FunctionDef) Generator[Tuple[expr, int, int, str], None, None][source]#

Extract type hints from postiional arguments.

Parameters:

func_node – Function ast containing the arguments to search.

Yields:

Type annotations for arguments which could be used as positional.