Skip to content

Models¤

Griffe stores information extracted from Python source code into data models.

These models represent trees of objects, starting with modules, and containing classes, functions, attributes, and type aliases.

Modules can have submodules, classes, functions, attributes, and type aliases. Classes can have nested classes, methods, attributes, and type aliases. Functions and attributes do not have any members.

Indirections to objects declared in other modules are represented as "aliases". An alias therefore represents an imported object, and behaves almost exactly like the object it points to: it is a light wrapper around the object, with special methods and properties that allow to access the target's data transparently.

The 6 models:

Model kind enumeration¤

Kind ¤

Bases: str, Enum

Enumeration of the different object kinds.

Attributes:

ALIAS class-attribute instance-attribute ¤

ALIAS = 'alias'

Aliases (imported objects).

ATTRIBUTE class-attribute instance-attribute ¤

ATTRIBUTE = 'attribute'

Attributes and properties.

CLASS class-attribute instance-attribute ¤

CLASS = 'class'

Classes.

FUNCTION class-attribute instance-attribute ¤

FUNCTION = 'function'

Functions and methods.

MODULE class-attribute instance-attribute ¤

MODULE = 'module'

Modules.

TYPE_ALIAS class-attribute instance-attribute ¤

TYPE_ALIAS = 'type alias'

Type aliases.

Models base classes¤

GetMembersMixin ¤

Mixin class to share methods for accessing members.

Methods:

  • get_member

    Get a member with its name or path.

  • __getitem__

    Same as get_member, with the item syntax [].

__getitem__ ¤

__getitem__(key: str | Sequence[str]) -> Any

Get a member with its name or path.

This method is part of the consumer API: do not use when producing Griffe trees!

Members will be looked up in both declared members and inherited ones, triggering computation of the latter.

Parameters:

Examples:

>>> foo = griffe_object["foo"]
>>> bar = griffe_object["path.to.bar"]
>>> qux = griffe_object[("path", "to", "qux")]

get_member ¤

get_member(key: str | Sequence[str]) -> Any

Get a member with its name or path.

This method is part of the producer API: you can use it safely while building Griffe trees (for example in Griffe extensions).

Members will be looked up in declared members only, not inherited ones.

Parameters:

Examples:

>>> foo = griffe_object["foo"]
>>> bar = griffe_object["path.to.bar"]
>>> bar = griffe_object[("path", "to", "bar")]

SetMembersMixin ¤

Mixin class to share methods for setting members.

Methods:

  • set_member

    Set a member with its name or path.

  • __setitem__

    Same as set_member, with the item syntax [].

__setitem__ ¤

__setitem__(
    key: str | Sequence[str], value: Object | Alias
) -> None

Set a member with its name or path.

This method is part of the consumer API: do not use when producing Griffe trees!

Parameters:

Examples:

>>> griffe_object["foo"] = foo
>>> griffe_object["path.to.bar"] = bar
>>> griffe_object[("path", "to", "qux")] = qux

set_member ¤

set_member(
    key: str | Sequence[str], value: Object | Alias
) -> None

Set a member with its name or path.

This method is part of the producer API: you can use it safely while building Griffe trees (for example in Griffe extensions).

Parameters:

Examples:

>>> griffe_object.set_member("foo", foo)
>>> griffe_object.set_member("path.to.bar", bar)
>>> griffe_object.set_member(("path", "to", "qux"), qux)

DelMembersMixin ¤

Mixin class to share methods for deleting members.

Methods:

  • del_member

    Delete a member with its name or path.

  • __delitem__

    Same as del_member, with the item syntax [].

__delitem__ ¤

__delitem__(key: str | Sequence[str]) -> None

Delete a member with its name or path.

This method is part of the consumer API: do not use when producing Griffe trees!

Members will be looked up in both declared members and inherited ones, triggering computation of the latter.

Parameters:

Examples:

>>> del griffe_object["foo"]
>>> del griffe_object["path.to.bar"]
>>> del griffe_object[("path", "to", "qux")]

del_member ¤

del_member(key: str | Sequence[str]) -> None

Delete a member with its name or path.

This method is part of the producer API: you can use it safely while building Griffe trees (for example in Griffe extensions).

Members will be looked up in declared members only, not inherited ones.

