# Buggy code
def build(self, parent_step=None, force_sequence=None):
    """Build a factory instance."""
    # TODO: Handle "batch build" natively
    pre, post = parse_declarations(
        self.extras,
        base_pre=self.factory_meta.pre_declarations,
        base_post=self.factory_meta.post_declarations,
    )

    if force_sequence is not None:
        sequence = force_sequence
    elif self.force_init_sequence is not None:
        sequence = self.force_init_sequence
    else:
        sequence = self.factory_meta.next_sequence()

    step = BuildStep(
        builder=self,
        sequence=sequence,
        parent_step=parent_step,
    )
    step.resolve(pre)

    args, kwargs = self.factory_meta.prepare_arguments(step.attributes)

    instance = self.factory_meta.instantiate(
        step=step,
        args=args,
        kwargs=kwargs,
    )

    postgen_results = {}
    for declaration_name in post.sorted():
        declaration = post[declaration_name]
        postgen_context = PostGenerationContext(
            value_provided="" in declaration.context,
            value=declaration.context.get(""),
            extra={k: v for k, v in declaration.context.items() if k != ""},
        )
        postgen_results[declaration_name] = declaration.declaration.call(
            instance=instance,
            step=step,
            context=postgen_context,
        )
    self.factory_meta.use_postgeneration_results(
        instance=instance,
        step=step,
        results=postgen_results,
    )
    return instance


# Traceback (TypeError)
Traceback (most recent call last):
File "/root/docs/notes/gist/factoryboy_maybe_broke_postgen.py", line 55, in <module>
value = CopyTargetFactory()
File "/root/.pyenv/versions/frontend/lib/python2.7/site-packages/factory/base.py", line 46, in __call__
return cls.create(**kwargs)
File "/root/.pyenv/versions/frontend/lib/python2.7/site-packages/factory/base.py", line 563, in create
return cls._generate(enums.CREATE_STRATEGY, kwargs)
File "/root/.pyenv/versions/frontend/lib/python2.7/site-packages/factory/base.py", line 500, in _generate
return step.build()
File "/root/.pyenv/versions/frontend/lib/python2.7/site-packages/factory/builder.py", line 293, in build
context=postgen_context,
File "/root/.pyenv/versions/frontend/lib/python2.7/site-packages/factory/declarations.py", line 472, in call
context=context,
File "/root/.pyenv/versions/frontend/lib/python2.7/site-packages/factory/declarations.py", line 610, in call
instance, create, context.value, **context.extra)
File "/root/docs/notes/gist/factoryboy_maybe_broke_postgen.py", line 50, in plain_m2m
for _ in xrange(num)))
TypeError: an integer is required

# Fixed code
def build(self, parent_step=None, force_sequence=None):
    """Build a factory instance."""
    # TODO: Handle "batch build" natively
    pre, post = parse_declarations(
        self.extras,
        base_pre=self.factory_meta.pre_declarations,
        base_post=self.factory_meta.post_declarations,
    )

    if force_sequence is not None:
        sequence = force_sequence
    elif self.force_init_sequence is not None:
        sequence = self.force_init_sequence
    else:
        sequence = self.factory_meta.next_sequence()

    step = BuildStep(
        builder=self,
        sequence=sequence,
        parent_step=parent_step,
    )
    step.resolve(pre)

    args, kwargs = self.factory_meta.prepare_arguments(step.attributes)

    instance = self.factory_meta.instantiate(
        step=step,
        args=args,
        kwargs=kwargs,
    )

    postgen_results = {}
    for declaration_name in post.sorted():
        declaration = post[declaration_name]
        unrolled_context = declaration.declaration.unroll_context(
            instance=instance,
            step=step,
            context=declaration.context,
        )

        postgen_context = PostGenerationContext(
            value_provided="" in unrolled_context,
            value=unrolled_context.get(""),
            extra={k: v for k, v in unrolled_context.items() if k != ""},
        )
        postgen_results[declaration_name] = declaration.declaration.call(
            instance=instance,
            step=step,
            context=postgen_context,
        )
    self.factory_meta.use_postgeneration_results(
        instance=instance,
        step=step,
        results=postgen_results,
    )
    return instance