sourcery_analytics.conditions

True or False statements about nodes.

sourcery_analytics.conditions.always(_node: NodeNG) bool

Always True for any node.

Useful as a default fallback.

sourcery_analytics.conditions.is_type(*t: Type[NodeNG]) Callable[[NodeNG], bool]

Construct a Condition based on the type of the node.

Examples

>>> is_method = is_type(astroid.nodes.FunctionDef)
>>> module = astroid.parse("def foo(): pass")
>>> is_method(module)
False
>>> is_method(module.body[0])
True
sourcery_analytics.conditions.is_elif(node: NodeNG) bool

True if the node corresponds to an elif statement.

Python parsers treat if.. elif.. statements as a nested if.. else: if.. statement, so detecting elifs is not trivial. Here, we assert that an if statement is an elif if its parent is an if, it is the first statement of the parent’s else condition, and the column offset is the same.

Note

Because of the final condition, this will not work if the node is constructed manually.

From an execution perspective, this distinction isn’t relevant, but from a code quality perspective, (especially for cognitive complexity) it is important.

sourcery_analytics.conditions.is_method(node: NodeNG)
sourcery_analytics.conditions.is_const(node: NodeNG)
sourcery_analytics.conditions.is_name(node: NodeNG)