Parameters:

Examples:

>>> griffe_object.del_member("foo")
>>> griffe_object.del_member("path.to.bar")
>>> griffe_object.del_member(("path", "to", "qux"))

SerializationMixin ¤

Mixin class to share methods for de/serializing objects.

Methods:

  • as_json

    Return this object's data as a JSON string.

  • from_json

    Create an instance of this class from a JSON string.

as_json ¤

as_json(*, full: bool = False, **kwargs: Any) -> str

Return this object's data as a JSON string.

Parameters:

  • full ¤

    (bool, default: False ) –

    Whether to return full info, or just base info.

  • **kwargs ¤

    (Any, default: {} ) –

    Additional serialization options passed to encoder.

Returns:

  • str

    A JSON string.

from_json classmethod ¤

from_json(json_string: str, **kwargs: Any) -> _ObjType

Create an instance of this class from a JSON string.

Parameters:

  • json_string ¤

    (str) –

    JSON to decode into Object.

  • **kwargs ¤

    (Any, default: {} ) –

    Additional options passed to decoder.

Returns:

  • _ObjType

    An Object instance.

Raises:

  • TypeError

    When the json_string does not represent and object of the class from which this classmethod has been called.

ObjectAliasMixin ¤

Bases: GetMembersMixin, SetMembersMixin, DelMembersMixin, SerializationMixin

Mixin class to share methods that appear both in objects and aliases, unchanged.

Attributes:

Methods:

  • __delitem__

    Delete a member with its name or path.

  • __getitem__

    Get a member with its name or path.

  • __setitem__

    Set a member with its name or path.

  • as_json

    Return this object's data as a JSON string.

  • del_member

    Delete a member with its name or path.

  • from_json

    Create an instance of this class from a JSON string.

  • get_member

    Get a member with its name or path.

  • set_member

    Set a member with its name or path.

all_members property ¤

all_members: dict[str, Object | Alias]

All members (declared and inherited).

This method is part of the consumer API: do not use when producing Griffe trees!

attributes property ¤

attributes: dict[str, Attribute]

The attribute members.

This method is part of the consumer API: do not use when producing Griffe trees!

classes property ¤

classes: dict[str, Class]

The class members.

This method is part of the consumer API: do not use when producing Griffe trees!

functions property ¤

functions: dict[str, Function]

The function members.

This method is part of the consumer API: do not use when producing Griffe trees!

is_class_private property ¤

is_class_private: bool

Whether this object/alias is class-private (starts with __ and is a class member).

is_deprecated property ¤

is_deprecated: bool

Whether this object is deprecated.

is_exported property ¤

is_exported: bool

Whether this object/alias is exported (listed in __all__).

is_generic property ¤

is_generic: bool

Whether this object is generic.

is_imported property ¤

is_imported: bool

Whether this object/alias was imported from another module.

is_private property ¤

is_private: bool

Whether this object/alias is private (starts with _) but not special.

is_public property ¤

is_public: bool

Whether this object is considered public.

In modules, developers can mark objects as public thanks to the __all__ variable. In classes however, there is no convention or standard to do so.

Therefore, to decide whether an object is public, we follow this algorithm:

  • If the object's public attribute is set (boolean), return its value.
  • If the object is listed in its parent's (a module) __all__ attribute, it is public.
  • If the parent (module) defines __all__ and the object is not listed in, it is private.
  • If the object has a private name, it is private.
  • If the object was imported from another module, it is private.
  • Otherwise, the object is public.

is_special property ¤

is_special: bool

Whether this object/alias is special ("dunder" attribute/method, starts and end with __).

is_wildcard_exposed property ¤

is_wildcard_exposed: bool

Whether this object/alias is exposed to wildcard imports.

To be exposed to wildcard imports, an object/alias must:

  • be available at runtime
  • have a module as parent
  • be listed in __all__ if __all__ is defined
  • or not be private (having a name starting with an underscore)

Special case for Griffe trees: a submodule is only exposed if its parent imports it.

Returns:

  • bool

    True or False.

modules property ¤

modules: dict[str, Module]

The module members.

This method is part of the consumer API: do not use when producing Griffe trees!

type_aliases property ¤

type_aliases: dict[str, TypeAlias]

The type alias members.

This method is part of the consumer API: do not use when producing Griffe trees!

__delitem__ ¤

__delitem__(key: str | Sequence[str]) -> None

Delete a member with its name or path.

This method is part of the consumer API: do not use when producing Griffe trees!

Members will be looked up in both declared members and inherited ones, triggering computation of the latter.

Parameters:

Examples:

>>> del griffe_object["foo"]
>>> del griffe_object["path.to.bar"]
>>> del griffe_object[("path", "to", "qux")]

__getitem__ ¤

__getitem__(key: str | Sequence[str]) -> Any

Get a member with its name or path.

This method is part of the consumer API: do not use when producing Griffe trees!

Members will be looked up in both declared members and inherited ones, triggering computation of the latter.

Parameters:

Examples:

>>> foo = griffe_object["foo"]
>>> bar = griffe_object["path.to.bar"]
>>> qux = griffe_object[("path", "to", "qux")]

__setitem__ ¤

__setitem__(
    key: str | Sequence[str], value: Object | Alias
) -> None

Set a member with its name or path.

This method is part of the consumer API: do not use when producing Griffe trees!

Parameters:

Examples:

>>> griffe_object["foo"] = foo
>>> griffe_object["path.to.bar"] = bar
>>> griffe_object[("path", "to", "qux")] = qux

as_json ¤

as_json(*, full: bool = False, **kwargs: Any) -> str

Return this object's data as a JSON string.

Parameters:

  • full ¤

    (bool, default: False ) –

    Whether to return full info, or just base info.

  • **kwargs ¤

    (Any, default: {} ) –

    Additional serialization options passed to encoder.

Returns:

  • str

    A JSON string.

del_member ¤

del_member(key: str | Sequence[str]) -> None

Delete a member with its name or path.

This method is part of the producer API: you can use it safely while building Griffe trees (for example in Griffe extensions).

Members will be looked up in declared members only, not inherited ones.

Parameters:

Examples:

>>> griffe_object.del_member("foo")
>>> griffe_object.del_member("path.to.bar")
>>> griffe_object.del_member(("path", "to", "qux"))

from_json classmethod ¤

from_json(json_string: str, **kwargs: Any) -> _ObjType

Create an instance of this class from a JSON string.

Parameters:

  • json_string ¤

    (str) –

    JSON to decode into Object.

  • **kwargs ¤

    (Any, default: {} ) –

    Additional options passed to decoder.

Returns:

  • _ObjType

    An Object instance.

Raises:

  • TypeError

    When the json_string does not represent and object of the class from which this classmethod has been called.

get_member ¤

get_member(key: str | Sequence[str]) -> Any

Get a member with its name or path.

This method is part of the producer API: you can use it safely while building Griffe trees (for example in Griffe extensions).

Members will be looked up in declared members only, not inherited ones.

Parameters:

Examples:

>>> foo = griffe_object["foo"]
>>> bar = griffe_object["path.to.bar"]
>>> bar = griffe_object[("path", "to", "bar")]

set_member ¤

set_member(
    key: str | Sequence[str], value: Object | Alias
) -> None

Set a member with its name or path.

This method is part of the producer API: you can use it safely while building Griffe trees (for example in Griffe extensions).

Parameters:

Examples:

>>> griffe_object.set_member("foo", foo)
>>> griffe_object.set_member("path.to.bar", bar)
>>> griffe_object.set_member(("path", "to", "qux"), qux)

Object ¤

Object(
    name: str,
    *,
    lineno: int | None = None,
    endlineno: int | None = None,
    runtime: bool = True,
    docstring: Docstring | None = None,
    type_parameters: TypeParameters | None = None,
    parent: Module | Class | None = None,
    lines_collection: LinesCollection | None = None,
    modules_collection: ModulesCollection | None = None,
)

Bases: ObjectAliasMixin

An abstract class representing a Python object.

Parameters:

  • name ¤

    (str) –

    The object name, as declared in the code.

  • lineno ¤

    (int | None, default: None ) –

    The object starting line, or None for modules. Lines start at 1.

  • endlineno ¤

    (int | None, default: None ) –

    The object ending line (inclusive), or None for modules.

  • runtime ¤

    (bool, default: True ) –

    Whether this object is present at runtime or not.

  • docstring ¤

    (Docstring | None, default: None ) –

    The object docstring.

  • type_parameters ¤

    (TypeParameters | None, default: None ) –

    The object type parameters, if any.

  • parent ¤

    (Module | Class | None, default: None ) –

    The object parent.

  • lines_collection ¤

    (LinesCollection | None, default: None ) –

    A collection of source code lines.

  • modules_collection ¤

    (ModulesCollection | None, default: None ) –

    A collection of modules.

Methods:

  • __bool__

    An object is always true-ish.

  • __delitem__

    Delete a member with its name or path.

  • __getitem__

    Get a member with its name or path.

  • __len__

    The number of members in this object, recursively.

  • __setitem__

    Set a member with its name or path.

  • as_dict

    Return this object's data as a dictionary.

  • as_json

    Return this object's data as a JSON string.

  • del_member

    Delete a member with its name or path.

  • filter_members

    Filter and return members based on predicates.

  • from_json

    Create an instance of this class from a JSON string.

  • get_member

    Get a member with its name or path.

  • has_labels

    Tell if this object has all the given labels.

  • is_kind

    Tell if this object is of the given kind.

  • resolve

    Resolve a name within this object's and parents' scope.

  • set_member

    Set a member with its name or path.

Attributes:

aliases instance-attribute ¤

aliases: dict[str, Alias] = {}

The aliases pointing to this object.

all_members property ¤

all_members: dict[str, Object | Alias]

All members (declared and inherited).

This method is part of the consumer API: do not use when producing Griffe trees!

attributes property ¤

attributes: dict[str, Attribute]

The attribute members.

This method is part of the consumer API: do not use when producing Griffe trees!

canonical_path property ¤

canonical_path: str

The full dotted path of this object.

The canonical path is the path where the object was defined (not imported).

See also: path.

classes property ¤

classes: dict[str, Class]

The class members.

This method is part of the consumer API: do not use when producing Griffe trees!

deprecated instance-attribute ¤

deprecated: bool | str | None = None

Whether this object is deprecated (boolean or deprecation message).

endlineno instance-attribute ¤

endlineno: int | None = endlineno

exports instance-attribute ¤

exports: list[str | ExprName] | None = None

The names of the objects exported by this (module) object through the __all__ variable.

Exports can contain string (object names) or resolvable names, like other lists of exports coming from submodules:

from .submodule import __all__ as submodule_all

__all__ = ["hello", *submodule_all]

Exports get expanded by the loader before it expands wildcards and resolves aliases.

See also: GriffeLoader.expand_exports.

extra instance-attribute ¤

Namespaced dictionaries storing extra metadata for this object, used by extensions.

filepath property ¤

filepath: Path | list[Path]

The file path (or directory list for namespace packages) where this object was defined.

See also: relative_filepath, relative_package_filepath.

Examples:

>>> import griffe
>>> markdown = griffe.load("markdown")
>>> markdown.filepath
PosixPath('~/project/.venv/lib/python3.11/site-packages/markdown/__init__.py')

functions property ¤

functions: dict[str, Function]

The function members.

This method is part of the consumer API: do not use when producing Griffe trees!

has_docstrings property ¤

has_docstrings: bool

Whether this object or any of its members has a docstring (empty or not).

Inherited members are not considered. Imported members are not considered, unless they are also public.

See also: docstring, has_docstring.

imports instance-attribute ¤

imports: dict[str, str] = {}

The other objects imported by this object.

Keys are the names within the object (from ... import ... as AS_NAME), while the values are the actual names of the objects (from ... import REAL_NAME as ...).

inherited class-attribute instance-attribute ¤

inherited: bool = False

Always false for objects.

Only aliases can be marked as inherited.

inherited_members property ¤

inherited_members: dict[str, Alias]

Members that are inherited from base classes.

This method is part of the consumer API: do not use when producing Griffe trees!

See also: members.

is_class_private property ¤

is_class_private: bool

Whether this object/alias is class-private (starts with __ and is a class member).

is_collection class-attribute instance-attribute ¤

is_collection: bool = False

Always false for objects.

is_deprecated property ¤

is_deprecated: bool

Whether this object is deprecated.

is_exported property ¤

is_exported: bool

Whether this object/alias is exported (listed in __all__).

