Skip to content

type_common

fastforward.type_common.IntOr2Tuple = ScalarOr2Tuple[int] module-attribute #

fastforward.type_common.IntOrTuple = ScalarOrTuple[int] module-attribute #

fastforward.type_common.ScalarOr2Tuple = T | tuple[T, T] module-attribute #

fastforward.type_common.ScalarOrTuple = T | tuple[T, ...] module-attribute #

fastforward.type_common.SizeT = torch.Size | tuple[int, ...] module-attribute #

fastforward.type_common.T = TypeVar('T') module-attribute #

fastforward.type_common.Tuple3 = tuple[T, T, T] module-attribute #

fastforward.type_common.MethodType #

Bases: Enum

Enumeration representing different types of methods in a class.

Attributes:

Name Type Description
METHOD

Regular instance method that takes self as first parameter.

CLASS_METHOD

Class method decorated with @classmethod that takes cls as first parameter.

STATIC_METHOD

Static method decorated with @staticmethod that doesn't take self or cls.

NO_METHOD

Indicates that the class does not have a method with the specified name. Can also be used to indicate that a function reference is not a method.

CLASS_METHOD = enum.auto() class-attribute instance-attribute #

METHOD = enum.auto() class-attribute instance-attribute #

NO_METHOD = enum.auto() class-attribute instance-attribute #

STATIC_METHOD = enum.auto() class-attribute instance-attribute #

fastforward.type_common.method_type(cls_or_module, method_name) #

Determine the type of a method in a class.

Parameters:

Name Type Description Default
cls_or_module type | ModuleType

The class or module to inspect.

required
method_name str

The name of the method to check.

required

Returns:

Type Description
MethodType

A MethodType enum value indicating the type of the method:

Source code in fastforward/type_common.py
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
def method_type(cls_or_module: type | types.ModuleType, method_name: str, /) -> MethodType:
    """Determine the type of a method in a class.

    Args:
        cls_or_module: The class or module to inspect.
        method_name: The name of the method to check.

    Returns:
        A MethodType enum value indicating the type of the method:
    """
    if not isinstance(cls_or_module, (type, types.ModuleType)):
        msg = "'cls_or_module' must be a module or class"  # type: ignore[unreachable]
        raise ValueError(msg)
    match cls_or_module, cls_or_module.__dict__.get(method_name, None):
        case type(), classmethod():
            return MethodType.CLASS_METHOD
        case type(), staticmethod():
            return MethodType.STATIC_METHOD
        case type(), types.FunctionType():
            return MethodType.METHOD
        case _:
            return MethodType.NO_METHOD