Return the name of a type Examples -------- >>> typename(int) 'int' >>> from dask.core import literal >>> typename(literal) 'dask.core.literal' >>> typename(literal, short=True) 'dask.literal'
(typ: Any, short: bool = False)
| 1048 | |
| 1049 | |
| 1050 | def typename(typ: Any, short: bool = False) -> str: |
| 1051 | """ |
| 1052 | Return the name of a type |
| 1053 | |
| 1054 | Examples |
| 1055 | -------- |
| 1056 | >>> typename(int) |
| 1057 | 'int' |
| 1058 | |
| 1059 | >>> from dask.core import literal |
| 1060 | >>> typename(literal) |
| 1061 | 'dask.core.literal' |
| 1062 | >>> typename(literal, short=True) |
| 1063 | 'dask.literal' |
| 1064 | """ |
| 1065 | if not isinstance(typ, type): |
| 1066 | return typename(type(typ)) |
| 1067 | try: |
| 1068 | if not typ.__module__ or typ.__module__ == "builtins": |
| 1069 | return typ.__name__ |
| 1070 | else: |
| 1071 | if short: |
| 1072 | module, *_ = typ.__module__.split(".") |
| 1073 | else: |
| 1074 | module = typ.__module__ |
| 1075 | return f"{module}.{typ.__name__}" |
| 1076 | except AttributeError: |
| 1077 | return str(typ) |
| 1078 | |
| 1079 | |
| 1080 | def ensure_bytes(s) -> bytes: |
searching dependent graphs…