is_generic property ¤

is_generic: bool

Whether this object is generic.

is_imported property ¤

is_imported: bool

Whether this object/alias was imported from another module.

is_init_module property ¤

is_init_module: bool

is_namespace_package property ¤

is_namespace_package: bool

is_private property ¤

is_private: bool

Whether this object/alias is private (starts with _) but not special.

is_public property ¤

is_public: bool

Whether this object is considered public.

In modules, developers can mark objects as public thanks to the __all__ variable. In classes however, there is no convention or standard to do so.

Therefore, to decide whether an object is public, we follow this algorithm:

  • If the object's public attribute is set (boolean), return its value.
  • If the object is listed in its parent's (a module) __all__ attribute, it is public.
  • If the parent (module) defines __all__ and the object is not listed in, it is private.
  • If the object has a private name, it is private.
  • If the object was imported from another module, it is private.
  • Otherwise, the object is public.

is_special property ¤

is_special: bool

Whether this object/alias is special ("dunder" attribute/method, starts and end with __).

is_subpackage property ¤

is_subpackage: bool

is_wildcard_exposed property ¤

is_wildcard_exposed: bool

Whether this object/alias is exposed to wildcard imports.

To be exposed to wildcard imports, an object/alias must:

  • be available at runtime
  • have a module as parent
  • be listed in __all__ if __all__ is defined
  • or not be private (having a name starting with an underscore)

Special case for Griffe trees: a submodule is only exposed if its parent imports it.

Returns:

  • bool

    True or False.

kind instance-attribute ¤

kind: Kind

The object kind.

labels instance-attribute ¤

labels: set[str] = set()

lines_collection property ¤

lines_collection: LinesCollection

The lines collection attached to this object or its parents.

See also: lines, source.

Raises:

  • ValueError

    When no modules collection can be found in the object or its parents.

module property ¤

module: Module

The parent module of this object.

See also: package.

Examples:

>>> import griffe
>>> markdown = griffe.load("markdown")
>>> markdown["core.Markdown.references"].module
Module(PosixPath('~/project/.venv/lib/python3.11/site-packages/markdown/core.py'))
>>> # The `module` of a module is itself.
>>> markdown["core"].module
Module(PosixPath('~/project/.venv/lib/python3.11/site-packages/markdown/core.py'))

Raises:

  • ValueError

    When the object is not a module and does not have a parent.

modules property ¤

modules: dict[str, Module]

The module members.

This method is part of the consumer API: do not use when producing Griffe trees!

modules_collection property ¤

modules_collection: ModulesCollection

The modules collection attached to this object or its parents.

Raises:

  • ValueError

    When no modules collection can be found in the object or its parents.

name instance-attribute ¤

name: str = name

The object name.

package property ¤

package: Module

The absolute top module (the package) of this object.

See also: module.

Examples:

>>> import griffe
>>> markdown = griffe.load("markdown")
>>> markdown["core.Markdown.references"].package
Module(PosixPath('~/project/.venv/lib/python3.11/site-packages/markdown/__init__.py'))

parent instance-attribute ¤

parent: Module | Class | None = parent

The parent of the object (none if top module).

path property ¤

path: str

The dotted path of this object.

On regular objects (not aliases), the path is the canonical path.

See also: canonical_path.

Examples:

>>> import griffe
>>> markdown = griffe.load("markdown")
>>> markdown["core.Markdown.references"].path
'markdown.core.Markdown.references'

public instance-attribute ¤

public: bool | None = None

Whether this object is public.

relative_filepath property ¤

relative_filepath: Path

The file path where this object was defined, relative to the current working directory.

If this object's file path is not relative to the current working directory, return its absolute path.

See also: filepath, relative_package_filepath.

Raises:

  • ValueError

    When the relative path could not be computed.

relative_package_filepath property ¤

relative_package_filepath: Path

runtime instance-attribute ¤

runtime: bool = runtime

Whether this object is available at runtime.

Typically, type-guarded objects (under an if TYPE_CHECKING condition) are not available at runtime.

type_aliases property ¤

type_aliases: dict[str, TypeAlias]

The type alias members.

This method is part of the consumer API: do not use when producing Griffe trees!

type_parameters instance-attribute ¤

