typing: Remove now-unnecessary conditional import.

As a result of dropping support for trusty, we can remove our old
pattern of putting `if False` before importing the typing module,
which was essential for Python 3.4 support, but not required and maybe
harmful on newer versions.

cron_file_helper
check_rabbitmq_consumers
hash_reqs
check_zephyr_mirror
check_personal_zephyr_mirrors
check_cron_file
zulip_tools
check_postgres_replication_lag
api_test_helpers
purge-old-deployments
setup_venv
node_cache
clean_venv_cache
clean_node_cache
clean_emoji_cache
pg_backup_and_purge
restore-backup
generate_secrets
zulip-ec2-configure-interfaces
diagnose
check_user_zephyr_mirror_liveness
This commit is contained in:
Wyatt Hoodes 2019-07-23 11:58:11 -10:00 committed by Tim Abbott
parent 865a7204f9
commit a109508e34
23 changed files with 25 additions and 116 deletions

View File

@ -95,36 +95,6 @@ an `Any` or `# type: ignore` so you're not blocked waiting for help,
add a `# TODO: ` comment so it doesn't get forgotten in code review, add a `# TODO: ` comment so it doesn't get forgotten in code review,
and ask for help in chat.zulip.org. and ask for help in chat.zulip.org.
## mypy in production scripts
While in most of the Zulip codebase, we can consistently use the
`typing` module (Part of the standard library in Python 3.5, but
present as an installable module with older Python), in our installer
and other production scripts that might run outside a Zulip
virtualenv, we cannot rely on the `typing` module being present on the
system.
To solve this problem, we use the following (semi-standard in the mypy
community) hack in those scripts:
```
if False:
# See https://zulip.readthedocs.io/en/latest/testing/mypy.html#mypy-in-production-scripts
from typing import List
```
and then use the Python 2 style type comment syntax for annotating
those files. This way, the Python interpreters for Python 2.7 and 3.4
will ignore this line, and thus not crash. But we can still get all
the benefits of type annotations in that codebase, since the `mypy`
type checker ignores the `if False` and thus still is able to
type-check the file using those imports.
The exception to this rule is that any scripts which use
`setup_path_on_import` before they import from the `typing` module are
safe. These, we generally declare in the relevant exclude line in
`tools/linter_lib/custom_check.py`
## mypy stubs for third-party modules. ## mypy stubs for third-party modules.
For the Python standard library and some popular third-party modules, For the Python standard library and some popular third-party modules,

View File

@ -4,9 +4,6 @@ file output by the cron job is correct.
""" """
import sys import sys
import time import time
if False:
# See https://zulip.readthedocs.io/en/latest/testing/mypy.html#mypy-in-production-scripts
from typing import Tuple from typing import Tuple
def nagios_from_file(results_file: str, max_time_diff: int=60 * 2) -> 'Tuple[int, str]': def nagios_from_file(results_file: str, max_time_diff: int=60 * 2) -> 'Tuple[int, str]':

View File

