attr

Report an issue View source Nightly · 7.3 · 7.2 · 7.1 · 7.0 · 6.5

This is a top-level module for defining the attribute schemas of a rule or aspect. Each function returns an object representing the schema of a single attribute. These objects are used as the values of the attrs dictionary argument of rule(), aspect(), repository_rule() and tag_class().

See the Rules page for more on defining and using attributes.

Members

bool

Attribute attr.bool(configurable=unbound, default=False, doc=None, mandatory=False)

Creates a schema for a boolean attribute. The corresponding ctx.attr attribute will be of type bool.

Parameters

Parameter Description
configurable bool; or unbound; default is unbound
This argument can only be specified for an attribute of a symbolic macro.

If configurable is explicitly set to False, the symbolic macro attribute is non-configurable - in other words, it cannot take a select() value. If the configurable is either unbound or explicitly set to True, the attribute is configurable and can take a select() value.

For an attribute of a rule or aspect, configurable must be left unbound. Most Starlark rule attributes are always configurable, with the exception of attr.output(), attr.output_list(), and attr.license() rule attributes, which are always non-configurable.

default bool; default is False
A default value to use if no value for this attribute is given when instantiating the rule.
doc string; or None; default is None
A description of the attribute that can be extracted by documentation generating tools.
mandatory bool; default is False
If true, the value must be specified explicitly (even if it has a default).

int

Attribute attr.int(configurable=unbound, default=0, doc=None, mandatory=False, values=[])

Creates a schema for an integer attribute. The value must be in the signed 32-bit range. The corresponding ctx.attr attribute will be of type int.

Parameters

Parameter Description
configurable bool; or unbound; default is unbound
This argument can only be specified for an attribute of a symbolic macro.

If configurable is explicitly set to False, the symbolic macro attribute is non-configurable - in other words, it cannot take a select() value. If the configurable is either unbound or explicitly set to True, the attribute is configurable and can take a select() value.

For an attribute of a rule or aspect, configurable must be left unbound. Most Starlark rule attributes are always configurable, with the exception of attr.output(), attr.output_list(), and attr.license() rule attributes, which are always non-configurable.

default int; default is 0
A default value to use if no value for this attribute is given when instantiating the rule.
doc string; or None; default is None
A description of the attribute that can be extracted by documentation generating tools.
mandatory bool; default is False
If true, the value must be specified explicitly (even if it has a default).
values sequence of ints; default is []
The list of allowed values for the attribute. An error is raised if any other value is given.

int_list

Attribute attr.int_list(mandatory=False, allow_empty=True, *, configurable=unbound, default=[], doc=None)

Creates a schema for a list-of-integers attribute. Each element must be in the signed 32-bit range.

Parameters

Parameter Description
mandatory bool; default is False
If true, the value must be specified explicitly (even if it has a default).
allow_empty bool; default is True
True if the attribute can be empty.
configurable bool; or unbound; default is unbound
This argument can only be specified for an attribute of a symbolic macro.

If configurable is explicitly set to False, the symbolic macro attribute is non-configurable - in other words, it cannot take a select() value. If the configurable is either unbound or explicitly set to True, the attribute is configurable and can take a select() value.

For an attribute of a rule or aspect, configurable must be left unbound. Most Starlark rule attributes are always configurable, with the exception of attr.output(), attr.output_list(), and attr.license() rule attributes, which are always non-configurable.

default sequence of ints; default is []
A default value to use if no value for this attribute is given when instantiating the rule.
doc string; or None; default is None
A description of the attribute that can be extracted by documentation generating tools.

label

Attribute attr.label(configurable=unbound, default=None, materializer=None, doc=None, executable=False, allow_files=None, allow_single_file=None, mandatory=False, skip_validations=False, providers=[], for_dependency_resolution=unbound, allow_rules=None, cfg=None, aspects=[], flags=[])

Creates a schema for a label attribute. This is a dependency attribute.

This attribute contains unique Label values. If a string is supplied in place of a Label, it will be converted using the label constructor. The relative parts of the label path, including the (possibly renamed) repository, are resolved with respect to the instantiated target's package.

