Attribute ¤
Attribute(
*args: Any,
value: str | Expr | None = None,
annotation: str | Expr | None = None,
**kwargs: Any,
)
Bases: Object
flowchart TD
griffe.Attribute[Attribute]
_griffe.models.Object[Object]
_griffe.mixins.ObjectAliasMixin[ObjectAliasMixin]
_griffe.mixins.GetMembersMixin[GetMembersMixin]
_griffe.mixins.SetMembersMixin[SetMembersMixin]
_griffe.mixins.DelMembersMixin[DelMembersMixin]
_griffe.mixins.SerializationMixin[SerializationMixin]
_griffe.models.Object --> griffe.Attribute
_griffe.mixins.ObjectAliasMixin --> _griffe.models.Object
_griffe.mixins.GetMembersMixin --> _griffe.mixins.ObjectAliasMixin
_griffe.mixins.SetMembersMixin --> _griffe.mixins.ObjectAliasMixin
_griffe.mixins.DelMembersMixin --> _griffe.mixins.ObjectAliasMixin
_griffe.mixins.SerializationMixin --> _griffe.mixins.ObjectAliasMixin
click griffe.Attribute href "" "griffe.Attribute"
click _griffe.models.Object href "" "_griffe.models.Object"
click _griffe.mixins.ObjectAliasMixin href "" "_griffe.mixins.ObjectAliasMixin"
click _griffe.mixins.GetMembersMixin href "" "_griffe.mixins.GetMembersMixin"
click _griffe.mixins.SetMembersMixin href "" "_griffe.mixins.SetMembersMixin"
click _griffe.mixins.DelMembersMixin href "" "_griffe.mixins.DelMembersMixin"
click _griffe.mixins.SerializationMixin href "" "_griffe.mixins.SerializationMixin"
The class representing a Python module/class/instance attribute.
Parameters:
-
*args
¤Any
, default:()
) –See
griffe.Object
. -
value
¤str | Expr | None
, default:None
) –The attribute value, if any.
-
annotation
¤str | Expr | None
, default:None
) –The attribute annotation, if any.
-
**kwargs
¤Any
, default:{}
) –See
griffe.Object
.
- Guide User guide
- Manipulating APIs
- How-to Supporting custom decorators
- Reference
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 function'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
(dict[str, Alias]
) –The aliases pointing to this object.
-
all_members
(dict[str, Object | Alias]
) –All members (declared and inherited).
-
annotation
(str | Expr | None
) –The attribute type annotation.
-
attributes
(dict[str, Attribute]
) –The attribute members.
-
canonical_path
(str
) –The full dotted path of this object.
-
classes
(dict[str, Class]
) –The class members.
-
deleter
(Function | None
) –The deleter linked to this property.
-
deprecated
(bool | str | None
) –Whether this object is deprecated (boolean or deprecation message).
-
docstring
(Docstring | None
) –The object docstring.
-
endlineno
(int | None
) –The ending line number of the object.
-
exports
(list[str | ExprName] | None
) –The names of the objects exported by this (module) object through the
__all__
variable. -
extra
(dict[str, dict[str, Any]]
) –Namespaced dictionaries storing extra metadata for this object, used by extensions.
-
filepath
(Path | list[Path]
) –The file path (or directory list for namespace packages) where this object was defined.
-
functions
(dict[str, Function]
) –The function members.
-
has_docstring
(bool
) –Whether this object has a docstring (empty or not).
-
has_docstrings
(bool
) –Whether this object or any of its members has a docstring (empty or not).
-
imports
(dict[str, str]
) –The other objects imported by this object.
-
inherited
(bool
) –Always false for objects.
-
inherited_members
(dict[str, Alias]
) –Members that are inherited from base classes.
-
is_alias
(bool
) –Always false for objects.
-
is_attribute
(bool
) –Whether this object is an attribute.
-
is_class
(bool
) –Whether this object is a class.
-
is_class_private
(bool
) –Whether this object/alias is class-private (starts with
__
and is a class member). -
is_collection
(bool
) –Always false for objects.
-
is_deprecated
(bool
) –Whether this object is deprecated.
-
is_exported
(bool
) –Whether this object/alias is exported (listed in
__all__
). -
is_function
(bool
) –Whether this object is a function.
-
is_imported
(bool
) –Whether this object/alias was imported from another module.
-
is_init_module
(bool
) –Whether this object is an
__init__.py
module. -
is_module
(bool
) –Whether this object is a module.
-
is_namespace_package
(bool
) –Whether this object is a namespace package (top folder, no
__init__.py
). -
is_namespace_subpackage
(bool
) –Whether this object is a namespace subpackage.
-
is_package
(bool
) –Whether this object is a package (top module).
-
is_private
(bool
) –Whether this object/alias is private (starts with
_
) but not special. -
is_public
(bool
) –Whether this object is considered public.
-
is_special
(bool
) –Whether this object/alias is special ("dunder" attribute/method, starts and end with
__
). -
is_subpackage
(bool
) –Whether this object is a subpackage.
-
is_wildcard_exposed
(bool
) –Whether this object/alias is exposed to wildcard imports.
-
kind
–The object kind.
-
labels
(set[str]
) –The object labels (
property
,dataclass
, etc.). -
lineno
(int | None
) –The starting line number of the object.
-
lines
(list[str]
) –The lines containing the source of this object.
-
lines_collection
(LinesCollection
) –The lines collection attached to this object or its parents.
-
members
(dict[str, Object | Alias]
) –The object members (modules, classes, functions, attributes).
-
module
(Module
) –The parent module of this object.
-
modules
(dict[str, Module]
) –The module members.
-
modules_collection
(ModulesCollection
) –The modules collection attached to this object or its parents.
-
name
(str
) –The object name.
-
package
(Module
) –The absolute top module (the package) of this object.
-
parent
(Module | Class | None
) –The parent of the object (none if top module).
-
path
(str
) –The dotted path of this object.
-
public
(bool | None
) –Whether this object is public.
-
relative_filepath
(Path
) –The file path where this object was defined, relative to the current working directory.
-
relative_package_filepath
(Path
) –The file path where this object was defined, relative to the top module path.
-
runtime
(bool
) –Whether this object is available at runtime.
-
setter
(Function | None
) –The setter linked to this property.
-
source
(str
) –The source code of this object.
-
value
(str | Expr | None
) –The attribute value.
aliases instance-attribute
¤
The aliases pointing to this object.
- Guide User guide Manipulating APIs Navigating APIs
all_members property
¤
All members (declared and inherited).
This method is part of the consumer API: do not use when producing Griffe trees!
- Guide User guide Manipulating APIs Navigating APIs Moving down: members Inherited members
annotation instance-attribute
¤
annotation: str | Expr | None = annotation
The attribute type annotation.
- Guide User guide Manipulating APIs Navigating APIs Model-specific fields Attributes
attributes property
¤
The attribute members.
This method is part of the consumer API: do not use when producing Griffe trees!
- Guide User guide Manipulating APIs Navigating APIs Moving down: members Inherited members
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
.
- Guide User guide Manipulating APIs Navigating APIs Object location
- Reference Python API
classes property
¤
The class members.
This method is part of the consumer API: do not use when producing Griffe trees!
- Guide User guide Manipulating APIs Navigating APIs Moving down: members Inherited members
deleter instance-attribute
¤
deleter: Function | None = None
The deleter linked to this property.
- Guide User guide Manipulating APIs Navigating APIs Model-specific fields Attributes
deprecated instance-attribute
¤
Whether this object is deprecated (boolean or deprecation message).
- Extensions Official griffe-warnings-deprecated
- Guide User guide Manipulating APIs Navigating APIs Object visibility
docstring instance-attribute
¤
The object docstring.
See also: has_docstring
, has_docstrings
.
endlineno instance-attribute
¤
exports instance-attribute
¤
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
.
- Guide User guide Manipulating APIs Navigating APIs Imports/exports
- Reference Python API Loaders
GriffeLoader
expand_exports
extra instance-attribute
¤
Namespaced dictionaries storing extra metadata for this object, used by extensions.
filepath property
¤
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')
- Guide User guide Manipulating APIs Navigating APIs Object location Source
- Reference Python API
functions property
¤
The function members.
This method is part of the consumer API: do not use when producing Griffe trees!
- Guide User guide Manipulating APIs Navigating APIs Moving down: members Inherited members
has_docstring property
¤
has_docstring: bool
Whether this object has a docstring (empty or not).
See also: docstring
, has_docstrings
.
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
¤
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 ...
).
- Guide User guide Manipulating APIs Navigating APIs Imports/exports
inherited class-attribute
instance-attribute
¤
inherited: bool = False
Always false for objects.
Only aliases can be marked as inherited.
inherited_members property
¤
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_alias class-attribute
instance-attribute
¤
is_alias: bool = False
Always false for objects.
- Guide User guide Manipulating APIs Navigating APIs
- Reference Python API
is_attribute property
¤
is_attribute: bool
Whether this object is an attribute.
See also: is_module
. is_class
, is_function
, is_alias
, is_kind
.
- Guide User guide Manipulating APIs Navigating APIs Object kind
- Reference Python API
is_class property
¤
is_class: bool
Whether this object is a class.
See also: is_module
. is_function
, is_attribute
, is_alias
, is_kind
.
- Guide User guide Manipulating APIs Navigating APIs Object kind
- Reference Python API
is_class_private property
¤
is_class_private: bool
Whether this object/alias is class-private (starts with __
and is a class member).
- Guide User guide Manipulating APIs Navigating APIs Object visibility
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.
- Guide User guide Manipulating APIs Navigating APIs Object visibility
is_exported property
¤
is_exported: bool
Whether this object/alias is exported (listed in __all__
).
- Changelog 0.46.0 - 2024-06-16 Deprecations
- Guide User guide Manipulating APIs Navigating APIs Imports/exports
is_function property
¤
is_function: bool
Whether this object is a function.
See also: is_module
. is_class
, is_attribute
, is_alias
, is_kind
.
- Guide User guide Manipulating APIs Navigating APIs Object kind
- Reference Python API
is_imported property
¤
is_imported: bool
Whether this object/alias was imported from another module.
- Guide User guide Manipulating APIs Navigating APIs Imports/exports
- Reference Python API Models
Alias
imports
is_init_module property
¤
is_init_module: bool
is_module property
¤
is_module: bool
Whether this object is a module.
See also: is_init_module
. is_class
, is_function
, is_attribute
, is_alias
, is_kind
.
- Guide User guide Manipulating APIs Navigating APIs Object kind
- Reference Python API
is_namespace_package property
¤
is_namespace_package: bool
Whether this object is a namespace package (top folder, no __init__.py
).
See also: is_namespace_subpackage
.
is_namespace_subpackage property
¤
is_namespace_subpackage: bool
is_package property
¤
is_package: bool
is_private property
¤
is_private: bool
Whether this object/alias is private (starts with _
) but not special.
- Guide User guide Manipulating APIs Navigating APIs Object visibility
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.
- Changelog 0.46.0 - 2024-06-16 Deprecations
- Guide User guide Manipulating APIs Navigating APIs Object visibility
is_special property
¤
is_special: bool
Whether this object/alias is special ("dunder" attribute/method, starts and end with __
).
- Guide User guide Manipulating APIs Navigating APIs Object visibility
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.
- Guide User guide Manipulating APIs Navigating APIs Imports/exports
labels instance-attribute
¤
lineno instance-attribute
¤
lines property
¤
The lines containing the source of this object.
See also: lines_collection
, source
.
lines_collection property
¤
lines_collection: LinesCollection
The lines collection attached to this object or its parents.
Raises:
-
ValueError
–When no modules collection can be found in the object or its parents.
members instance-attribute
¤
The object members (modules, classes, functions, attributes).
See also: inherited_members
, get_member
, set_member
, filter_members
.
- Guide User guide Manipulating APIs Navigating APIs Moving down: members
- Reference Python API
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
¤
The module members.
This method is part of the consumer API: do not use when producing Griffe trees!
- Guide User guide Manipulating APIs Navigating APIs Moving down: members Inherited members
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.
- Guide User guide Manipulating APIs Navigating APIs Moving up: parents
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
¤
The parent of the object (none if top module).
- Guide User guide Manipulating APIs Navigating APIs Moving up: parents
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.
- Guide User guide Manipulating APIs Navigating APIs Object visibility
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.
- Guide User guide Manipulating APIs Navigating APIs Object location Source
- Reference Python API
relative_package_filepath property
¤
relative_package_filepath: Path
The file path where this object was defined, relative to the top module path.
See also: filepath
, relative_filepath
.
Raises:
-
ValueError
–When the relative path could not be computed.
runtime instance-attribute
¤
Whether this object is available at runtime.
Typically, type-guarded objects (under an if TYPE_CHECKING
condition) are not available at runtime.
setter instance-attribute
¤
setter: Function | None = None
The setter linked to this property.
- Guide User guide Manipulating APIs Navigating APIs Model-specific fields Attributes
source property
¤
source: str
value instance-attribute
¤
The attribute value.
- Guide User guide Manipulating APIs Navigating APIs Model-specific fields Attributes
__delitem__ ¤
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__ ¤
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__ ¤
Set a member with its name or path.
This method is part of the consumer API: do not use when producing Griffe trees!
Parameters:
-
key
¤str | Sequence[str]
) –The name or path of the member.
-
value
¤Object | Alias
) –The member.
Examples:
>>> griffe_object["foo"] = foo
>>> griffe_object["path.to.bar"] = bar
>>> griffe_object[("path", "to", "qux")] = qux
as_dict ¤
as_json ¤
del_member ¤
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 ¤
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.
- Guide User guide Manipulating APIs Serializing APIs Python API
- Reference
get_member ¤
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 ¤
is_kind ¤
Tell if this object is of the given kind.
See also: is_module
, is_class
, is_function
, is_attribute
, is_alias
.
Parameters:
Raises:
-
ValueError
–When an empty set is given as argument.
Returns:
-
bool
–True or False.
- Guide User guide Manipulating APIs Navigating APIs Object kind
- Reference Python API
resolve ¤
Resolve a name within this object's and parents' scope.
Parameters:
Raises:
-
NameResolutionError
–When the name could not be resolved.
Returns:
-
str
–The resolved name.
set_member ¤
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:
-
key
¤str | Sequence[str]
) –The name or path of the member.
-
value
¤Object | Alias
) –The member.
Examples:
>>> griffe_object.set_member("foo", foo)
>>> griffe_object.set_member("path.to.bar", bar)
>>> griffe_object.set_member(("path", "to", "qux"), qux)