@ -4,13 +4,12 @@
Nagios plugin to check the difference between the primary and Nagios plugin to check the difference between the primary and
secondary Postgres servers' xlog location. secondary Postgres servers' xlog location.
""" """
if False:
# See https://zulip.readthedocs.io/en/latest/testing/mypy.html#mypy-in-production-scripts
from mypy_extensions import NoReturn
import subprocess import subprocess
import re import re
from mypy_extensions import NoReturn
states = { states = {
"OK": 0, "OK": 0,
"WARNING": 1, "WARNING": 1,

View File

@ -10,7 +10,6 @@ import dateutil.parser
import pytz import pytz
import time import time
from datetime import datetime, timedelta from datetime import datetime, timedelta
if False:
from typing import Dict, List from typing import Dict, List
logging.Formatter.converter = time.gmtime logging.Formatter.converter = time.gmtime

View File

@ -8,10 +8,7 @@ This script works by just monitoring the files under
mirrors when they receive the messages sent every minute by mirrors when they receive the messages sent every minute by
/etc/cron.d/test_zephyr_personal_mirrors /etc/cron.d/test_zephyr_personal_mirrors
""" """
if False:
# See https://zulip.readthedocs.io/en/latest/testing/mypy.html#mypy-in-production-scripts
from typing import Dict from typing import Dict
import os import os
import time import time

View File

@ -24,8 +24,6 @@ django.setup()
from zerver.models import UserActivity from zerver.models import UserActivity
if False:
# See https://zulip.readthedocs.io/en/latest/testing/mypy.html#mypy-in-production-scripts
from typing import Any, Dict, Set, Optional from typing import Any, Dict, Set, Optional
states = { states = {

View File

@ -9,10 +9,7 @@ run out of cron.
See puppet/zulip_ops/files/cron.d/zephyr-mirror for the crontab details. See puppet/zulip_ops/files/cron.d/zephyr-mirror for the crontab details.
""" """
if False:
# See https://zulip.readthedocs.io/en/latest/testing/mypy.html#mypy-in-production-scripts
from typing import Dict from typing import Dict
import os import os
import time import time

View File

@ -50,8 +50,7 @@ import subprocess
import boto.utils import boto.utils
import netifaces import netifaces
if False:
# See https://zulip.readthedocs.io/en/latest/testing/mypy.html#mypy-in-production-scripts
from typing import Optional from typing import Optional
def address_of(device_id): def address_of(device_id):

View File

@ -3,8 +3,6 @@ import argparse
import os import os
import sys import sys
if False:
# See https://zulip.readthedocs.io/en/latest/testing/mypy.html#mypy-in-production-scripts
from typing import Set from typing import Set
ZULIP_PATH = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) ZULIP_PATH = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

View File

@ -4,8 +4,6 @@ import os
import subprocess import subprocess
import sys import sys
if False:
# See https://zulip.readthedocs.io/en/latest/testing/mypy.html#mypy-in-production-scripts
from typing import Set from typing import Set
ZULIP_PATH = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) ZULIP_PATH = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

View File

@ -3,8 +3,6 @@ import argparse
import os import os
import sys import sys
if False:
# See https://zulip.readthedocs.io/en/latest/testing/mypy.html#mypy-in-production-scripts
from typing import Set from typing import Set
ZULIP_PATH = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) ZULIP_PATH = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

View File

@ -3,9 +3,6 @@ import os
import sys import sys
import argparse import argparse
import hashlib import hashlib
if False:
# See https://zulip.readthedocs.io/en/latest/testing/mypy.html#mypy-in-production-scripts
from typing import Iterable, List, MutableSet from typing import Iterable, List, MutableSet
def expand_reqs_helper(fpath, visited): def expand_reqs_helper(fpath, visited):

View File

@ -3,10 +3,7 @@ import os
import hashlib import hashlib
import json import json
if False:
# See https://zulip.readthedocs.io/en/latest/testing/mypy.html#mypy-in-production-scripts
from typing import Optional, List, IO, Any from typing import Optional, List, IO, Any
from scripts.lib.zulip_tools import subprocess_text_output, run from scripts.lib.zulip_tools import subprocess_text_output, run
ZULIP_PATH = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) ZULIP_PATH = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

View File

