factory_boy icon indicating copy to clipboard operation
factory_boy copied to clipboard

Make argument to factory.Sequence optional

Open jocassid opened this issue 5 years ago • 1 comments
trafficstars

The problem

To create an integer sequence I used id = factory.Sequence(lambda n: n). Is there a way to have an integer sequence without the do-nothing lambda n: n?

Proposed solution

Make the __init__ argument optional. I think the code would look like:

class Sequence(BaseDeclaration):
    def __init__(self, function=None):
        super(Sequence, self).__init__()
        self.function = function

    def evaluate(self, instance, step, extra):
        logger.debug("Sequence: Computing next value of %r for seq=%s", self.function, step.sequence)
        n = int(step.sequence)
        if self.function:
            return self.function(n)
        return n

Extra notes

  • Would need to update documentation for this change
  • If there is already a simple way to do this, please add a link at the top of this section in the documentation.

jocassid avatar Feb 19 '20 20:02 jocassid

Here you are for a simple solution tant lambda n: n:

id = factory.Sequence(int)

It will call int as a function:

str_x = "1"
int_x = int(str_x)
assert isinstance(int_x, int)

KerberosMorphy avatar Apr 24 '23 14:04 KerberosMorphy