CodeQL library for Python
codeql/python-all 7.1.0 (changelog, source)
Search

Module DuckTyping

Provides predicates for approximating type properties of user-defined classes based on their structure (method declarations, base classes).

This module should not be used in the call graph computation itself, as parts of it may depend on layers that themselves build upon the call graph (e.g. API graphs).

Import path

import semmle.python.dataflow.new.internal.DataFlowDispatch

Predicates

declaresAttribute

Holds if cls directly assigns to an attribute named name in its class body. This covers attribute assignments like x = value, but not method definitions.

getAnAttributeValue

Gets the value expression assigned to attribute name directly in the class body of cls.

getInit

Gets the __init__ function that will be invoked when cls is constructed, resolved according to the MRO.

hasMethod

Holds if cls or any of its resolved superclasses declares a method with the given name.

hasUnreliableMro

Holds if cls or any of its superclasses uses multiple inheritance, or has an unresolved base class. In these cases, our MRO approximation may resolve to the wrong __init__, so we should not flag argument mismatches.

hasUnresolvedBase

Holds if cls has a base class that cannot be resolved to a user-defined class and is not just object, meaning it may inherit methods from an unknown class.

isCallable

Holds if cls is callable, i.e. it declares __call__.

isContainer

Holds if cls supports the container protocol, i.e. it declares __contains__, __iter__, or __getitem__.

isContextManager

Holds if cls supports the context manager protocol, i.e. it declares both __enter__ and __exit__.

isDescriptor

Holds if cls supports the descriptor protocol, i.e. it declares __get__, __set__, or __delete__.

isIterable

Holds if cls supports the iterable protocol, i.e. it declares __iter__ or __getitem__.

isIterator

Holds if cls supports the iterator protocol, i.e. it declares both __iter__ and __next__.

isMapping

Holds if cls supports the mapping protocol, i.e. it declares __getitem__ and keys, or __getitem__ and __iter__.

isNewStyle

Holds if cls is a new-style class. In Python 3, all classes are new-style. In Python 2, a class is new-style if it (transitively) inherits from object, or has a declared __metaclass__, or is in a module with a module-level __metaclass__ declaration, or has an unresolved base class.

isPropertyAccessor

Holds if f is a property accessor (decorated with @property, @name.setter, or @name.deleter).

overridesMethod

Holds if f overrides a method in a superclass with the same name.

overridesMethod

Holds if f overrides overridden declared in superclass.