Some functions that may be useful for various checkers.
Exception |
|
A format string ended in the middle of a format specifier. |
Exception |
|
Undocumented |
Exception |
|
Undocumented |
Exception |
|
A format character in a format string is not one of the supported format characters. |
Function | assign |
Return the higher parent which is not an AssignName, Tuple or List node. |
Function | assigned |
Returns true if a node is a nodes.Assign that returns a constant boolean. |
Function | check |
Kept for backwards compatibility, deprecated. |
Function | class |
Return true if the given class node should be considered as an abstract class. |
Function | collect |
Given a format string, return an iterator of all the valid format fields. |
Function | decorated |
Determine if the `func` node has a decorator with the qualified name `qname`. |
Function | decorated |
Detect if the given function node is decorated with a property. |
Function | defnode |
Undocumented |
Function | error |
Check if the given exception handler catches the given error_type. |
Function | find |
Yield the names of assignment targets, accounting for nested ones. |
Function | find |
Return the ExceptHandler in which the node is, without going out of scope. |
Function | find |
Undocumented |
Function | find |
Return the ExceptHandler or the TryExcept node in which the node is. |
Function | get |
Recursively returns all atoms in nested lists and tuples. |
Function | get |
Returns the specified argument from a function call. |
Function | get |
Return the contextlib suppressors handling the exception. |
Function | get |
Get all contextlib.with statements in the ancestors of the given node. |
Function | get |
Return the collections of handlers handling the exception in arguments. |
Function | get |
Get a prepared module name from the given import node. |
Function | get |
Returns the inverse comparator given a comparator. |
Function | get |
Get the name of the dictionary which keys are being iterated over on a ``nodes.For`` or ``nodes.Comprehension`` node. |
Function | get |
Return the first parent node that is any of the provided types (or None). |
Function | get |
Modified version of get_node_first_ancestor_of_type to also return the descendant visited directly before reaching the sought ancestor. |
Function | get |
Get the last lineno of the given node. |
Function | get |
Return the class that is the outer class of given (nested) class_node. |
Function | get |
Returns the value 'subscript.slice' of a Subscript node. |
Function | has |
Return true if all base classes of a class could be inferred. |
Function | has |
Yield ``True`` if a Starred node is found recursively. |
Function | in |
Returns True if stmt is inside the else branch for a parent For stmt. |
Function | in |
Check if a node is guarded by a TYPE_CHECKING guard. |
Function | infer |
Undocumented |
Function | inherit |
Return whether the given class node is subclass of exceptions.Exception. |
Function | is |
Return whether `frame` is an astroid.Class node with `node` in the subtree of its bases attribute. |
Function | is |
Test if AssignName node has `typing_name` annotation. |
Function | is |
Check that attribute name is private (at least two leading underscores, at most one trailing underscore). |
Function | is |
Return True if attribute name is protected (start with _ and some other details), False otherwise. |
Function | is |
Test if attribute is typed annotation in current node or any base nodes. |
Function | is |
Determine if the node is assigning itself (with modifications) to itself. |
Function | is |
Undocumented |
Function | is |
Return True if node is the function being called in a Call node. |
Function | is |
Return true if <name> could be considered as a builtin defined by python. |
Function | is |
Returns True if the given node is an object from the __builtin__ module. |
Function | is |
Checks if node is a function call with the given name. |
Function | is |
Undocumented |
Function | is |
Check if class is subscriptable with PEP 585 and postponed evaluation enabled. |
Function | is |
Test if ClassDef node is Type. |
Function | is |
Undocumented |
Function | is |
Return true if the given Name node is used in function or lambda default argument's value. |
Function | is |
Searches for a tree node that defines the given variable name. |
Function | is |
Check if the given variable node is defined before. |
Function | is |
Undocumented |
Function | is |
Check if the given variable name is deleted in the same scope after the current node. |
Function | is |
Undocumented |
Function | is |
Undocumented |
Function | is |
Undocumented |
Function | is |
Return true if the given function node only raises an exception. |
Function | is |
Check if the given node is from a fallback import block. |
Function | is |
Return true if the name is used in function decorator. |
Function | is |
Checks whether a function body only consists of a single Ellipsis. |
Function | is |
Return whether any inferred value of `node` is hashable. |
Function | is |
Undocumented |
Function | is |
Return whether the given node is inside a lambda. |
Function | is |
Undocumented |
Function | is |
Undocumented |
Function | is |
Undocumented |
Function | is |
Return True if node is part for guarded if block. |
Function | is |
Check if node is in type annotation context. |
Function | is |
Return True if node is part for guarded `typing.TYPE_CHECKING` if block. |
Function | is |
Check if the node is directly under a Try/Except statement (but not under an ExceptHandler!). |
Function | is |
Undocumented |
Function | is |
Check if a node is a function stub decorated with typing.overload. |
Function | is |
Check if the postponed evaluation of annotations is enabled. |
Function | is |
Check if the given node is a property deleter. |
Function | is |
Check if the given node is a property setter. |
Function | is |
Check if the given node is either a property setter or a deleter. |
Function | is |
Check if the given node represents a protocol class. |
Function | is |
Check if the given variable name is reassigned in the same scope after the current node. |
Function | is |
Check if the given function node is a singledispatch function. |
Function | is |
Check if the given function node is a singledispatchmethod function. |
Function | is |
Undocumented |
Function | is |
Check if first node is a subclass of second node. |
Function | is |
Return True if the node is referencing the "super" builtin function. |
Function | is |
Return True if IF stmt is a sys.version_info guard. |
Function | is |
Detect call to exit(), quit(), os._exit(), or sys.exit(). |
Function | is |
Returns true if the given node is being tested for truthiness. |
Function | is |
Return True if IF stmt is a typing guard. |
Function | is |
Check if `node` is a member of the `typing` module and has one of the names from `names_to_check`. |
Function | node |
Return the class that is wrapping the given node. |
Function | node |
Check if the node is in a TryExcept which handles the given exception. |
Function | node |
Return the inferred type for `node`. |
Function | not |
Undocumented |
Function | only |
Decorator to store messages that are handled by a checker method as an attribute of the function object. |
Function | overrides |
Return True if <name> is a method overridden from an ancestor which is not the base object class. |
Function | parse |
Parses a PEP 3101 format string, returning a tuple of (keyword_arguments, implicit_pos_args_cnt, explicit_pos_args). |
Function | parse |
Parses a format string, returning a tuple (keys, num_args). |
Function | returns |
Returns true if a node is a nodes.Return that returns a constant boolean. |
Function | safe |
Return the inferred value for the given node. |
Function | split |
Undocumented |
Function | supports |
Undocumented |
Function | supports |
Undocumented |
Function | supports |
Undocumented |
Function | supports |
Undocumented |
Function | unimplemented |
Get the unimplemented abstract methods for the given *node*. |
Function | uninferable |
Return a list of uninferable `typing.final` decorators in `node`. |
Constant | ABC |
Undocumented |
Constant | ABC |
Undocumented |
Constant | AITER |
Undocumented |
Constant | CLASS |
Undocumented |
Constant | COMMUTATIVE |
Undocumented |
Constant | COMP |
Undocumented |
Constant | CONTAINS |
Undocumented |
Constant | DELITEM |
Undocumented |
Constant | EXCEPTIONS |
Undocumented |
Constant | GETITEM |
Undocumented |
Constant | ITER |
Undocumented |
Constant | KEYS |
Undocumented |
Constant | NEXT |
Undocumented |
Constant | PYMETHODS |
Undocumented |
Constant | SETITEM |
Undocumented |
Constant | SINGLETON |
Undocumented |
Constant | SPECIAL |
Undocumented |
Constant | SPECIAL |
Undocumented |
Constant | SUBSCRIPTABLE |
Undocumented |
Constant | TERMINATING |
Undocumented |
Constant | TYPING |
Undocumented |
Type Alias |
|
Undocumented |
Variable | builtins |
Undocumented |
Function | _except |
Undocumented |
Function | _get |
Undocumented |
Function | _is |
Undocumented |
Function | _is |
Undocumented |
Function | _is |
Undocumented |
Function | _is |
Determine whether the target name-like node is referenced in the side node. |
Function | _supports |
Undocumented |
Function | _supports |
Undocumented |
Function | _supports |
Undocumented |
Function | _supports |
Undocumented |
Function | _supports |
Undocumented |
Function | _supports |
Undocumented |
Function | _supports |
Undocumented |
Function | _supports |
Undocumented |
Function | _supports |
Undocumented |
Function | _suppresses |
Check if the given node suppresses the given exception. |
Constant | _SPECIAL |
Undocumented |
Type Variable | _ |
Undocumented |
Type Variable | _ |
Undocumented |
str
) -> Callable[ [ AstCallbackMethod[ _CheckerT, _NodeT]], AstCallbackMethod[ _CheckerT, _NodeT]]
:
(source)
¶
Kept for backwards compatibility, deprecated. Use only_required_for_messages instead, which conveys the intent of the decorator much clearer.
Given a format string, return an iterator of all the valid format fields. It handles nested fields as well.
((nodes.ClassDef|nodes.FunctionDef)|astroid.BoundMethod)|astroid.UnboundMethod
, qnames: Iterable[ str]
) -> bool
:
(source)
¶
Determine if the `func` node has a decorator with the qualified name `qname`.
nodes.NodeNG
, varname: str
, scope: nodes.NodeNG
) -> nodes.NodeNG|None
:
(source)
¶
Undocumented
nodes.ExceptHandler
, error_type: (str|type[ Exception])|tuple[ str|type[ Exception], ...]
) -> bool
:
(source)
¶
Check if the given exception handler catches the given error_type. The *handler* parameter is a node, representing an ExceptHandler node. The *error_type* can be an exception, such as AttributeError, the name of an exception, or it can be a tuple of errors. The function will return True if the handler catches any of the given errors.
nodes.AssignName|nodes.BaseContainer
) -> Iterator[ str]
:
(source)
¶
Yield the names of assignment targets, accounting for nested ones.
nodes.NodeNG
) -> (nodes.ExceptHandler|nodes.TryExcept)|None
:
(source)
¶
Return the ExceptHandler in which the node is, without going out of scope.
nodes.NodeNG
) -> (nodes.ExceptHandler|nodes.TryExcept)|None
:
(source)
¶
Return the ExceptHandler or the TryExcept node in which the node is.
nodes.Call
, position: int|None
= None, keyword: str|None
= None) -> nodes.Name
:
(source)
¶
Returns the specified argument from a function call. :param nodes.Call call_node: Node representing a function call to check. :param int position: position of the argument. :param str keyword: the keyword of the argument. :returns: The node representing the argument, None if the argument is not found. :rtype: nodes.Name :raises ValueError: if both position and keyword are None. :raises NoSuchArgumentError: if no argument at the provided position or with the provided keyword.
nodes.NodeNG
, exception: type[ Exception]|str
= Exception) -> Iterator[ nodes.With]
:
(source)
¶
Return the contextlib suppressors handling the exception. Args: node (nodes.NodeNG): A node that is potentially wrapped in a contextlib.suppress. exception (builtin.Exception): exception or name of the exception. Yields: nodes.With: A with node that is suppressing the exception.
nodes.NodeNG
, exception: type[ Exception]|str
= Exception) -> list[ nodes.ExceptHandler]|None
:
(source)
¶
Return the collections of handlers handling the exception in arguments. Args: node (nodes.NodeNG): A node that is potentially wrapped in a try except. exception (builtin.Exception or str): exception or name of the exception. Returns: list: the collection of handlers that are handling the exception or None.
Get a prepared module name from the given import node. In the case of relative imports, this will return the absolute qualified module name, which might be useful for debugging. Otherwise, the initial module name is returned unchanged. :param importnode: node representing import statement. :param modname: module name from import statement. :returns: absolute qualified module name of the module used in import.
Returns the inverse comparator given a comparator. E.g. when given "==", returns "!=" :param str op: the comparator to look up. :returns: The inverse of the comparator in string format :raises KeyError: if input is not recognized as a comparator
Get the name of the dictionary which keys are being iterated over on a ``nodes.For`` or ``nodes.Comprehension`` node. If the iterating object is not either the keys method of a dictionary or a dictionary itself, this returns None.
nodes.NodeNG
, ancestor_type: type[ _NodeT]|tuple[ type[ _NodeT], ...]
) -> _NodeT|None
:
(source)
¶
Return the first parent node that is any of the provided types (or None).
nodes.NodeNG
, ancestor_type: type[ _NodeT]|tuple[ type[ _NodeT], ...]
) -> tuple[ None, None]|tuple[ _NodeT, nodes.NodeNG]
:
(source)
¶
Modified version of get_node_first_ancestor_of_type to also return the descendant visited directly before reaching the sought ancestor. Useful for extracting whether a statement is guarded by a try, except, or finally when searching for a TryFinally ancestor.
Get the last lineno of the given node. For a simple statement this will just be node.lineno, but for a node that has child statements (e.g. a method) this will be the lineno of the last child statement recursively.
Returns the value 'subscript.slice' of a Subscript node. :param node: Subscript Node to extract value from :returns: Const Node containing subscript value :raises InferredTypeError: if the subscript node cannot be inferred as a Const
nodes.ClassDef
, context: InferenceContext|None
= None) -> bool
:
(source)
¶
Return true if all base classes of a class could be inferred.
(nodes.For|nodes.Comprehension)|nodes.Set
) -> Iterator[ bool]
:
(source)
¶
Yield ``True`` if a Starred node is found recursively.
def in_for_else_branch(parent:
nodes.NodeNG
, stmt: nodes.Statement
) -> bool
:
(source)
¶
Returns True if stmt is inside the else branch for a parent For stmt.
def infer_all(node:
nodes.NodeNG
, context: InferenceContext|None
= None) -> list[ InferenceResult]
:
(source)
¶
Undocumented
Test if AssignName node has `typing_name` annotation. Especially useful to check for `typing._SpecialForm` instances like: `Union`, `Optional`, `Literal`, `ClassVar`, `Final`.
Check that attribute name is private (at least two leading underscores, at most one trailing underscore).
nodes.ClassDef|astroid.Instance
, attr_name: str
) -> bool
:
(source)
¶
Test if attribute is typed annotation in current node or any base nodes.
nodes.ClassDef
, node: nodes.NodeNG
) -> bool
:
(source)
¶
Check if class is subscriptable with PEP 585 and postponed evaluation enabled.
Check if the given variable node is defined before. Verify that the variable node is defined by a parent node (e.g. if or with) earlier than `var_node`, or is defined by a (list, set, dict, or generator comprehension, lambda) or in a previous sibling node on the same line (statement_defining ; statement_using).
nodes.NodeNG
, varname: str
, scope: nodes.NodeNG
) -> bool
:
(source)
¶
Undocumented
Check if node is in type annotation context. Check for 'AnnAssign', function 'Arguments', or part of function return type annotation.
Check if the node is directly under a Try/Except statement (but not under an ExceptHandler!). Args: node (nodes.Raise): the node raising the exception. Returns: bool: True if the node is inside a try/except statement, False otherwise.
Check if a node is a function stub decorated with typing.overload. :param node: Node to check. :returns: True if node is an overload function stub. False otherwise.
Check if the given node represents a protocol class. :param cls: The node to check :returns: True if the node is or inherits from typing.Protocol directly, false otherwise.
Check if first node is a subclass of second node. :param child: Node to check for subclass. :param parent: Node to check for superclass. :returns: True if child is derived from parent. False otherwise.
Return True if IF stmt is a sys.version_info guard. >>> import sys >>> if sys.version_info > (3, 8): >>> from typing import Literal >>> else: >>> from typing_extensions import Literal
Return True if IF stmt is a typing guard. >>> from typing import TYPE_CHECKING >>> if TYPE_CHECKING: >>> from xyz import a
Return the class that is wrapping the given node. The function returns a class for a method node (or a staticmethod or a classmethod), otherwise it returns `None`.
nodes.NodeNG
, exception: type[ Exception]|str
= Exception) -> bool
:
(source)
¶
Check if the node is in a TryExcept which handles the given exception. If the exception is not given, the function is going to look for bare excepts.
Return the inferred type for `node`. If there is more than one possible type, or if inferred type is Uninferable or None, return None
(((nodes.Compare|nodes.Name)|nodes.UnaryOp)|nodes.BoolOp)|nodes.BinOp
) -> str
:
(source)
¶
Undocumented
str
) -> Callable[ [ AstCallbackMethod[ _CheckerT, _NodeT]], AstCallbackMethod[ _CheckerT, _NodeT]]
:
(source)
¶
Decorator to store messages that are handled by a checker method as an attribute of the function object. This information is used by ``ASTWalker`` to decide whether to call the decorated method or not. If none of the messages is enabled, the method will be skipped. Therefore, the list of messages must be well maintained at all times! This decorator only has an effect on ``visit_*`` and ``leave_*`` methods of a class inheriting from ``BaseChecker``.
str
) -> tuple[ list[ tuple[ str, list[ tuple[ bool, str]]]], int, int]
:
(source)
¶
Parses a PEP 3101 format string, returning a tuple of (keyword_arguments, implicit_pos_args_cnt, explicit_pos_args). keyword_arguments is the set of mapping keys in the format string, implicit_pos_args_cnt is the number of arguments required by the format string and explicit_pos_args is the number of arguments passed with the position.
str
) -> tuple[ set[ str], int, dict[ str, str], list[ str]]
:
(source)
¶
Parses a format string, returning a tuple (keys, num_args). Where 'keys' is the set of mapping keys in the format string, and 'num_args' is the number of arguments required by the format string. Raises IncompleteFormatString or UnsupportedFormatCharacter if a parse error occurs.
def safe_infer(node:
nodes.NodeNG
, context: InferenceContext|None
= None, *, compare_constants: bool
= False) -> InferenceResult|None
:
(source)
¶
Return the inferred value for the given node. Return None if inference failed or if there is some ambiguity (more than one node has been inferred of different types). If compare_constants is True and if multiple constants are inferred, unequal inferred values are also considered ambiguous and return None.
str
) -> tuple[ str, Iterable[ tuple[ bool, str]]]
:
(source)
¶
Undocumented
def unimplemented_abstract_methods(node:
nodes.ClassDef
, is_abstract_cb: nodes.FunctionDef
= None) -> dict[ str, nodes.FunctionDef]
:
(source)
¶
Get the unimplemented abstract methods for the given *node*. A method can be considered abstract if the callback *is_abstract_cb* returns a ``True`` value. The check defaults to verifying that a method is decorated with abstract methods. It will return a dictionary of abstract method names and their inferred objects.
nodes.Decorators
) -> list[ (nodes.Attribute|nodes.Name)|None]
:
(source)
¶
Return a list of uninferable `typing.final` decorators in `node`. This function is used to determine if the `typing.final` decorator is used with an unsupported Python version; the decorator cannot be inferred when using a Python version lower than 3.8.
Undocumented
Value |
|
Undocumented
Value |
|
Undocumented
Value |
|
nodes.ExceptHandler
, exceptions: tuple[ type[ ImportError], type[ ModuleNotFoundError]]
) -> bool
:
(source)
¶
Undocumented
nodes.AssignName|nodes.AssignAttr
, side: nodes.NodeNG|None
) -> bool
:
(source)
¶
Determine whether the target name-like node is referenced in the side node.
nodes.NodeNG
, protocol_callback: Callable[ [ nodes.NodeNG], bool]
) -> bool
:
(source)
¶
Undocumented
nodes.Call
, exception: type[ Exception]|str
= Exception) -> bool
:
(source)
¶
Check if the given node suppresses the given exception.
Undocumented
Value |
|