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

pantsbuild / pants / 18252174847

05 Oct 2025 01:36AM UTC coverage: 43.382% (-36.9%) from 80.261%
18252174847

push

github

web-flow
run tests on mac arm (#22717)

Just doing the minimal to pull forward the x86_64 pattern.

ref #20993

25776 of 59416 relevant lines covered (43.38%)

1.3 hits per line

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

0.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
×
5

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

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

19
_QUOTE_CHAR = r"(?:'|\")"
×
20
# NB: We don't specify what a valid file name looks like to avoid accidentally breaking unicode.
21
_FILE_NAME = r"(.+?\.thrift)"
×
22
_IMPORT_REGEX = re.compile(rf"include\s+{_QUOTE_CHAR}{_FILE_NAME}{_QUOTE_CHAR}\s*")
×
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*")
×
27

28

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

36

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

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

45

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

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

62
    return ParsedThrift(
×
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():
×
69
    return collect_rules()
×
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

© 2025 Coveralls, Inc