Skip to content

attrs.any() doesn't accept records #1171

@lf-

Description

@lf-

I bet this is because the types available as attrs are special somehow that's not documented in the docs, and probably even for a good reason. However, the use case I have here is to have type safe nested structures in BUCK files (which one might rightfully question why. avoiding reinventing configerator while having type safe configs is the motive).

BUCK:

load("//dux/rules:json_document.bzl", "json_document", "Oops")

json_document(name = "foo", contents = Oops(a = 1))

//dux/rules/json_document.bzl:

"""
Allows you to write a JSON document in a Starlark file, to get comments and
schema and such.
"""

Oops = record(a = int)

def _json_document_impl(ctx: AnalysisContext):
    out = ctx.actions.write(
        "{}.json".format(ctx.label.name),
        ctx.attrs.content,
        pretty = True
    )
    return [DefaultInfo(default_outputs=[out])]

json_document = rule(
    impl = _json_document_impl,
    attrs = {
        "contents": attrs.any(doc = "What to put in the JSON document; should generally be a record."),
    }
)
$ buck build //dux/code-intel/config:foo
BUILD FAILED
Error evaluating build file: `root//dux/code-intel/config:BUCK`

Caused by:
    0: Traceback (most recent call last):
         * dux/code-intel/config/BUCK:5, in <module>
             json_document(name = "foo", contents = Oops(a = 1))
       
       error: Error coercing attribute `contents` of `root//dux/code-intel/config:foo`
        --> dux/code-intel/config/BUCK:5:1
         |
       5 | json_document(name = "foo", contents = Oops(a = 1))
         | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
         |
    1: Error coercing attribute `contents` of type `attrs.any()`
    2: Error coercing record[Oops](a=1)
    3: Cannot coerce value of type `record` to any: `record[Oops](a=1)`

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions