Skip to content

Commit 4f194d0

Browse files
grievejiafacebook-github-bot
authored andcommitted
Implement pyre statistics (7/9) -- aggregate stats
Reviewed By: dkgi Differential Revision: D30414289 fbshipit-source-id: 165480c7bee7c70f19e1606daaeb91ef1e04b7c2
1 parent da82d6f commit 4f194d0

File tree

2 files changed

+129
-0
lines changed

2 files changed

+129
-0
lines changed

client/commands/v2/statistics.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,42 @@ def collect_statistics(sources: Sequence[Path], strict_default: bool) -> Statist
151151
)
152152

153153

154+
@dataclasses.dataclass(frozen=True)
155+
class AggregatedStatisticsData:
156+
annotations: Dict[str, int] = dataclasses.field(default_factory=dict)
157+
fixmes: int = 0
158+
ignores: int = 0
159+
strict: int = 0
160+
unsafe: int = 0
161+
162+
163+
def aggregate_statistics(data: StatisticsData) -> AggregatedStatisticsData:
164+
aggregate_annotations = {
165+
"return_count": 0,
166+
"annotated_return_count": 0,
167+
"globals_count": 0,
168+
"annotated_globals_count": 0,
169+
"parameter_count": 0,
170+
"annotated_parameter_count": 0,
171+
"attribute_count": 0,
172+
"annotated_attribute_count": 0,
173+
"partially_annotated_function_count": 0,
174+
"fully_annotated_function_count": 0,
175+
"line_count": 0,
176+
}
177+
for annotation_data in data.annotations.values():
178+
for key in aggregate_annotations.keys():
179+
aggregate_annotations[key] += annotation_data[key]
180+
181+
return AggregatedStatisticsData(
182+
annotations=aggregate_annotations,
183+
fixmes=sum(len(fixmes) for fixmes in data.fixmes.values()),
184+
ignores=sum(len(ignores) for ignores in data.ignores.values()),
185+
strict=sum(strictness["strict_count"] for strictness in data.strict.values()),
186+
unsafe=sum(strictness["unsafe_count"] for strictness in data.strict.values()),
187+
)
188+
189+
154190
def run_statistics(
155191
configuration: configuration_module.Configuration,
156192
statistics_arguments: command_arguments.StatisticsArguments,

client/commands/v2/tests/statistics_test.py

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
parse_text_to_module,
1818
parse_path_to_module,
1919
collect_statistics,
20+
aggregate_statistics,
21+
AggregatedStatisticsData,
2022
)
2123

2224

@@ -154,3 +156,94 @@ def test_collect_statistics(self) -> None:
154156
self.assertIn(str(bar_path), data.fixmes)
155157
self.assertIn(str(bar_path), data.ignores)
156158
self.assertIn(str(bar_path), data.strict)
159+
160+
def test_aggregate_statistics__single_file(self) -> None:
161+
with tempfile.TemporaryDirectory() as root:
162+
root_path = Path(root)
163+
a_path = root_path / "a.py"
164+
a_path.write_text(
165+
textwrap.dedent(
166+
"""
167+
# pyre-unsafe
168+
169+
def foo():
170+
return 1
171+
""".rstrip()
172+
)
173+
)
174+
175+
self.assertEqual(
176+
aggregate_statistics(
177+
collect_statistics([a_path], strict_default=False)
178+
),
179+
AggregatedStatisticsData(
180+
annotations={
181+
"return_count": 1,
182+
"annotated_return_count": 0,
183+
"globals_count": 0,
184+
"annotated_globals_count": 0,
185+
"parameter_count": 0,
186+
"annotated_parameter_count": 0,
187+
"attribute_count": 0,
188+
"annotated_attribute_count": 0,
189+
"partially_annotated_function_count": 0,
190+
"fully_annotated_function_count": 0,
191+
"line_count": 6,
192+
},
193+
fixmes=0,
194+
ignores=0,
195+
strict=0,
196+
unsafe=1,
197+
),
198+
)
199+
200+
def test_aggregate_statistics__multiple_files(self) -> None:
201+
with tempfile.TemporaryDirectory() as root:
202+
root_path = Path(root)
203+
a_path = root_path / "a.py"
204+
b_path = root_path / "b.py"
205+
a_path.write_text(
206+
textwrap.dedent(
207+
"""
208+
# pyre-unsafe
209+
210+
def foo():
211+
return 1
212+
""".rstrip()
213+
)
214+
)
215+
b_path.write_text(
216+
textwrap.dedent(
217+
"""
218+
# pyre-strict
219+
220+
def foo(x: int) -> int:
221+
return 1
222+
""".rstrip()
223+
)
224+
)
225+
226+
self.assertEqual(
227+
aggregate_statistics(
228+
collect_statistics([a_path, b_path], strict_default=False)
229+
),
230+
AggregatedStatisticsData(
231+
annotations={
232+
"return_count": 2,
233+
"annotated_return_count": 1,
234+
"globals_count": 0,
235+
"annotated_globals_count": 0,
236+
"parameter_count": 1,
237+
"annotated_parameter_count": 1,
238+
"attribute_count": 0,
239+
"annotated_attribute_count": 0,
240+
"partially_annotated_function_count": 0,
241+
"fully_annotated_function_count": 1,
242+
"line_count": 12,
243+
},
244+
fixmes=0,
245+
ignores=0,
246+
strict=1,
247+
unsafe=1,
248+
),
249+
)

0 commit comments

Comments
 (0)