# Buggy code
def __getattr__(self, name):
    """Retrieve an attribute's value.

    This will compute it if needed, unless it is already on the list of
    attributes being computed.
    """
    if name in self.__pending:
        raise errors.CyclicDefinitionError(
            "Cyclic lazy attribute definition for %r; cycle found in %r."
            % (name, self.__pending)
        )
    elif name in self.__values:
        return self.__values[name]
    elif name in self.__declarations:
        declaration = self.__declarations[name]
        value = declaration.declaration
        if enums.get_builder_phase(value) == enums.BuilderPhase.ATTRIBUTE_RESOLUTION:
            self.__pending.append(name)
            try:
                value = value.evaluate(
                    instance=self,
                    step=self.__step,
                    extra=declaration.context,
                )
            finally:
                last = self.__pending.pop()
            assert name == last

        self.__values[name] = value
        return value
    else:
        raise AttributeError(
            "The parameter %r is unknown. Evaluated attributes are %r, "
            "definitions are %r." % (name, self.__values, self.__declarations)
        )


# 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 __getattr__(self, name):
    """Retrieve an attribute's value.

    This will compute it if needed, unless it is already on the list of
    attributes being computed.
    """
    if name in self.__pending:
        raise errors.CyclicDefinitionError(
            "Cyclic lazy attribute definition for %r; cycle found in %r."
            % (name, self.__pending)
        )
    elif name in self.__values:
        return self.__values[name]
    elif name in self.__declarations:
        declaration = self.__declarations[name]
        value = declaration.declaration
        if enums.get_builder_phase(value) == enums.BuilderPhase.ATTRIBUTE_RESOLUTION:
            self.__pending.append(name)
            try:
                context = value.unroll_context(
                    instance=self,
                    step=self.__step,
                    context=declaration.context,
                )

                value = value.evaluate(
                    instance=self,
                    step=self.__step,
                    extra=context,
                )
            finally:
                last = self.__pending.pop()
            assert name == last

        self.__values[name] = value
        return value
    else:
        raise AttributeError(
            "The parameter %r is unknown. Evaluated attributes are %r, "
            "definitions are %r." % (name, self.__values, self.__declarations)
        )