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

pantsbuild / pants / 24164262159

08 Apr 2026 11:36PM UTC coverage: 85.889% (-7.0%) from 92.91%
24164262159

Pull #23238

github

web-flow
Merge 88d90acad into 5a129def6
Pull Request #23238: TypeScript: `prettier fmt` crashes with resolve in subdirectory

3 of 3 new or added lines in 1 file covered. (100.0%)

5412 existing lines in 246 files now uncovered.

72530 of 84446 relevant lines covered (85.89%)

3.13 hits per line

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

90.0
/src/python/pants/backend/codegen/thrift/thrift_parser.py
1
# Copyright 2022 Pants project contributors (see CONTRIBUTORS.md).
2
# Licensed under the Apache License, Version 2.0 (see LICENSE).
3

4
from __future__ import annotations
1✔
5

6
import re
1✔
7
from collections.abc import Mapping
1✔
8
from dataclasses import dataclass
1✔
9

10
from pants.backend.codegen.thrift.target_types import ThriftSourceField
1✔
11
from pants.engine.engine_aware import EngineAwareParameter
1✔
12
from pants.engine.internals.graph import hydrate_sources
1✔
13
from pants.engine.intrinsics import get_digest_contents
1✔
14
from pants.engine.rules import collect_rules, implicitly, rule
1✔
15
from pants.engine.target import HydrateSourcesRequest
1✔
16
from pants.util.frozendict import FrozenDict
1✔
17
from pants.util.ordered_set import FrozenOrderedSet
1✔
18

19
_QUOTE_CHAR = r"(?:'|\")"
1✔
20
# NB: We don't specify what a valid file name looks like to avoid accidentally breaking unicode.
21
_FILE_NAME = r"(.+?\.thrift)"
1✔
22
_IMPORT_REGEX = re.compile(rf"include\s+{_QUOTE_CHAR}{_FILE_NAME}{_QUOTE_CHAR}\s*")
1✔
23

24
# NB: We don't specify what a valid namespace looks like to avoid accidentally breaking unicode,
25
# but we do limit the namespace language because that is provided by Thrift.
26
_NAMESPACE_REGEX = re.compile(r"namespace\s+([a-z]+)\s+(.+)\s*")
1✔
27

28

29
@dataclass(frozen=True)
1✔
30
class ParsedThrift:
1✔
31
    imports: FrozenOrderedSet[str]
1✔
32
    # Note that Thrift only allows one namespace per language per file; later namespaces overwrite
33
    # earlier ones.
34
    namespaces: FrozenDict[str, str]
1✔
35

36

37
@dataclass(frozen=True)
1✔
38
class ParsedThriftRequest(EngineAwareParameter):
1✔
39
    sources_field: ThriftSourceField
1✔
40
    extra_namespace_directives: tuple[str, ...] = ()
1✔
41

42
    def debug_hint(self) -> str:
1✔
43
        return self.sources_field.file_path
×
44

45

46
@rule
1✔
47
async def parse_thrift_file(request: ParsedThriftRequest) -> ParsedThrift:
1✔
48
    sources = await hydrate_sources(HydrateSourcesRequest(request.sources_field), **implicitly())
1✔
49
    digest_contents = await get_digest_contents(sources.snapshot.digest)
1✔
50
    assert len(digest_contents) == 1
1✔
51

52
    file_content = digest_contents[0].content.decode()
1✔
53
    extra_namespaces: Mapping[str, str] = {}
1✔
54
    if request.extra_namespace_directives:
1✔
UNCOV
55
        for directive in request.extra_namespace_directives:
×
UNCOV
56
            extra_namespace_pattern = re.compile(rf"{directive}\s+([a-z]+)\s+(.+)\s*")
×
UNCOV
57
            extra_namespaces = {
×
58
                **extra_namespaces,
59
                **dict(extra_namespace_pattern.findall(file_content)),
60
            }
61

62
    return ParsedThrift(
1✔
63
        imports=FrozenOrderedSet(_IMPORT_REGEX.findall(file_content)),
64
        namespaces=FrozenDict({**dict(_NAMESPACE_REGEX.findall(file_content)), **extra_namespaces}),
65
    )
66

67

68
def rules():
1✔
69
    return collect_rules()
1✔
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