edq.util.reflection

 1import typing
 2
 3def get_qualified_name(target: typing.Union[type, object, str]) -> str:
 4    """
 5    Try to get a qualified name for a type (or for the type of an object).
 6    Names will not always come out clean.
 7    """
 8
 9    # Get the type for this target.
10    if (isinstance(target, type)):
11        target_class = target
12    elif (callable(target)):
13        target_class = typing.cast(type, target)
14    else:
15        target_class = type(target)
16
17    # Check for various name components.
18    parts = []
19
20    if (hasattr(target_class, '__module__')):
21        parts.append(str(getattr(target_class, '__module__')))
22
23    if (hasattr(target_class, '__qualname__')):
24        parts.append(str(getattr(target_class, '__qualname__')))
25    elif (hasattr(target_class, '__name__')):
26        parts.append(str(getattr(target_class, '__name__')))
27
28    # Fall back to just the string reprsentation.
29    if (len(parts) == 0):
30        return str(target_class)
31
32    return '.'.join(parts)
def get_qualified_name(target: Union[type, object, str]) -> str:
 4def get_qualified_name(target: typing.Union[type, object, str]) -> str:
 5    """
 6    Try to get a qualified name for a type (or for the type of an object).
 7    Names will not always come out clean.
 8    """
 9
10    # Get the type for this target.
11    if (isinstance(target, type)):
12        target_class = target
13    elif (callable(target)):
14        target_class = typing.cast(type, target)
15    else:
16        target_class = type(target)
17
18    # Check for various name components.
19    parts = []
20
21    if (hasattr(target_class, '__module__')):
22        parts.append(str(getattr(target_class, '__module__')))
23
24    if (hasattr(target_class, '__qualname__')):
25        parts.append(str(getattr(target_class, '__qualname__')))
26    elif (hasattr(target_class, '__name__')):
27        parts.append(str(getattr(target_class, '__name__')))
28
29    # Fall back to just the string reprsentation.
30    if (len(parts) == 0):
31        return str(target_class)
32
33    return '.'.join(parts)

Try to get a qualified name for a type (or for the type of an object). Names will not always come out clean.