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

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

2

3 from patterns.python import check_nested_imports, check_wildcard_imports

4 from parsers.python import parse

5

6

7 def test_nested_imports():

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

9 code = """

10 import os

11

12 def foo():

13 import sys

14 return sys.path

15

16 class 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

30 def test_no_nested_imports():

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

32 code = """

33 import os

34 import sys

35 from pathlib import Path

36"""

37

38 root = parse(code)

39 violations = check_nested_imports(root)

40

41 assert len(violations) == 0

42

43

44 def test_wildcard_imports():

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

46 code = """

47 from os import *

48 from 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

59 def test_no_wildcard_imports():

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

61 code = """

62 from os import path

63 from pathlib import Path, PosixPath

64"""

65

66 root = parse(code)

67 violations = check_wildcard_imports(root)

68

69 assert len(violations) == 0