From b8e7369dee52bc1938140c7d8a7d33afdeef8c2e Mon Sep 17 00:00:00 2001 From: Tim Abbott Date: Fri, 25 Aug 2017 11:01:20 -0700 Subject: [PATCH] mypy: Remove type: ignores not needed in Python 3. --- scripts/setup/generate_secrets.py | 2 +- tools/show-profile-results.py | 2 +- tools/test-backend | 2 +- tools/test-tools | 2 +- tools/tests/test_css_parser.py | 16 ------------- tools/tests/test_template_parser.py | 8 ------- zerver/lib/cache.py | 2 +- zerver/lib/exceptions.py | 4 ++-- zerver/lib/test_helpers.py | 2 +- zerver/lib/test_runner.py | 6 ++--- zerver/storage.py | 4 ++-- zerver/tests/test_narrow.py | 4 ++-- zerver/tests/test_push_notifications.py | 2 +- zerver/tests/test_type_debug.py | 2 +- zerver/tornado/application.py | 2 +- zerver/tornado/event_queue.py | 2 +- zerver/tornado/ioloop_logging.py | 31 +++++++++---------------- zproject/jinja2/__init__.py | 2 +- 18 files changed, 31 insertions(+), 64 deletions(-) diff --git a/scripts/setup/generate_secrets.py b/scripts/setup/generate_secrets.py index cc1d0ba7b6..3dfb6f0cec 100755 --- a/scripts/setup/generate_secrets.py +++ b/scripts/setup/generate_secrets.py @@ -61,7 +61,7 @@ def get_old_conf(output_filename): secrets_file = six.moves.configparser.RawConfigParser() secrets_file.read(output_filename) - return dict(secrets_file.items("secrets")) # type: ignore # likely typeshed issue + return dict(secrets_file.items("secrets")) def generate_secrets(development=False): # type: (bool) -> None diff --git a/tools/show-profile-results.py b/tools/show-profile-results.py index 9a9be06bce..39c9b5574d 100755 --- a/tools/show-profile-results.py +++ b/tools/show-profile-results.py @@ -19,6 +19,6 @@ except IndexError: ''') sys.exit(1) -p = pstats.Stats(fn) # type: ignore # stats stubs are broken +p = pstats.Stats(fn) p.strip_dirs().sort_stats('cumulative').print_stats(25) # type: ignore # stats stubs are broken p.strip_dirs().sort_stats('time').print_stats(25) # type: ignore # stats stubs are broken diff --git a/tools/test-backend b/tools/test-backend index ef49ec2a85..ee9a777b7a 100755 --- a/tools/test-backend +++ b/tools/test-backend @@ -150,7 +150,7 @@ def block_internet(): "https://zulip.readthedocs.io/en/latest/testing.html#internet-access-inside-test-suits") httplib2.Http.request = internet_guard - requests.request = internet_guard # type: ignore # mypy bug; see our #6017 + requests.request = internet_guard if __name__ == "__main__": block_internet() diff --git a/tools/test-tools b/tools/test-tools index 92f9e441ce..542576c662 100755 --- a/tools/test-tools +++ b/tools/test-tools @@ -38,7 +38,7 @@ if __name__ == '__main__': suite = loader.discover(start_dir=tools_test_dir, top_level_dir=root_dir) runner = unittest.TextTestRunner(verbosity=2) - result = runner.run(suite) # type: ignore # https://github.com/python/typeshed/issues/372 + result = runner.run(suite) if result.errors or result.failures: raise Exception('Test failed!') diff --git a/tools/tests/test_css_parser.py b/tools/tests/test_css_parser.py index b29ebdca42..852168393a 100644 --- a/tools/tests/test_css_parser.py +++ b/tools/tests/test_css_parser.py @@ -19,14 +19,6 @@ except ImportError: sys.exit(1) class ParserTestHappyPath(unittest.TestCase): - def __init__(self, *args, **kwargs): - # type: (*Any, **Any) -> None - # This method should be removed when we migrate to version 3 of Python - import six - if six.PY2: - self.assertRaisesRegex = self.assertRaisesRegexp # type: ignore - super(ParserTestHappyPath, self).__init__(*args, **kwargs) - def test_basic_parse(self): # type: () -> None my_selector = 'li.foo' @@ -170,14 +162,6 @@ class ParserTestSadPath(unittest.TestCase): of selectors. Some of this is just for expediency; some of this is to enforce consistent formatting. ''' - def __init__(self, *args, **kwargs): - # type: (*Any, **Any) -> None - # This method should be removed when we migrate to version 3 of Python - import six - if six.PY2: - self.assertRaisesRegex = self.assertRaisesRegexp # type: ignore - super(ParserTestSadPath, self).__init__(*args, **kwargs) - def _assert_error(self, my_css, error): # type: (str, str) -> None with self.assertRaisesRegex(CssParserException, error): diff --git a/tools/tests/test_template_parser.py b/tools/tests/test_template_parser.py index 10a5b6dc5b..b67d566285 100644 --- a/tools/tests/test_template_parser.py +++ b/tools/tests/test_template_parser.py @@ -18,14 +18,6 @@ except ImportError: sys.exit(1) class ParserTest(unittest.TestCase): - def __init__(self, *args, **kwargs): - # type: (*Any, **Any) -> None - # This method should be removed when we migrate to version 3 of Python - import six - if six.PY2: - self.assertRaisesRegex = self.assertRaisesRegexp # type: ignore - super(ParserTest, self).__init__(*args, **kwargs) - def _assert_validate_error(self, error, fn=None, text=None, check_indent=True): # type: (str, Optional[str], Optional[str], bool) -> None with self.assertRaisesRegex(TemplateParserException, error): diff --git a/zerver/lib/cache.py b/zerver/lib/cache.py index 4e57beefd6..90ef6ebe84 100644 --- a/zerver/lib/cache.py +++ b/zerver/lib/cache.py @@ -287,7 +287,7 @@ def cache(func): Uses a key based on the function's name, filename, and the repr() of its arguments.""" - func_uniqifier = '%s-%s' % (func.__code__.co_filename, func.__name__) # type: ignore # https://github.com/python/mypy/issues/1923 + func_uniqifier = '%s-%s' % (func.__code__.co_filename, func.__name__) @wraps(func) def keyfunc(*args, **kwargs): diff --git a/zerver/lib/exceptions.py b/zerver/lib/exceptions.py index ceb1ca4d6f..e28c5e420c 100644 --- a/zerver/lib/exceptions.py +++ b/zerver/lib/exceptions.py @@ -10,7 +10,7 @@ class AbstractEnum(Enum): def __new__(cls): # type: (Type[AbstractEnum]) -> AbstractEnum obj = object.__new__(cls) - obj._value_ = len(cls.__members__) + 1 # type: ignore # typeshed enum missing Enum.__members__ + obj._value_ = len(cls.__members__) + 1 return obj # Override all the `Enum` methods that use `_value_`. @@ -144,7 +144,7 @@ class JsonableError(Exception): def __str__(self): # type: () -> str - return self.msg # type: ignore # remove once py3-only + return self.msg class RateLimited(PermissionDenied): def __init__(self, msg=""): diff --git a/zerver/lib/test_helpers.py b/zerver/lib/test_helpers.py index 2434c3d37a..184fbc0b49 100644 --- a/zerver/lib/test_helpers.py +++ b/zerver/lib/test_helpers.py @@ -180,7 +180,7 @@ def stdout_suppressed(): """Redirect stdout to /dev/null.""" with open(os.devnull, 'a') as devnull: - stdout, sys.stdout = sys.stdout, devnull # type: ignore # monkey-patching + stdout, sys.stdout = sys.stdout, devnull yield stdout sys.stdout = stdout diff --git a/zerver/lib/test_runner.py b/zerver/lib/test_runner.py index edbf718dac..3365991084 100644 --- a/zerver/lib/test_runner.py +++ b/zerver/lib/test_runner.py @@ -256,7 +256,7 @@ def run_subsuite(args): # type of Partial is different from Callable. All the methods of # TestResult are passed TestCase as the first argument but # addInstrumentation does not need it. - process_instrumented_calls(partial(result.addInstrumentation, None)) # type: ignore + process_instrumented_calls(partial(result.addInstrumentation, None)) return subsuite_index, result.events # Monkey-patch database creation to fix unnecessary sleep(1) @@ -365,7 +365,7 @@ class TestSuite(unittest.TestSuite): if getattr(result, '_testRunEntered', False) is False: result._testRunEntered = topLevel = True - for test in self: # type: ignore # Mypy cannot recognize this + for test in self: # but this is correct. Taken from unittest. if result.shouldStop: break @@ -503,7 +503,7 @@ def get_test_names(suite): def get_tests_from_suite(suite): # type: (TestSuite) -> TestCase - for test in suite: # type: ignore + for test in suite: if isinstance(test, TestSuite): for child in get_tests_from_suite(test): yield child diff --git a/zerver/storage.py b/zerver/storage.py index de4b5b638e..bc546b1011 100644 --- a/zerver/storage.py +++ b/zerver/storage.py @@ -43,7 +43,7 @@ class AddHeaderMixin(object): ret_dict[path] = (path, path, True) - super_class = super(AddHeaderMixin, self) # type: ignore # https://github.com/JukkaL/mypy/issues/857 + super_class = super(AddHeaderMixin, self) if hasattr(super_class, 'post_process'): super_ret = super_class.post_process(paths, dry_run, **kwargs) # type: ignore # https://github.com/python/mypy/issues/2956 else: @@ -73,7 +73,7 @@ class RemoveUnminifiedFilesMixin(object): is_valid = lambda p: all([not p.startswith(k) for k in to_remove]) paths = {k: v for k, v in paths.items() if is_valid(k)} - super_class = super(RemoveUnminifiedFilesMixin, self) # type: ignore # https://github.com/JukkaL/mypy/issues/857 + super_class = super(RemoveUnminifiedFilesMixin, self) if hasattr(super_class, 'post_process'): return super_class.post_process(paths, dry_run, **kwargs) # type: ignore # https://github.com/python/mypy/issues/2956 diff --git a/zerver/tests/test_narrow.py b/zerver/tests/test_narrow.py index 7c17f127a4..a1a75d4c8a 100644 --- a/zerver/tests/test_narrow.py +++ b/zerver/tests/test_narrow.py @@ -8,7 +8,7 @@ from django.test import override_settings from sqlalchemy.sql import ( and_, select, column, table, ) -from sqlalchemy.sql import compiler # type: ignore +from sqlalchemy.sql import compiler from zerver.models import ( Realm, Recipient, Stream, Subscription, UserProfile, Attachment, @@ -50,7 +50,7 @@ import ujson def get_sqlalchemy_query_params(query): # type: (Text) -> Dict[Text, Text] - dialect = get_sqlalchemy_connection().dialect # type: ignore + dialect = get_sqlalchemy_connection().dialect comp = compiler.SQLCompiler(dialect, query) return comp.params diff --git a/zerver/tests/test_push_notifications.py b/zerver/tests/test_push_notifications.py index cb94d88224..5ae6c27cb0 100644 --- a/zerver/tests/test_push_notifications.py +++ b/zerver/tests/test_push_notifications.py @@ -256,7 +256,7 @@ class PushNotificationTest(BouncerTestCase): super(PushNotificationTest, self).setUp() self.user_profile = self.example_user('hamlet') apn.connection = apn.get_connection('fake-cert', 'fake-key') - self.redis_client = apn.redis_client = MockRedis() # type: ignore + self.redis_client = apn.redis_client = MockRedis() self.tokens = [u'aaaa', u'bbbb'] for token in self.tokens: PushDeviceToken.objects.create( diff --git a/zerver/tests/test_type_debug.py b/zerver/tests/test_type_debug.py index 412ea411db..aaa251bf0a 100644 --- a/zerver/tests/test_type_debug.py +++ b/zerver/tests/test_type_debug.py @@ -49,7 +49,7 @@ class TypesPrintTest(TestCase): def empty_func(): # type: () -> None pass - self.check_signature("empty_func() -> None", None, empty_func) # type: ignore # https://github.com/python/mypy/issues/1932 + self.check_signature("empty_func() -> None", None, empty_func) self.check_signature("() -> None", None, (lambda: None)) # type: ignore # https://github.com/python/mypy/issues/1932 def test_basic(self): diff --git a/zerver/tornado/application.py b/zerver/tornado/application.py index b38281e7b2..a3ece857bf 100644 --- a/zerver/tornado/application.py +++ b/zerver/tornado/application.py @@ -18,7 +18,7 @@ def setup_tornado_rabbitmq(): if settings.USING_RABBITMQ: queue_client = get_queue_client() atexit.register(lambda: queue_client.close()) - tornado.autoreload.add_reload_hook(lambda: queue_client.close()) # type: ignore # TODO: Fix missing tornado.autoreload stub + tornado.autoreload.add_reload_hook(lambda: queue_client.close()) def create_tornado_application(): # type: () -> tornado.web.Application diff --git a/zerver/tornado/event_queue.py b/zerver/tornado/event_queue.py index be3c686324..7c80d98e95 100644 --- a/zerver/tornado/event_queue.py +++ b/zerver/tornado/event_queue.py @@ -473,7 +473,7 @@ def setup_event_queue(): atexit.register(dump_event_queues) # Make sure we dump event queues even if we exit via signal signal.signal(signal.SIGTERM, lambda signum, stack: sys.exit(1)) - tornado.autoreload.add_reload_hook(dump_event_queues) # type: ignore # TODO: Fix missing tornado.autoreload stub + tornado.autoreload.add_reload_hook(dump_event_queues) try: os.rename(settings.JSON_PERSISTENT_QUEUE_FILENAME, "/var/tmp/event_queues.json.last") diff --git a/zerver/tornado/ioloop_logging.py b/zerver/tornado/ioloop_logging.py index c081d22478..bec7b29732 100644 --- a/zerver/tornado/ioloop_logging.py +++ b/zerver/tornado/ioloop_logging.py @@ -8,28 +8,19 @@ import select from tornado import ioloop from django.conf import settings -try: - # Tornado 2.4 - orig_poll_impl = ioloop._poll # type: ignore # cross-version type variation is hard for mypy +from tornado.ioloop import IOLoop, PollIOLoop +# There isn't a good way to get at what the underlying poll implementation +# will be without actually constructing an IOLoop, so we just assume it will +# be epoll. +orig_poll_impl = select.epoll - def instrument_tornado_ioloop(): - # type: () -> None - ioloop._poll = InstrumentedPoll # type: ignore # cross-version type variation is hard for mypy -except Exception: - # Tornado 3 - from tornado.ioloop import IOLoop, PollIOLoop - # There isn't a good way to get at what the underlying poll implementation - # will be without actually constructing an IOLoop, so we just assume it will - # be epoll. - orig_poll_impl = select.epoll +class InstrumentedPollIOLoop(PollIOLoop): + def initialize(self, **kwargs): # type: ignore # TODO investigate likely buggy monkey patching here + super(InstrumentedPollIOLoop, self).initialize(impl=InstrumentedPoll(), **kwargs) - class InstrumentedPollIOLoop(PollIOLoop): - def initialize(self, **kwargs): # type: ignore # TODO investigate likely buggy monkey patching here - super(InstrumentedPollIOLoop, self).initialize(impl=InstrumentedPoll(), **kwargs) - - def instrument_tornado_ioloop(): - # type: () -> None - IOLoop.configure(InstrumentedPollIOLoop) +def instrument_tornado_ioloop(): + # type: () -> None + IOLoop.configure(InstrumentedPollIOLoop) # A hack to keep track of how much time we spend working, versus sleeping in # the event loop. diff --git a/zproject/jinja2/__init__.py b/zproject/jinja2/__init__.py index 462837a799..9ace126ac4 100644 --- a/zproject/jinja2/__init__.py +++ b/zproject/jinja2/__init__.py @@ -24,7 +24,7 @@ def environment(**options): 'minified_js': minified_js, }) - env.install_gettext_translations(translation, True) # type: ignore # https://github.com/python/typeshed/issues/927 + env.install_gettext_translations(translation, True) env.filters['slugify'] = slugify env.filters['pluralize'] = pluralize