Skip to content

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
@runtime_checkable
class Moduled(Protocol):
    """The moduled protocol for types that contain the `__module__` attribute."""

    __module__: str

Named

Bases: Protocol

The named protocol for types that contain the __name__ attribute.

Source code in named/core.py
32
33
34
35
36
@runtime_checkable
class Named(Protocol):
    """The named protocol for types that contain the `__name__` attribute."""

    __name__: str

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
def get_module(item: Moduled) -> str:
    """Fetches the `__module__` of the [`Moduled`][named.core.Moduled] `item`.

    Arguments:
        item: The item to fetch the module of.

    Returns:
        The module of the item.
    """
    return item.__module__

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
def get_name(item: Named) -> str:
    """Fetches the `__name__` of the [`Named`][named.core.Named] `item`.

    Arguments:
        item: The item to fetch the name of.

    Returns:
        The name of the item.
    """
    return item.__name__

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
def get_type_module(item: Any) -> str:
    """Fetches the `__module__` of the `item` type.

    Arguments:
        item: The item to fetch the type module of.

    Returns:
        The module of the item type.
    """
    return get_module(type(item))  # type: ignore

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
def get_type_name(item: Any) -> str:
    """Fetches the `__name__` of the `item` type.

    Arguments:
        item: The item to fetch the type name of.

    Returns:
        The name of the item type.
    """
    return get_name(type(item))  # type: ignore

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 Moduled protocol.

Source code in named/core.py
154
155
156
157
158
159
160
161
162
163
def is_moduled(item: Any) -> TypeIs[Moduled]:
    """Checks if the `item` implements the [`Moduled`][named.core.Moduled] protocol.

    Arguments:
        item: The item to check.

    Returns:
        Whether the item implements the [`Moduled`][named.core.Moduled] protocol.
    """
    return has_attribute(item, MODULE)

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 Named protocol.

Source code in named/core.py
83
84
85
86
87
88
89
90
91
92
def is_named(item: Any) -> TypeIs[Named]:
    """Checks if the `item` implements the [`Named`][named.core.Named] protocol.

    Arguments:
        item: The item to check.

    Returns:
        Whether the item implements the [`Named`][named.core.Named] protocol.
    """
    return has_attribute(item, NAME)

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
def set_module(item: Moduled, module: str) -> None:
    """Sets the `__module__` of the [`Moduled`][named.core.Moduled] `item`.

    Arguments:
        item: The item to set the module of.
        module: The module to set on the item.
    """
    item.__module__ = module

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
def set_name(item: Named, name: str) -> None:
    """Sets the `__name__` of the [`Named`][named.core.Named] `item`.

    Arguments:
        item: The item to set the name of.
        name: The name to set on the item.
    """
    item.__name__ = name

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
def set_type_module(item: Any, module: str) -> None:
    """Sets the `__module__` of the `item` type.

    Arguments:
        item: The item to set the type module of.
        module: The module to set on the item type.
    """
    set_module(type(item), module)  # type: ignore

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
def set_type_name(item: Any, name: str) -> None:
    """Sets the `__name__` of the `item` type.

    Arguments:
        item: The item to set the type name of.
        name: The name to set on the item type.
    """
    set_name(type(item), name)  # type: ignore