mypy: Fix strict-optional in analytics.

This commit is contained in:
umkay 2017-05-25 17:36:54 -07:00 committed by Tim Abbott
parent 7f43ec05f2
commit d9b23b39d3
5 changed files with 10 additions and 8 deletions

View File

@ -143,6 +143,7 @@ def do_fill_count_stat_at_hour(stat, end_time):
start_time = end_time - stat.interval start_time = end_time - stat.interval
if not isinstance(stat, LoggingCountStat): if not isinstance(stat, LoggingCountStat):
timer = time.time() 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) rows_added = stat.data_collector.pull_function(stat.property, start_time, end_time)
logger.info("%s run pull_function (%dms/%sr)" % logger.info("%s run pull_function (%dms/%sr)" %
(stat.property, (time.time()-timer)*1000, rows_added)) (stat.property, (time.time()-timer)*1000, rows_added))

View File

@ -17,7 +17,7 @@ from zerver.models import Realm, UserProfile, Stream, Message, Client, \
from datetime import datetime, timedelta from datetime import datetime, timedelta
from six.moves import zip 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): class Command(BaseCommand):
help = """Populates analytics tables with randomly generated data.""" 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) shylock = self.create_user('shylock@analytics.ds', 'Shylock', True, installation_time, realm)
def insert_fixture_data(stat, fixture_data, table): 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, end_times = time_range(last_end_time, last_end_time, stat.frequency,
len(list(fixture_data.values())[0])) len(list(fixture_data.values())[0]))
if table == RealmCount: if table == RealmCount:
@ -76,13 +76,14 @@ class Command(BaseCommand):
stat = COUNT_STATS['realm_active_humans::day'] stat = COUNT_STATS['realm_active_humans::day']
realm_data = { realm_data = {
None: self.generate_fixture_data(stat, .1, .03, 3, .5, 3, partial_sum=True), 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) insert_fixture_data(stat, realm_data, RealmCount)
FillState.objects.create(property=stat.property, end_time=last_end_time, FillState.objects.create(property=stat.property, end_time=last_end_time,
state=FillState.DONE) state=FillState.DONE)
stat = COUNT_STATS['messages_sent:is_bot:hour'] 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) insert_fixture_data(stat, user_data, UserCount)
realm_data = {'false': self.generate_fixture_data(stat, 35, 15, 6, .6, 4), realm_data = {'false': self.generate_fixture_data(stat, 35, 15, 6, .6, 4),
'true': self.generate_fixture_data(stat, 15, 15, 3, .4, 2)} 'true': self.generate_fixture_data(stat, 15, 15, 3, .4, 2)}

View File

@ -201,7 +201,7 @@ class TestProcessCountStat(AnalyticsTestCase):
with self.assertRaises(ValueError): with self.assertRaises(ValueError):
process_count_stat(stat, installation_epoch() + 65*self.MINUTE) process_count_stat(stat, installation_epoch() + 65*self.MINUTE)
with self.assertRaises(ValueError): 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. # 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 # It is important that do_delete_counts_at_hour not delete any of the collected

View File

@ -19,7 +19,7 @@ import mock
import ujson import ujson
from six.moves import range from six.moves import range
from typing import List, Dict from typing import List, Dict, Optional
class TestStatsEndpoint(ZulipTestCase): class TestStatsEndpoint(ZulipTestCase):
def test_stats(self): def test_stats(self):
@ -47,7 +47,7 @@ class TestGetChartData(ZulipTestCase):
return [0, 0, i, 0] return [0, 0, i, 0]
def insert_data(self, stat, realm_subgroups, user_subgroups): 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: if stat.frequency == CountStat.HOUR:
insert_time = self.end_times_hour[2] insert_time = self.end_times_hour[2]
fill_time = self.end_times_hour[-1] fill_time = self.end_times_hour[-1]

View File

@ -183,7 +183,7 @@ def rewrite_client_arrays(value_arrays):
return mapped_arrays return mapped_arrays
def get_time_series_by_subgroup(stat, table, key_id, end_times, subgroup_to_label, include_empty_subgroups): 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) \ queryset = table_filtered_to_id(table, key_id).filter(property=stat.property) \
.values_list('subgroup', 'end_time', 'value') .values_list('subgroup', 'end_time', 'value')
value_dicts = defaultdict(lambda: defaultdict(int)) # type: Dict[Optional[str], Dict[datetime, int]] value_dicts = defaultdict(lambda: defaultdict(int)) # type: Dict[Optional[str], Dict[datetime, int]]