type_parameters: TypeParameters = (
    type_parameters or TypeParameters()
)

The object type parameters.

__bool__ ¤

__bool__() -> bool

An object is always true-ish.

__delitem__ ¤

__delitem__(key: str | Sequence[str]) -> None

Delete a member with its name or path.

This method is part of the consumer API: do not use when producing Griffe trees!

Members will be looked up in both declared members and inherited ones, triggering computation of the latter.

Parameters:

Examples:

>>> del griffe_object["foo"]
>>> del griffe_object["path.to.bar"]
>>> del griffe_object[("path", "to", "qux")]

__getitem__ ¤

__getitem__(key: str | Sequence[str]) -> Any

Get a member with its name or path.

This method is part of the consumer API: do not use when producing Griffe trees!

Members will be looked up in both declared members and inherited ones, triggering computation of the latter.

Parameters:

Examples:

>>> foo = griffe_object["foo"]
>>> bar = griffe_object["path.to.bar"]
>>> qux = griffe_object[("path", "to", "qux")]

__len__ ¤

__len__() -> int

The number of members in this object, recursively.

__setitem__ ¤

__setitem__(
    key: str | Sequence[str], value: Object | Alias
) -> None

Set a member with its name or path.

This method is part of the consumer API: do not use when producing Griffe trees!

Parameters:

Examples:

>>> griffe_object["foo"] = foo
>>> griffe_object["path.to.bar"] = bar
>>> griffe_object[("path", "to", "qux")] = qux

as_dict ¤

as_dict(
    *, full: bool = False, **kwargs: Any
) -> dict[str, Any]

Return this object's data as a dictionary.

See also: as_json.

Parameters:

  • full ¤

    (bool, default: False ) –

    Whether to return full info, or just base info.

  • **kwargs ¤

    (Any, default: {} ) –

    Additional serialization options.

Returns:

as_json ¤

as_json(*, full: bool = False, **kwargs: Any) -> str

Return this object's data as a JSON string.

Parameters:

  • full ¤

    (bool, default: False ) –

    Whether to return full info, or just base info.

  • **kwargs ¤

    (Any, default: {} ) –

    Additional serialization options passed to encoder.

Returns:

  • str

    A JSON string.

del_member ¤

del_member(key: str | Sequence[str]) -> None

Delete a member with its name or path.

This method is part of the producer API: you can use it safely while building Griffe trees (for example in Griffe extensions).

Members will be looked up in declared members only, not inherited ones.

Parameters:

Examples:

>>> griffe_object.del_member("foo")
>>> griffe_object.del_member("path.to.bar")
>>> griffe_object.del_member(("path", "to", "qux"))

filter_members ¤

filter_members(
    *predicates: Callable[[Object | Alias], bool],
) -> dict[str, Object | Alias]

Filter and return members based on predicates.

See also: members.

Parameters:

  • *predicates ¤

    (Callable[[Object | Alias], bool], default: () ) –

    A list of predicates, i.e. callables accepting a member as argument and returning a boolean.

Returns:

from_json classmethod ¤

from_json(json_string: str, **kwargs: Any) -> _ObjType

Create an instance of this class from a JSON string.

Parameters:

  • json_string ¤

    (str) –

    JSON to decode into Object.

  • **kwargs ¤

    (Any, default: {} ) –

    Additional options passed to decoder.

Returns:

  • _ObjType

    An Object instance.

Raises:

  • TypeError

    When the json_string does not represent and object of the class from which this classmethod has been called.

get_member ¤

get_member(key: str | Sequence[str]) -> Any

Get a member with its name or path.

This method is part of the producer API: you can use it safely while building Griffe trees (for example in Griffe extensions).

Members will be looked up in declared members only, not inherited ones.

Parameters:

Examples:

>>> foo = griffe_object["foo"]
>>> bar = griffe_object["path.to.bar"]
>>> bar = griffe_object[("path", "to", "bar")]

has_labels ¤

has_labels(*labels: str) -> bool

Tell if this object has all the given labels.

See also: labels.

Parameters:

  • *labels ¤

    (str, default: () ) –

    Labels that must be present.

Returns:

  • bool

    True or False.

resolve ¤

resolve(name: str) -> str

Resolve a name within this object's and parents' scope.

Parameters:

  • name ¤

    (str) –

    The name to resolve.

