Development of an internal social media platform with personalised dashboards for students
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

brain_nose.py 2.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. # Copyright (c) 2015-2016 Claudiu Popa <pcmanticore@gmail.com>
  2. # Licensed under the LGPL: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html
  3. # For details: https://github.com/PyCQA/astroid/blob/master/COPYING.LESSER
  4. """Hooks for nose library."""
  5. import re
  6. import textwrap
  7. import astroid
  8. import astroid.builder
  9. _BUILDER = astroid.builder.AstroidBuilder(astroid.MANAGER)
  10. def _pep8(name, caps=re.compile('([A-Z])')):
  11. return caps.sub(lambda m: '_' + m.groups()[0].lower(), name)
  12. def _nose_tools_functions():
  13. """Get an iterator of names and bound methods."""
  14. module = _BUILDER.string_build(textwrap.dedent('''
  15. import unittest
  16. class Test(unittest.TestCase):
  17. pass
  18. a = Test()
  19. '''))
  20. try:
  21. case = next(module['a'].infer())
  22. except astroid.InferenceError:
  23. return
  24. for method in case.methods():
  25. if method.name.startswith('assert') and '_' not in method.name:
  26. pep8_name = _pep8(method.name)
  27. yield pep8_name, astroid.BoundMethod(method, case)
  28. if method.name == 'assertEqual':
  29. # nose also exports assert_equals.
  30. yield 'assert_equals', astroid.BoundMethod(method, case)
  31. def _nose_tools_transform(node):
  32. for method_name, method in _nose_tools_functions():
  33. node.locals[method_name] = [method]
  34. def _nose_tools_trivial_transform():
  35. """Custom transform for the nose.tools module."""
  36. stub = _BUILDER.string_build('''__all__ = []''')
  37. all_entries = ['ok_', 'eq_']
  38. for pep8_name, method in _nose_tools_functions():
  39. all_entries.append(pep8_name)
  40. stub[pep8_name] = method
  41. # Update the __all__ variable, since nose.tools
  42. # does this manually with .append.
  43. all_assign = stub['__all__'].parent
  44. all_object = astroid.List(all_entries)
  45. all_object.parent = all_assign
  46. all_assign.value = all_object
  47. return stub
  48. astroid.register_module_extender(astroid.MANAGER, 'nose.tools.trivial',
  49. _nose_tools_trivial_transform)
  50. astroid.MANAGER.register_transform(astroid.Module, _nose_tools_transform,
  51. lambda n: n.name == 'nose.tools')