• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

speedyk-005 / chunklet-py / 24798516591

22 Apr 2026 07:19PM UTC coverage: 90.606% (-0.2%) from 90.758%
24798516591

push

github

speedyk-005
refactor: remove redundant type hints from docstrings

- Strip (type) from Args/Returns where signature already has types
- Simplify Returns format to prose description
- Run clean_docstrings.py on src/chunklet (26 files)
- Add ExtractionState TypedDict for type safety (from earlier refactor)

1360 of 1501 relevant lines covered (90.61%)

3.62 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

79.17
/src/chunklet/code_chunker/utils.py
1
import ast
4✔
2
import re
4✔
3
from pathlib import Path
4✔
4

5
from pygments.lexers import guess_lexer
4✔
6
from pygments.util import ClassNotFound
4✔
7

8
from chunklet.common.path_utils import is_path_like
4✔
9
from chunklet.common.validation import validate_input
4✔
10

11

12
@validate_input
4✔
13
def is_python_code(source: str | Path) -> bool:
4✔
14
    """
15
    Check if a source is written in Python.
16

17
    This function uses multiple indicators, prioritizing syntactic validity
18
    via the Abstract Syntax Tree (AST) parser for maximum confidence.
19

20
    Indicators used:
21
      - File extension check for path inputs (e.g., .py, .pyi, .pyx, .pyw).
22
      - Shebang line detection (e.g., "#!/usr/bin/python").
23
      - Definitive syntax check using Python's `ast.parse()`.
24
      - Fallback heuristic via Pygments lexer guessing.
25

26
    Note:
27
        The function is definitive for complete, syntactically correct code blocks.
28
        It falls back to a Pygments heuristic only for short, incomplete, or
29
        ambiguous code snippets that fail AST parsing.
30

31
    Args:
32
        source: raw code string or Path to source file to check.
33

34
    Returns:
35
        True if the source is written in Python.
36
    """
37
    # Path-based check
38
    if isinstance(source, Path) or (isinstance(source, str) and is_path_like(source)):
4✔
39
        path = Path(source)
×
40
        return path.suffix.lower() in {".py", ".pyi", ".pyx", ".pyw"}
×
41

42
    # Shebang line check
43
    if re.match(r"#!/usr/bin/(env\s+)?python", source.strip()):
4✔
44
        return True
×
45

46
    # Definitive syntactic check
47
    # If parsing succeeds, it's definitely Python code
48
    try:
4✔
49
        ast.parse(source)
4✔
50
        return True
4✔
51
    except Exception:  # noqa: S110
4✔
52
        # If fails, it might still be Python code (e.g., incomplete snippet), so continue with heuristics
53
        pass
4✔
54

55
    # Pygments heuristic (Lowest confidence, last resort)
56
    try:
4✔
57
        lexer = guess_lexer(source)
4✔
58
        return lexer.name.lower() == "python"
4✔
59
    except ClassNotFound:
×
60
        return False
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc