Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/astroid/brain/brain_nose.py: 38%

39 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2023-06-07 06:53 +0000

1# Licensed under the LGPL: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html 

2# For details: https://github.com/pylint-dev/astroid/blob/main/LICENSE 

3# Copyright (c) https://github.com/pylint-dev/astroid/blob/main/CONTRIBUTORS.txt 

4 

5"""Hooks for nose library.""" 

6 

7import re 

8import textwrap 

9 

10import astroid.builder 

11from astroid.brain.helpers import register_module_extender 

12from astroid.exceptions import InferenceError 

13from astroid.manager import AstroidManager 

14 

15_BUILDER = astroid.builder.AstroidBuilder(AstroidManager()) 

16 

17 

18CAPITALS = re.compile("([A-Z])") 

19 

20 

21def _pep8(name, caps=CAPITALS): 

22 return caps.sub(lambda m: "_" + m.groups()[0].lower(), name) 

23 

24 

25def _nose_tools_functions(): 

26 """Get an iterator of names and bound methods.""" 

27 module = _BUILDER.string_build( 

28 textwrap.dedent( 

29 """ 

30 import unittest 

31 

32 class Test(unittest.TestCase): 

33 pass 

34 a = Test() 

35 """ 

36 ) 

37 ) 

38 try: 

39 case = next(module["a"].infer()) 

40 except (InferenceError, StopIteration): 

41 return 

42 for method in case.methods(): 

43 if method.name.startswith("assert") and "_" not in method.name: 

44 pep8_name = _pep8(method.name) 

45 yield pep8_name, astroid.BoundMethod(method, case) 

46 if method.name == "assertEqual": 

47 # nose also exports assert_equals. 

48 yield "assert_equals", astroid.BoundMethod(method, case) 

49 

50 

51def _nose_tools_transform(node): 

52 for method_name, method in _nose_tools_functions(): 

53 node.locals[method_name] = [method] 

54 

55 

56def _nose_tools_trivial_transform(): 

57 """Custom transform for the nose.tools module.""" 

58 stub = _BUILDER.string_build("""__all__ = []""") 

59 all_entries = ["ok_", "eq_"] 

60 

61 for pep8_name, method in _nose_tools_functions(): 

62 all_entries.append(pep8_name) 

63 stub[pep8_name] = method 

64 

65 # Update the __all__ variable, since nose.tools 

66 # does this manually with .append. 

67 all_assign = stub["__all__"].parent 

68 all_object = astroid.List(all_entries) 

69 all_object.parent = all_assign 

70 all_assign.value = all_object 

71 return stub 

72 

73 

74register_module_extender( 

75 AstroidManager(), "nose.tools.trivial", _nose_tools_trivial_transform 

76) 

77AstroidManager().register_transform( 

78 astroid.Module, _nose_tools_transform, lambda n: n.name == "nose.tools" 

79)