This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author cjw296
Recipients cjw296, lisroach, mariocj89, michael.foord, piscvau, xtreak
Date 2019-11-10.07:00:43
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <B30E9C22-E5FB-40D2-B1A0-EAFD1765499C@withers.org>
In-reply-to <1573361366.33.0.271459329886.issue38757@roundup.psfhosted.org>
Content
Not sure this is correct, if an effect is an exception and requires args, then it should be passed as an instance, not a class:

Mock(side_effect=MyException(‘foo’))

> On 10 Nov 2019, at 04:49, Karthikeyan Singaravelan <report@bugs.python.org> wrote:
> 
> 
> Karthikeyan Singaravelan <tir.karthi@gmail.com> added the comment:
> 
> Currently, the exception is not instantiated. Maybe we can check if it's callable and pass args, kwargs to the exception constructor to be raised.
> 
> diff --git a/Lib/unittest/mock.py b/Lib/unittest/mock.py
> index a48132c5b1..f5bcb911f5 100644
> --- a/Lib/unittest/mock.py
> +++ b/Lib/unittest/mock.py
> @@ -1145,7 +1145,10 @@ class CallableMixin(Base):
>         effect = self.side_effect
>         if effect is not None:
>             if _is_exception(effect):
> -                raise effect
> +                if _callable(effect):
> +                    raise effect(*args, **kwargs)
> +                else:
> +                    raise effect
>             elif not _callable(effect):
>                 result = next(effect)
>                 if _is_exception(result):
> 
> ----------
> nosy: +cjw296, lisroach, mariocj89, michael.foord
> 
> _______________________________________
> Python tracker <report@bugs.python.org>
> <https://bugs.python.org/issue38757>
> _______________________________________
History
Date User Action Args
2019-11-10 07:00:43cjw296setrecipients: + cjw296, michael.foord, lisroach, mariocj89, xtreak, piscvau
2019-11-10 07:00:43cjw296linkissue38757 messages
2019-11-10 07:00:43cjw296create