0

all this won't work. even changing side_effect to something completelly different won't solve a problem.

mock = unittest.mock.MagicMock()
with unittest.mock.patch('builtins.set', mock):
    handle = set()

print(mock.called)

import builtins
mock = unittest.mock.MagicMock()
with unittest.mock.patch.object(builtins, "set"):
    handle = set()

I actually need somehow to know, if nested function uses some classes\methods\structures. For instance, set(), tuple(), dict() | {}.

For instance, it works with dict(), but when you use {}, there is False returns. Is there any convenient way to introspect code in assertions, thinking about it as a black-box?

kAldown
  • 610
  • 2
  • 8
  • 27
  • Why do you need to know that? Maybe there’s a better approach. – Ry- Feb 19 '18 at 12:08
  • What is your use case? You can't intercept/mock `[]` and `{}` because they are literals and will bypass global/builtin lookup. – code_onkel Feb 19 '18 at 12:08
  • I have a module. And want to know, if any function inside this module. uses set(), or dict(), etc ... Let me tell you whole. I made a task for students to code functions that behave like a set. Wrote tests, Apart from test, I must know, if anyone cheated and used builtin.set() – kAldown Feb 19 '18 at 12:13
  • 1
    Could you use the ast module on their code to check that way? – Tom Dalton Feb 19 '18 at 12:40

0 Answers0