From d9b23b39d3290f218a1dd478520737ec2749d3c6 Mon Sep 17 00:00:00 2001 From: umkay Date: Thu, 25 May 2017 17:36:54 -0700 Subject: [PATCH] mypy: Fix strict-optional in analytics. --- analytics/lib/counts.py | 1 + analytics/management/commands/populate_analytics_db.py | 9 +++++---- analytics/tests/test_counts.py | 2 +- analytics/tests/test_views.py | 4 ++-- analytics/views.py | 2 +- 5 files changed, 10 insertions(+), 8 deletions(-) diff --git a/analytics/lib/counts.py b/analytics/lib/counts.py index 9ac30c6407..72852becfa 100644 --- a/analytics/lib/counts.py +++ b/analytics/lib/counts.py @@ -143,6 +143,7 @@ def do_fill_count_stat_at_hour(stat, end_time): start_time = end_time - stat.interval if not isinstance(stat, LoggingCountStat): timer = time.time() + assert(stat.data_collector.pull_function is not None) rows_added = stat.data_collector.pull_function(stat.property, start_time, end_time) logger.info("%s run pull_function (%dms/%sr)" % (stat.property, (time.time()-timer)*1000, rows_added)) diff --git a/analytics/management/commands/populate_analytics_db.py b/analytics/management/commands/populate_analytics_db.py index 5e8e0d2b33..32baacc3c0 100644 --- a/analytics/management/commands/populate_analytics_db.py +++ b/analytics/management/commands/populate_analytics_db.py @@ -17,7 +17,7 @@ from zerver.models import Realm, UserProfile, Stream, Message, Client, \ from datetime import datetime, timedelta from six.moves import zip -from typing import Any, Dict, List, Optional, Text, Type, Union +from typing import Any, Dict, List, Optional, Text, Type, Union, Mapping class Command(BaseCommand): help = """Populates analytics tables with randomly generated data.""" @@ -60,7 +60,7 @@ class Command(BaseCommand): shylock = self.create_user('shylock@analytics.ds', 'Shylock', True, installation_time, realm) def insert_fixture_data(stat, fixture_data, table): - # type: (CountStat, Dict[Optional[str], List[int]], Type[BaseCount]) -> None + # type: (CountStat, Mapping[Optional[str], List[int]], Type[BaseCount]) -> None end_times = time_range(last_end_time, last_end_time, stat.frequency, len(list(fixture_data.values())[0])) if table == RealmCount: @@ -76,13 +76,14 @@ class Command(BaseCommand): stat = COUNT_STATS['realm_active_humans::day'] realm_data = { None: self.generate_fixture_data(stat, .1, .03, 3, .5, 3, partial_sum=True), - } # type: Dict[Optional[str], List[int]] + } # type: Mapping[Optional[str], List[int]] insert_fixture_data(stat, realm_data, RealmCount) FillState.objects.create(property=stat.property, end_time=last_end_time, state=FillState.DONE) stat = COUNT_STATS['messages_sent:is_bot:hour'] - user_data = {'false': self.generate_fixture_data(stat, 2, 1, 1.5, .6, 8, holiday_rate=.1)} + user_data = {'false': self.generate_fixture_data( + stat, 2, 1, 1.5, .6, 8, holiday_rate=.1)} # type: Mapping[Optional[str], List[int]] insert_fixture_data(stat, user_data, UserCount) realm_data = {'false': self.generate_fixture_data(stat, 35, 15, 6, .6, 4), 'true': self.generate_fixture_data(stat, 15, 15, 3, .4, 2)} diff --git a/analytics/tests/test_counts.py b/analytics/tests/test_counts.py index d74a66e033..cf17edb0a8 100644 --- a/analytics/tests/test_counts.py +++ b/analytics/tests/test_counts.py @@ -201,7 +201,7 @@ class TestProcessCountStat(AnalyticsTestCase): with self.assertRaises(ValueError): process_count_stat(stat, installation_epoch() + 65*self.MINUTE) with self.assertRaises(ValueError): - process_count_stat(stat, installation_epoch().replace(tzinfo=None) + self.HOUR) + process_count_stat(stat, installation_epoch().replace(tzinfo=None) + self.HOUR) # type: ignore # https://github.com/python/typeshed/pull/1347 # This tests the LoggingCountStat branch of the code in do_delete_counts_at_hour. # It is important that do_delete_counts_at_hour not delete any of the collected diff --git a/analytics/tests/test_views.py b/analytics/tests/test_views.py index b8a213466a..bde11b2620 100644 --- a/analytics/tests/test_views.py +++ b/analytics/tests/test_views.py @@ -19,7 +19,7 @@ import mock import ujson from six.moves import range -from typing import List, Dict +from typing import List, Dict, Optional class TestStatsEndpoint(ZulipTestCase): def test_stats(self): @@ -47,7 +47,7 @@ class TestGetChartData(ZulipTestCase): return [0, 0, i, 0] def insert_data(self, stat, realm_subgroups, user_subgroups): - # type: (CountStat, List[str], List[str]) -> None + # type: (CountStat, List[Optional[str]], List[str]) -> None if stat.frequency == CountStat.HOUR: insert_time = self.end_times_hour[2] fill_time = self.end_times_hour[-1] diff --git a/analytics/views.py b/analytics/views.py index e5b26c501d..0c4788dd70 100644 --- a/analytics/views.py +++ b/analytics/views.py @@ -183,7 +183,7 @@ def rewrite_client_arrays(value_arrays): return mapped_arrays def get_time_series_by_subgroup(stat, table, key_id, end_times, subgroup_to_label, include_empty_subgroups): - # type: (CountStat, Type[BaseCount], Optional[int], List[datetime], Dict[Optional[str], str], bool) -> Dict[str, List[int]] + # type: (CountStat, Type[BaseCount], int, List[datetime], Dict[Optional[str], str], bool) -> Dict[str, List[int]] queryset = table_filtered_to_id(table, key_id).filter(property=stat.property) \ .values_list('subgroup', 'end_time', 'value') value_dicts = defaultdict(lambda: defaultdict(int)) # type: Dict[Optional[str], Dict[datetime, int]]