Reference
Named types.
This library defines the Named protocol for types
that contain the __name__ attribute, abstracting the attribute itself away.
It also provides the get_name function to fetch
the name of the item provided, along with get_type_name, such that
get_type_item(item)
# is equivalent to
get_name(type(item))
Lastly, there is the type guard function called is_named
(aliased as has_name), which checks for the presence
of __name__ attribute (which is exported in the NAME constant).
This library also provides the functionality for accessing and manipulating module names:
__name__ |
__module__ |
|---|---|
Named |
Moduled |
get_name |
get_module |
get_type_name |
get_type_module |
is_named |
is_moduled |
has_name |
has_module |
NAME |
MODULE |
MODULE = '__module__'
module-attribute
The literal __module__ string.
NAME = '__name__'
module-attribute
The literal __name__ string.
has_module = is_moduled
module-attribute
An alias of is_moduled.
has_name = is_named
module-attribute
An alias of is_named.
Moduled
Bases: Protocol
The moduled protocol for types that contain the __module__ attribute.
Source code in named/core.py
103 104 105 106 107 | |
Named
Bases: Protocol
The named protocol for types that contain the __name__ attribute.
Source code in named/core.py
32 33 34 35 36 | |
get_module(item: Moduled) -> str
Fetches the __module__ of the Moduled item.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
item |
Moduled
|
The item to fetch the module of. |
required |
Returns:
| Type | Description |
|---|---|
str
|
The module of the item. |
Source code in named/core.py
110 111 112 113 114 115 116 117 118 119 | |
get_name(item: Named) -> str
Fetches the __name__ of the Named item.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
item |
Named
|
The item to fetch the name of. |
required |
Returns:
| Type | Description |
|---|---|
str
|
The name of the item. |
Source code in named/core.py
39 40 41 42 43 44 45 46 47 48 | |
get_type_module(item: Any) -> str
Fetches the __module__ of the item type.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
item |
Any
|
The item to fetch the type module of. |
required |
Returns:
| Type | Description |
|---|---|
str
|
The module of the item type. |
Source code in named/core.py
132 133 134 135 136 137 138 139 140 141 | |
get_type_name(item: Any) -> str
Fetches the __name__ of the item type.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
item |
Any
|
The item to fetch the type name of. |
required |
Returns:
| Type | Description |
|---|---|
str
|
The name of the item type. |
Source code in named/core.py
61 62 63 64 65 66 67 68 69 70 | |
is_moduled(item: Any) -> TypeIs[Moduled]
Checks if the item implements the Moduled protocol.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
item |
Any
|
The item to check. |
required |
Returns:
| Type | Description |
|---|---|
TypeIs[Moduled]
|
Whether the item implements the |
Source code in named/core.py
154 155 156 157 158 159 160 161 162 163 | |
is_named(item: Any) -> TypeIs[Named]
Checks if the item implements the Named protocol.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
item |
Any
|
The item to check. |
required |
Returns:
| Type | Description |
|---|---|
TypeIs[Named]
|
Whether the item implements the |
Source code in named/core.py
83 84 85 86 87 88 89 90 91 92 | |
set_module(item: Moduled, module: str) -> None
Sets the __module__ of the Moduled item.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
item |
Moduled
|
The item to set the module of. |
required |
module |
str
|
The module to set on the item. |
required |
Source code in named/core.py
122 123 124 125 126 127 128 129 | |
set_name(item: Named, name: str) -> None
Sets the __name__ of the Named item.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
item |
Named
|
The item to set the name of. |
required |
name |
str
|
The name to set on the item. |
required |
Source code in named/core.py
51 52 53 54 55 56 57 58 | |
set_type_module(item: Any, module: str) -> None
Sets the __module__ of the item type.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
item |
Any
|
The item to set the type module of. |
required |
module |
str
|
The module to set on the item type. |
required |
Source code in named/core.py
144 145 146 147 148 149 150 151 | |
set_type_name(item: Any, name: str) -> None
Sets the __name__ of the item type.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
item |
Any
|
The item to set the type name of. |
required |
name |
str
|
The name to set on the item type. |
required |
Source code in named/core.py
73 74 75 76 77 78 79 80 | |