Skip to content

Commit

Permalink
Add basic support for is_generated
Browse files Browse the repository at this point in the history
Summary: Right now ig manually filters this out by manually filtering on file names, they have requested that generated code not be included in roll ups and other tools in the dashboard. This is the first step in getting that data into the pipelines and then into the tool itself. We should also be able to index on it, which should make filtering this data faster and simpler from both a code and user perspective.

Reviewed By: inseokhwang

Differential Revision: D52545219

fbshipit-source-id: cecd68dd48cd79107db81648bf6256dcd0322f2d
  • Loading branch information
Maggie Moss authored and facebook-github-bot committed Jan 4, 2024
1 parent 7700ef5 commit 095100a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
7 changes: 7 additions & 0 deletions client/coverage_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ class ModuleMode(str, Enum):
class ModuleModeInfo(json_mixins.SnakeCaseAndExcludeJsonMixin):
mode: ModuleMode
explicit_comment_line: Optional[LineNumber]
is_generated: bool


class FunctionAnnotationStatus(str, Enum):
Expand Down Expand Up @@ -461,6 +462,7 @@ class ModuleModeCollector(VisitorWithPositionData):
ignore_all_by_code_regex: Pattern[str] = compile(
r" ?#+ *pyre-ignore-all-errors\[[0-9]+[0-9, ]*\]"
)
is_generated_regex: Pattern[str] = compile(r"@" + "generated")

def __init__(self, strict_by_default: bool) -> None:
self.strict_by_default: bool = strict_by_default
Expand All @@ -471,6 +473,7 @@ def __init__(self, strict_by_default: bool) -> None:
ModuleMode.STRICT if strict_by_default else ModuleMode.UNSAFE
)
self.explicit_comment_line: Optional[int] = None
self.is_generated: bool = False

def is_strict_module(self) -> bool:
return self.mode == ModuleMode.STRICT
Expand All @@ -488,6 +491,9 @@ def visit_Comment(self, node: libcst.Comment) -> None:
self.mode = ModuleMode.IGNORE_ALL
self.explicit_comment_line = self.location(node).start_line

if self.is_generated_regex.search(node.value):
self.is_generated = True


def collect_mode(
module: libcst.MetadataWrapper,
Expand All @@ -500,6 +506,7 @@ def collect_mode(
return ModuleModeInfo(
mode=mode,
explicit_comment_line=visitor.explicit_comment_line,
is_generated=visitor.is_generated,
)


Expand Down
15 changes: 15 additions & 0 deletions client/tests/coverage_data_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -1051,13 +1051,16 @@ def assert_counts(
default_strict: bool,
mode: ModuleMode,
explicit_comment_line: Optional[int],
is_generated: bool = False,
) -> None:
source_module = parse_code(source)
result = coverage_data.collect_mode(source_module, default_strict)
self.assertEqual(mode, result.mode)
self.assertEqual(explicit_comment_line, result.explicit_comment_line)
self.assertEqual(is_generated, result.is_generated)

def test_strict_files(self) -> None:
generated_string = "generated"
self.assert_counts(
"""
# pyre-unsafe
Expand Down Expand Up @@ -1136,6 +1139,18 @@ def foo(x: str) -> int:
mode=ModuleMode.STRICT,
explicit_comment_line=None,
)
self.assert_counts(
f"""
# @{generated_string}
def foo(x: str) -> int:
return x
""",
default_strict=True,
mode=ModuleMode.STRICT,
explicit_comment_line=None,
is_generated=True,
)


class ModuleFindingHelpersTest(testslide.TestCase):
Expand Down

0 comments on commit 095100a

Please sign in to comment.