Raises:

Returns:

  • str

    The resolved name.

set_member ¤

set_member(
    key: str | Sequence[str], value: Object | Alias
) -> None

Set a member with its name or path.

This method is part of the producer API: you can use it safely while building Griffe trees (for example in Griffe extensions).

Parameters:

Examples:

>>> griffe_object.set_member("foo", foo)
>>> griffe_object.set_member("path.to.bar", bar)
>>> griffe_object.set_member(("path", "to", "qux"), qux)

Models type parameter¤

TypeParameters ¤

TypeParameters(*type_parameters: TypeParameter)

This class is a container for type parameters.

It allows to get type parameters using their position (index) or their name:

>>> type_parameters = TypeParameters(TypeParameter("hello"), kind=TypeParameterKind.type_var)
>>> type_parameters[0] is type_parameters["hello"]
True

Parameters:

  • *type_parameters ¤

    (TypeParameter, default: () ) –

    The initial type parameters to add to the container.

Methods:

  • __contains__

    Whether a type parameter with the given name is present.

  • __delitem__

    Delete a type parameter by index or name.

  • __getitem__

    Get a type parameter by index or name.

  • __iter__

    Iterate over the type parameters, in order.

  • __len__

    The number of type parameters.

  • __setitem__

    Set a type parameter by index or name.

  • add

    Add a type parameter to the container.

__contains__ ¤

__contains__(type_param_name: str)

Whether a type parameter with the given name is present.

__delitem__ ¤

__delitem__(name_or_index: int | str) -> None

Delete a type parameter by index or name.

__getitem__ ¤

__getitem__(name_or_index: int | str) -> TypeParameter

Get a type parameter by index or name.

__iter__ ¤

__iter__()

Iterate over the type parameters, in order.

__len__ ¤

__len__()

The number of type parameters.

__setitem__ ¤

__setitem__(
    name_or_index: int | str, type_parameter: TypeParameter
) -> None

Set a type parameter by index or name.

add ¤

Add a type parameter to the container.

Parameters:

Raises:

  • ValueError

    When a type parameter with the same name is already present.

TypeParameter ¤

TypeParameter(
    name: str,
    *,
    kind: TypeParameterKind,
    bound: str | Expr | None = None,
    constraints: Sequence[str | Expr] | None = None,
    default: str | Expr | None = None,
)

This class represents a type parameter.

Parameters:

  • name ¤

    (str) –

    The type parameter name, without leading stars (* or **).

  • kind ¤

    (TypeParameterKind) –

    The type parameter kind.

  • bound ¤

    (str | Expr | None, default: None ) –

    The type parameter bound, if any. Mutually exclusive with constraints.

  • constraints ¤

    (Sequence[str | Expr] | None, default: None ) –

    The type parameter constraints, if any. Mutually exclusive with bound.

  • default ¤

    (str | Expr | None, default: None ) –

    The type parameter default, if any.

Raises:

  • ValueError

    When more than one of bound and constraints is set.

Methods:

  • as_dict

    Return this type parameter's data as a dictionary.

Attributes:

annotation instance-attribute ¤

annotation: str | Expr | None

The type parameter bound or constraints.

bound property writable ¤

bound: str | Expr | None

The type parameter bound.

constraints property writable ¤

constraints: tuple[str | Expr, ...] | None

The type parameter constraints.

default instance-attribute ¤

default: str | Expr | None = default

The type parameter default value.

kind instance-attribute ¤

The type parameter kind.

name instance-attribute ¤

name: str = name

The type parameter name.

as_dict ¤

as_dict(**kwargs: Any) -> dict[str, Any]

Return this type parameter's data as a dictionary.

Parameters:

  • **kwargs ¤

    (Any, default: {} ) –

    Additional serialization options.

Returns:

TypeParameterKind ¤

Bases: str, Enum

Enumeration of the different type parameter kinds.

Attributes:

param_spec class-attribute instance-attribute ¤

param_spec = 'param-spec'

Parameter specification variable.

type_var class-attribute instance-attribute ¤

type_var = 'type-var'

Type variable.

type_var_tuple class-attribute instance-attribute ¤

type_var_tuple = 'type-var-tuple'

Type variable tuple.

Was this page helpful?