Skip to content

FuzzyChoice does not accept SelfAttribute #1050

@anentropic

Description

@anentropic

The problem

I wanted to do this:

class MyFactory(factory.Factory):
    class Params:
        country_code_choices = (
            "AU",
            "GB",
            "DE",
            "US",
        )

    country_code = factory.fuzzy.FuzzyChoice(factory.SelfAttribute("..country_code_choices"))

But I get: TypeError: 'SelfAttribute' object is not iterable

Proposed solution

The docs are a bit unclear where exactly SelfAttribute is valid to be used or not. Examples show it used as argument to other fields, seems a similar usage to what I tried.

So I'm not sure if this is a bug or just needs documenting.

Extra notes

I am using this workaround:

    country_code = factory.LazyAttribute(
        lambda o: randgen.choice(o.country_code_choices)
    )

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions