From 4beec5c6b9122f36fdc85801d26fee619fb228ae Mon Sep 17 00:00:00 2001 From: Wyatt Hoodes Date: Tue, 30 Jul 2019 08:58:48 -1000 Subject: [PATCH] typing: Use TYPE_CHECKING when dealing with cyclic dependencies. --- tools/coveragerc | 2 ++ zerver/lib/cache.py | 5 +++-- zerver/lib/test_helpers.py | 4 ++-- zerver/tornado/descriptors.py | 4 ++-- 4 files changed, 9 insertions(+), 6 deletions(-) diff --git a/tools/coveragerc b/tools/coveragerc index 30ea2a067f..cb97c4ef4d 100644 --- a/tools/coveragerc +++ b/tools/coveragerc @@ -13,6 +13,8 @@ exclude_lines = def __str__[(]self[)] -> .*: # Don't require coverage for errors about unsupported webhook event types raise UnexpectedWebhookEventType + # Don't require coverage for blocks only run when type-checking + if TYPE_CHECKING: [run] omit = diff --git a/zerver/lib/cache.py b/zerver/lib/cache.py index d5b511ff7e..d2d7f3d27c 100644 --- a/zerver/lib/cache.py +++ b/zerver/lib/cache.py @@ -9,7 +9,8 @@ from django.db.models import Q from django.core.cache.backends.base import BaseCache from django.http import HttpRequest -from typing import Any, Callable, Dict, Iterable, List, Optional, TypeVar, Tuple +from typing import Any, Callable, Dict, Iterable, List, \ + Optional, TypeVar, Tuple, TYPE_CHECKING from zerver.lib.utils import statsd, statsd_key, make_safe_digest import time @@ -19,7 +20,7 @@ import sys import os import hashlib -if False: +if TYPE_CHECKING: # These modules have to be imported for type annotations but # they cannot be imported at runtime due to cyclic dependency. from zerver.models import UserProfile, Realm, Message diff --git a/zerver/lib/test_helpers.py b/zerver/lib/test_helpers.py index 7e6b4a4854..d75f18def8 100644 --- a/zerver/lib/test_helpers.py +++ b/zerver/lib/test_helpers.py @@ -1,7 +1,7 @@ from contextlib import contextmanager from typing import ( Any, Callable, Dict, Generator, Iterable, Iterator, List, Mapping, - Optional, Tuple, Union, IO, TypeVar + Optional, Tuple, Union, IO, TypeVar, TYPE_CHECKING ) from django.core import signing @@ -38,7 +38,7 @@ from zerver.models import ( UserProfile, ) -if False: +if TYPE_CHECKING: # Avoid an import cycle; we only need these for type annotations. from zerver.lib.test_classes import ZulipTestCase, MigrationsTestCase diff --git a/zerver/tornado/descriptors.py b/zerver/tornado/descriptors.py index a911b92a9d..032cd19d25 100644 --- a/zerver/tornado/descriptors.py +++ b/zerver/tornado/descriptors.py @@ -1,6 +1,6 @@ -from typing import Dict, Optional +from typing import Dict, Optional, TYPE_CHECKING -if False: +if TYPE_CHECKING: from zerver.tornado.event_queue import ClientDescriptor descriptors_by_handler_id = {} # type: Dict[int, ClientDescriptor]