At analysis time (within the rule's implementation function), when retrieving the attribute value from ctx.attr, labels are replaced by the corresponding Targets. This allows you to access the providers of the current target's dependencies.

In addition to ordinary source files, this kind of attribute is often used to refer to a tool -- for example, a compiler. Such tools are considered to be dependencies, just like source files. To avoid requiring users to specify the tool's label every time they use the rule in their BUILD files, you can hard-code the label of a canonical tool as the default value of this attribute. If you also want to prevent users from overriding this default, you can make the attribute private by giving it a name that starts with an underscore. See the Rules page for more information.

Parameters

Parameter Description
configurable bool; or unbound; default is unbound
This argument can only be specified for an attribute of a symbolic macro.

If configurable is explicitly set to False, the symbolic macro attribute is non-configurable - in other words, it cannot take a select() value. If the configurable is either unbound or explicitly set to True, the attribute is configurable and can take a select() value.

For an attribute of a rule or aspect, configurable must be left unbound. Most Starlark rule attributes are always configurable, with the exception of attr.output(), attr.output_list(), and attr.license() rule attributes, which are always non-configurable.

default Label; or string; or LateBoundDefault; or NativeComputedDefault; or function; or None; default is None
A default value to use if no value for this attribute is given when instantiating the rule.Use a string or the Label function to specify a default value, for example, attr.label(default = "//a:b").
materializer function; default is None
Experimental. This parameter is experimental and may change at any time. Please do not depend on it. It may be enabled on an experimental basis by setting --experimental_dormant_deps
If set, the attribute materializes dormant dependencies from the transitive closure. The value of this parameter must be a functon that gets access to the values of the attributes of the rule that either are not dependencies or are marked as available for dependency resolution. It must return either a dormant dependency or a list of them depending on the type of the attribute
doc string; or None; default is None
A description of the attribute that can be extracted by documentation generating tools.
executable bool; default is False
True if the dependency has to be executable. This means the label must refer to an executable file, or to a rule that outputs an executable file. Access the label with ctx.executable.<attribute_name>.
allow_files bool; or sequence of strings; or None; default is None
Whether File targets are allowed. Can be True, False (default), or a list of file extensions that are allowed (for example, [".cc", ".cpp"]).
allow_single_file default is None
This is similar to allow_files, with the restriction that the label must correspond to a single File. Access it through ctx.file.<attribute_name>.
mandatory bool; default is False
If true, the value must be specified explicitly (even if it has a default).
skip_validations bool; default is False
If true, validation actions of transitive dependencies from this attribute will not run. This is a temporary mitigation and WILL be removed in the future.
providers sequence; default is []
The providers that must be given by any dependency appearing in this attribute.

The format of this argument is a list of lists of providers -- *Info objects returned by provider() (or in the case of a legacy provider, its string name). The dependency must return ALL providers mentioned in at least ONE of the inner lists. As a convenience, this argument may also be a single-level list of providers, in which case it is wrapped in an outer list with one element. It is NOT required that the rule of the dependency advertises those providers in its provides parameter, however, it is considered best practice.

for_dependency_resolution default is unbound
If this is set, the attribute is available for materializers. Only rules marked with the flag of the same name are allowed to be referenced through such attributes.
allow_rules sequence of strings; or None; default is None
Which rule targets (name of the classes) are allowed. This is deprecated (kept only for compatibility), use providers instead.
cfg default is None
Configuration of the attribute. It can be either "exec", which indicates that the dependency is built for the execution platform, or "target", which indicates that the dependency is build for the target platform. A typical example of the difference is when building mobile apps, where the target platform is Android or iOS while the execution platform is Linux, macOS, or Windows. This parameter is required if executable is True to guard against accidentally building host tools in the target configuration. "target" has no semantic effect, so don't set it when executable is False unless it really helps clarify your intentions.
aspects sequence of Aspects; default is []
Aspects that should be applied to the dependency or dependencies specified by this attribute.
flags sequence of strings; default is []
Deprecated, will be removed.

label_keyed_string_dict

Attribute attr.label_keyed_string_dict(allow_empty=True, *, configurable=unbound, default={}, doc=None, allow_files=None, allow_rules=None, providers=[], for_dependency_resolution=unbound, flags=[], mandatory=False, cfg=None, aspects=[])

Creates a schema for an attribute holding a dictionary, where the keys are labels and the values are strings. This is a dependency attribute.

This attribute contains unique Label values. If a string is supplied in place of a Label, it will be converted using the label constructor. The relative parts of the label path, including the (possibly renamed) repository, are resolved with respect to the instantiated target's package.

At analysis time (within the rule's implementation function), when retrieving the attribute value from ctx.attr, labels are replaced by the corresponding Targets. This allows you to access the providers of the current target's dependencies.

Parameters

Parameter Description
allow_empty bool; default is True
True if the attribute can be empty.
configurable bool; or unbound; default is unbound
This argument can only be specified for an attribute of a symbolic macro.

If configurable is explicitly set to False, the symbolic macro attribute is non-configurable - in other words, it cannot take a select() value. If the configurable is either unbound or explicitly set to True, the attribute is configurable and can take a select() value.

For an attribute of a rule or aspect, configurable must be left unbound. Most Starlark rule attributes are always configurable, with the exception of attr.output(), attr.output_list(), and attr.license() rule attributes, which are always non-configurable.

default dict; or function; default is {}
A default value to use if no value for this attribute is given when instantiating the rule.Use strings or the Label function to specify default values, for example, attr.label_keyed_string_dict(default = {"//a:b": "value", "//a:c": "string"}).
doc string; or None; default is None
A description of the attribute that can be extracted by documentation generating tools.
allow_files bool; or sequence of strings; or None; default is None
Whether File targets are allowed. Can be True, False (default), or a list of file extensions that are allowed (for example, [".cc", ".cpp"]).
allow_rules sequence of strings; or None; default is None
Which rule targets (name of the classes) are allowed. This is deprecated (kept only for compatibility), use providers instead.
providers sequence; default is []
The providers that must be given by any dependency appearing in this attribute.

The format of this argument is a list of lists of providers -- *Info objects returned by provider() (or in the case of a legacy provider, its string name). The dependency must return ALL providers mentioned in at least ONE of the inner lists. As a convenience, this argument may also be a single-level list of providers, in which case it is wrapped in an outer list with one element. It is NOT required that the rule of the dependency advertises those providers in its provides parameter, however, it is considered best practice.

for_dependency_resolution default is unbound
If this is set, the attribute is available for materializers. Only rules marked with the flag of the same name are allowed to be referenced through such attributes.
flags sequence of strings; default is []
Deprecated, will be removed.
mandatory bool; default is False
If true, the value must be specified explicitly (even if it has a default).
cfg default is None
Configuration of the attribute. It can be either "exec", which indicates that the dependency is built for the execution platform, or "target", which indicates that the dependency is build for the target platform. A typical example of the difference is when building mobile apps, where the target platform is Android or iOS while the execution platform is Linux, macOS, or Windows.
aspects sequence of Aspects; default is []
Aspects that should be applied to the dependency or dependencies specified by this attribute.

label_list

Attribute attr.label_list(allow_empty=True, *, configurable=unbound, default=[], materializer=None, doc=None, allow_files=None, allow_rules=None, providers=[], for_dependency_resolution=unbound, flags=[], mandatory=False, skip_validations=False, cfg=None, aspects=[])

Creates a schema for a list-of-labels attribute. This is a dependency attribute. The corresponding ctx.attr attribute will be of type list of Targets.

This attribute contains unique Label values. If a string is supplied in place of a Label, it will be converted using the label constructor. The relative parts of the label path, including the (possibly renamed) repository, are resolved with respect to the instantiated target's package.

At analysis time (within the rule's implementation function), when retrieving the attribute value from ctx.attr, labels are replaced by the corresponding Targets. This allows you to access the providers of the current target's dependencies.

Parameters

Parameter Description
allow_empty bool; default is True
True if the attribute can be empty.
configurable bool; or unbound; default is unbound
This argument can only be specified for an attribute of a symbolic macro.

If configurable is explicitly set to False, the symbolic macro attribute is non-configurable - in other words, it cannot take a select() value. If the configurable is either unbound or explicitly set to True, the attribute is configurable and can take a select() value.

For an attribute of a rule or aspect, configurable must be left unbound. Most Starlark rule attributes are always configurable, with the exception of attr.output(), attr.output_list(), and attr.license() rule attributes, which are always non-configurable.

default sequence of Labels; or function; default is []
A default value to use if no value for this attribute is given when instantiating the rule.Use strings or the Label function to specify default values, for example, attr.label_list(default = ["//a:b", "//a:c"]).
materializer function; default is None
Experimental. This parameter is experimental and may change at any time. Please do not depend on it. It may be enabled on an experimental basis by setting --experimental_dormant_deps
If set, the attribute materializes dormant dependencies from the transitive closure. The value of this parameter must be a functon that gets access to the values of the attributes of the rule that either are not dependencies or are marked as available for dependency resolution. It must return either a dormant dependency or a list of them depending on the type of the attribute
doc string; or None; default is None
A description of the attribute that can be extracted by documentation generating tools.
allow_files bool; or sequence of strings; or None; default is None
Whether File targets are allowed. Can be True, False (default), or a list of file extensions that are allowed (for example, [".cc", ".cpp"]).
allow_rules sequence of strings; or None; default is None
Which rule targets (name of the classes) are allowed. This is deprecated (kept only for compatibility), use providers instead.
providers sequence; default is []
The providers that must be given by any dependency appearing in this attribute.

The format of this argument is a list of lists of providers -- *Info objects returned by provider() (or in the case of a legacy provider, its string name). The dependency must return ALL providers mentioned in at least ONE of the inner lists. As a convenience, this argument may also be a single-level list of providers, in which case it is wrapped in an outer list with one element. It is NOT required that the rule of the dependency advertises those providers in its provides parameter, however, it is considered best practice.

for_dependency_resolution default is unbound
If this is set, the attribute is available for materializers. Only rules marked with the flag of the same name are allowed to be referenced through such attributes.
flags sequence of strings; default is []
Deprecated, will be removed.
mandatory bool; default is False
If true, the value must be specified explicitly (even if it has a default).
skip_validations bool; default is False
If true, validation actions of transitive dependencies from this attribute will not run. This is a temporary mitigation and WILL be removed in the future.
cfg default is None
Configuration of the attribute. It can be either "exec", which indicates that the dependency is built for the execution platform, or "target", which indicates that the dependency is build for the target platform. A typical example of the difference is when building mobile apps, where the target platform is Android or iOS while the execution platform is Linux, macOS, or Windows.
aspects sequence of Aspects; default is []
Aspects that should be applied to the dependency or dependencies specified by this attribute.

output

Attribute attr.output(doc=None, mandatory=False)

Creates a schema for an output (label) attribute.

This attribute contains unique Label values. If a string is supplied in place of a Label, it will be converted using the label constructor. The relative parts of the label path, including the (possibly renamed) repository, are resolved with respect to the instantiated target's package.

At analysis time, the corresponding File can be retrieved using ctx.outputs.

Parameters

Parameter Description
doc string; or None; default is None
A description of the attribute that can be extracted by documentation generating tools.
mandatory bool; default is False
If true, the value must be specified explicitly (even if it has a default).

output_list

Attribute attr.output_list(allow_empty=True, *, doc=None, mandatory=False)

Creates a schema for a list-of-outputs attribute.

This attribute contains unique Label values. If a string is supplied in place of a Label, it will be converted using the label constructor. The relative parts of the label path, including the (possibly renamed) repository, are resolved with respect to the instantiated target's package.

At analysis time, the corresponding File can be retrieved using ctx.outputs.

Parameters

Parameter Description
allow_empty bool; default is True
True if the attribute can be empty.
doc string; or None; default is None
A description of the attribute that can be extracted by documentation generating tools.
mandatory bool; default is False
If true, the value must be specified explicitly (even if it has a default).

string

Attribute attr.string(configurable=unbound, default='', doc=None, mandatory=False, values=[])

Creates a schema for a string attribute.

Parameters

Parameter Description
configurable bool; or unbound; default is unbound
This argument can only be specified for an attribute of a symbolic macro.

If configurable is explicitly set to False, the symbolic macro attribute is non-configurable - in other words, it cannot take a select() value. If the configurable is either unbound or explicitly set to True, the attribute is configurable and can take a select() value.

For an attribute of a rule or aspect, configurable must be left unbound. Most Starlark rule attributes are always configurable, with the exception of attr.output(), attr.output_list(), and attr.license() rule attributes, which are always non-configurable.

default string; or NativeComputedDefault; default is ''
A default value to use if no value for this attribute is given when instantiating the rule.
doc string; or None; default is None
A description of the attribute that can be extracted by documentation generating tools.
mandatory bool; default is False
If true, the value must be specified explicitly (even if it has a default).
values sequence of strings; default is []
The list of allowed values for the attribute. An error is raised if any other value is given.

string_dict

Attribute attr.string_dict(allow_empty=True, *, configurable=unbound, default={}, doc=None, mandatory=False)

Creates a schema for an attribute holding a dictionary, where the keys and values are strings.

Parameters

Parameter Description
allow_empty bool; default is True
True if the attribute can be empty.
configurable bool; or unbound; default is unbound
This argument can only be specified for an attribute of a symbolic macro.

If configurable is explicitly set to False, the symbolic macro attribute is non-configurable - in other words, it cannot take a select() value. If the configurable is either unbound or explicitly set to True, the attribute is configurable and can take a select() value.

For an attribute of a rule or aspect, configurable must be left unbound. Most Starlark rule attributes are always configurable, with the exception of attr.output(), attr.output_list(), and attr.license() rule attributes, which are always non-configurable.

default dict; default is {}
A default value to use if no value for this attribute is given when instantiating the rule.
doc string; or None; default is None
A description of the attribute that can be extracted by documentation generating tools.
mandatory bool; default is False
If true, the value must be specified explicitly (even if it has a default).

string_list

Attribute attr.string_list(mandatory=False, allow_empty=True, *, configurable=unbound, default=[], doc=None)

Creates a schema for a list-of-strings attribute.

Parameters

Parameter Description
mandatory bool; default is False
If true, the value must be specified explicitly (even if it has a default).
allow_empty bool; default is True
True if the attribute can be empty.
configurable bool; or unbound; default is unbound
This argument can only be specified for an attribute of a symbolic macro.

If configurable is explicitly set to False, the symbolic macro attribute is non-configurable - in other words, it cannot take a select() value. If the configurable is either unbound or explicitly set to True, the attribute is configurable and can take a select() value.

For an attribute of a rule or aspect, configurable must be left unbound. Most Starlark rule attributes are always configurable, with the exception of attr.output(), attr.output_list(), and attr.license() rule attributes, which are always non-configurable.

default sequence of strings; or NativeComputedDefault; default is []
A default value to use if no value for this attribute is given when instantiating the rule.
doc string; or None; default is None
A description of the attribute that can be extracted by documentation generating tools.

string_list_dict

Attribute attr.string_list_dict(allow_empty=True, *, configurable=unbound, default={}, doc=None, mandatory=False)

Creates a schema for an attribute holding a dictionary, where the keys are strings and the values are lists of strings.

Parameters

Parameter Description
allow_empty bool; default is True
True if the attribute can be empty.
configurable bool; or unbound; default is unbound
This argument can only be specified for an attribute of a symbolic macro.

If configurable is explicitly set to False, the symbolic macro attribute is non-configurable - in other words, it cannot take a select() value. If the configurable is either unbound or explicitly set to True, the attribute is configurable and can take a select() value.

For an attribute of a rule or aspect, configurable must be left unbound. Most Starlark rule attributes are always configurable, with the exception of attr.output(), attr.output_list(), and attr.license() rule attributes, which are always non-configurable.

default dict; default is {}
A default value to use if no value for this attribute is given when instantiating the rule.
doc string; or None; default is None
A description of the attribute that can be extracted by documentation generating tools.
mandatory bool; default is False
If true, the value must be specified explicitly (even if it has a default).