Coverage for tests / test_patterns / test_python.py: 100%

28 statements  

« prev     ^ index     » next       coverage.py v7.13.3, created at 2026-02-08 15:04 -0800

1"""Tests for Python pattern detection.""" 

2 

3from patterns.python import check_nested_imports, check_wildcard_imports 

4from parsers.python import parse 

5 

6 

7def test_nested_imports(): 

8 """Test that nested imports are detected.""" 

9 code = """ 

10import os 

11 

12def foo(): 

13 import sys 

14 return sys.path 

15 

16class Bar: 

17 import pathlib 

18""" 

19 

20 root = parse(code) 

21 violations = check_nested_imports(root) 

22 

23 assert len(violations) == 2 

24 assert violations[0].type == "nested_import" 

25 assert violations[0].line == 5 

26 assert violations[1].type == "nested_import" 

27 assert violations[1].line == 9 

28 

29 

30def test_no_nested_imports(): 

31 """Test that module-level imports are not flagged.""" 

32 code = """ 

33import os 

34import sys 

35from pathlib import Path 

36""" 

37 

38 root = parse(code) 

39 violations = check_nested_imports(root) 

40 

41 assert len(violations) == 0 

42 

43 

44def test_wildcard_imports(): 

45 """Test that wildcard imports are detected.""" 

46 code = """ 

47from os import * 

48from pathlib import * 

49""" 

50 

51 root = parse(code) 

52 violations = check_wildcard_imports(root) 

53 

54 assert len(violations) == 2 

55 assert violations[0].type == "wildcard_import" 

56 assert violations[0].line == 2 

57 

58 

59def test_no_wildcard_imports(): 

60 """Test that explicit imports are not flagged.""" 

61 code = """ 

62from os import path 

63from pathlib import Path, PosixPath 

64""" 

65 

66 root = parse(code) 

67 violations = check_wildcard_imports(root) 

68 

69 assert len(violations) == 0