@ -7,6 +7,8 @@ import sys
from scripts.lib.zulip_tools import run, run_as_root, ENDC, WARNING from scripts.lib.zulip_tools import run, run_as_root, ENDC, WARNING
from scripts.lib.hash_reqs import expand_reqs from scripts.lib.hash_reqs import expand_reqs
from typing import List, Optional, Tuple, Set
ZULIP_PATH = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) ZULIP_PATH = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
VENV_CACHE_PATH = "/srv/zulip-venv-cache" VENV_CACHE_PATH = "/srv/zulip-venv-cache"
@ -14,10 +16,6 @@ if 'TRAVIS' in os.environ:
# In Travis CI, we don't have root access # In Travis CI, we don't have root access
VENV_CACHE_PATH = "/home/travis/zulip-venv-cache" VENV_CACHE_PATH = "/home/travis/zulip-venv-cache"
if False:
# See https://zulip.readthedocs.io/en/latest/testing/mypy.html#mypy-in-production-scripts
from typing import List, Optional, Tuple, Set
VENV_DEPENDENCIES = [ VENV_DEPENDENCIES = [
"build-essential", "build-essential",
"libffi-dev", "libffi-dev",

View File

@ -16,8 +16,6 @@ import json
import uuid import uuid
import configparser import configparser
if False:
# See https://zulip.readthedocs.io/en/latest/testing/mypy.html#mypy-in-production-scripts
from typing import Sequence, Set, Any, Dict, List from typing import Sequence, Set, Any, Dict, List
DEPLOYMENTS_DIR = "/home/zulip/deployments" DEPLOYMENTS_DIR = "/home/zulip/deployments"

View File

@ -7,9 +7,6 @@ import configparser
from collections import defaultdict from collections import defaultdict
import os import os
import subprocess import subprocess
if False:
# See https://zulip.readthedocs.io/en/latest/testing/mypy.html#mypy-in-production-scripts
from typing import Dict from typing import Dict
states = { states = {

View File

@ -1,7 +1,5 @@
import time import time
if False:
# See https://zulip.readthedocs.io/en/latest/testing/mypy.html#mypy-in-production-scripts
from typing import Tuple from typing import Tuple
def nagios_from_file(results_file): def nagios_from_file(results_file):

View File

@ -4,8 +4,6 @@ import os
import subprocess import subprocess
import sys import sys
if False:
# See https://zulip.readthedocs.io/en/latest/testing/mypy.html#mypy-in-production-scripts
from typing import Set from typing import Set
ZULIP_PATH = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) ZULIP_PATH = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

View File

@ -3,8 +3,7 @@
import sys import sys
import os import os
if False:
# See https://zulip.readthedocs.io/en/latest/testing/mypy.html#mypy-in-production-scripts
from typing import Dict, List from typing import Dict, List
BASE_DIR = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) BASE_DIR = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

View File

@ -7,7 +7,6 @@ import subprocess
import sys import sys
import tempfile import tempfile
if False:
from typing import IO from typing import IO
BASE_DIR = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) BASE_DIR = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

View File

@ -6,8 +6,6 @@ import shlex
import sys import sys
import subprocess import subprocess
if False:
# See https://zulip.readthedocs.io/en/latest/testing/mypy.html#mypy-in-production-scripts
from typing import Callable, List from typing import Callable, List
TOOLS_DIR = os.path.dirname(__file__) TOOLS_DIR = os.path.dirname(__file__)

View File

@ -330,21 +330,6 @@ python_rules = RuleList(
'description': 'Most scripts are intended to run on systems without sudo.', 'description': 'Most scripts are intended to run on systems without sudo.',
'good_lines': ['subprocess.check_call(["ls"])'], 'good_lines': ['subprocess.check_call(["ls"])'],
'bad_lines': ['subprocess.check_call(["sudo", "ls"])']}, 'bad_lines': ['subprocess.check_call(["sudo", "ls"])']},
{'pattern': '^from typing import',
'strip': '\n',
'include_only': set([
'scripts/',
'puppet/',
]),
'exclude': set([
# Not important, but should fix
'scripts/lib/process-mobile-i18n',
# Uses setup_path_on_import before importing.
'puppet/zulip/files/nagios_plugins/zulip_app_frontend/check_send_receive_time',
]),
'description': 'For scripts run as part of installer, cannot rely on typing existing; use `if False` workaround.',
'good_lines': [' from typing import List'],
'bad_lines': ['from typing import List']},
{'pattern': 'django.utils.translation', {'pattern': 'django.utils.translation',
'include_only': set(['test/', 'zerver/views/development/']), 'include_only': set(['test/', 'zerver/views/development/']),
'description': 'Test strings should not be tagged for translation', 'description': 'Test strings should not be tagged for translation',
@ -798,10 +783,6 @@ markdown_rules = RuleList(
'description': 'Linkified markdown URLs should use cleaner <http://example.com> syntax.'}, 'description': 'Linkified markdown URLs should use cleaner <http://example.com> syntax.'},
{'pattern': 'https://zulip.readthedocs.io/en/latest/[a-zA-Z0-9]', {'pattern': 'https://zulip.readthedocs.io/en/latest/[a-zA-Z0-9]',
'exclude': ['docs/overview/contributing.md', 'docs/overview/readme.md', 'docs/README.md'], 'exclude': ['docs/overview/contributing.md', 'docs/overview/readme.md', 'docs/README.md'],
'exclude_line': set([
('docs/testing/mypy.md',
'# See https://zulip.readthedocs.io/en/latest/testing/mypy.html#mypy-in-production-scripts')
]),
'include_only': set(['docs/']), 'include_only': set(['docs/']),
'description': "Use relative links (../foo/bar.html) to other documents in docs/", 'description': "Use relative links (../foo/bar.html) to other documents in docs/",
}, },

View File

@ -6,7 +6,6 @@ import os
from zerver.lib import mdiff from zerver.lib import mdiff
from zerver.lib.openapi import validate_against_openapi_schema from zerver.lib.openapi import validate_against_openapi_schema
if False:
from zulip import Client from zulip import Client
ZULIP_DIR = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) ZULIP_DIR = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))