Skip to content

Commit

Permalink
python: Pre-fix a few spots for better Black formatting.
Browse files Browse the repository at this point in the history
Signed-off-by: Anders Kaseorg <anders@zulip.com>
  • Loading branch information
andersk authored and timabbott committed Sep 4, 2020
1 parent c67ea05 commit f91d287
Show file tree
Hide file tree
Showing 33 changed files with 123 additions and 97 deletions.
6 changes: 4 additions & 2 deletions confirmation/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,10 @@ def __init__(self, url_name: str,
Confirmation.INVITATION: ConfirmationType('check_prereg_key_and_redirect',
validity_in_days=settings.INVITATION_LINK_VALIDITY_DAYS),
Confirmation.EMAIL_CHANGE: ConfirmationType('zerver.views.user_settings.confirm_email_change'),
Confirmation.UNSUBSCRIBE: ConfirmationType('zerver.views.unsubscribe.email_unsubscribe',
validity_in_days=1000000), # should never expire
Confirmation.UNSUBSCRIBE: ConfirmationType(
'zerver.views.unsubscribe.email_unsubscribe',
validity_in_days=1000000, # should never expire
),
Confirmation.MULTIUSE_INVITE: ConfirmationType(
'zerver.views.registration.accounts_home_from_multiuse_invite',
validity_in_days=settings.INVITATION_LINK_VALIDITY_DAYS),
Expand Down
5 changes: 3 additions & 2 deletions scripts/nagios/check-rabbitmq-consumers
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,11 @@ output = subprocess.check_output(['/usr/sbin/rabbitmqctl', 'list_consumers'],
consumers: Dict[str, int] = defaultdict(int)

sys.path.append(os.path.join(os.path.dirname(os.path.dirname(os.path.dirname(__file__)))))
queues = set(normal_queues).union({
queues = {
*normal_queues,
# These queues may not be present if settings.TORNADO_PROCESSES > 1
'notify_tornado',
})
}

for queue_name in queues:
queue_name = queue_name.strip()
Expand Down
8 changes: 4 additions & 4 deletions scripts/setup/restore-backup
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,9 @@ def restore_backup(tarball_file: IO[bytes]) -> None:
run(
[
os.path.join(
settings.DEPLOY_ROOT, "scripts", "setup", "terminate-psql-sessions",
settings.DEPLOY_ROOT, "scripts", "setup", "terminate-psql-sessions"
),
db["NAME"],
db["NAME"]
],
env=postgres_env,
)
Expand Down Expand Up @@ -136,8 +136,8 @@ def restore_backup(tarball_file: IO[bytes]) -> None:
run(
[
os.path.join(settings.DEPLOY_ROOT, "scripts", "zulip-puppet-apply"),
"-f",
],
"-f"
]
)

# Now, restore the the database backup using pg_restore. This
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
'https://www.udemy.com/course/the-complete-react-native-and-redux-course/',
]

VNU_IGNORE = re.compile(r'|'.join([
VNU_IGNORE = [
# Real errors that should be fixed.
r'Duplicate ID “[^”]*”\.',
r'The first occurrence of ID “[^”]*” was here\.',
Expand All @@ -39,7 +39,8 @@

# Warnings that are probably less important.
r'The “type” attribute is unnecessary for JavaScript resources\.',
]))
]
VNU_IGNORE_REGEX = re.compile(r'|'.join(VNU_IGNORE))


class BaseDocumentationSpider(scrapy.Spider):
Expand Down Expand Up @@ -91,7 +92,7 @@ def _vnu_callback(self, url: str) -> Callable[[Response], None]:
def callback(response: Response) -> None:
vnu_out = json.loads(response.text)
for message in vnu_out['messages']:
if not VNU_IGNORE.fullmatch(message['message']):
if not VNU_IGNORE_REGEX.fullmatch(message['message']):
self.logger.error(
'"%s":%d.%d-%d.%d: %s: %s',
url,
Expand Down
21 changes: 5 additions & 16 deletions tools/lib/capitalization.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,15 +174,15 @@
SPLIT_BOUNDARY_REGEX = re.compile(fr'[{SPLIT_BOUNDARY}]')

# Regexes which check capitalization in sentences.
DISALLOWED_REGEXES = [re.compile(regex) for regex in [
DISALLOWED = [
r'^[a-z](?!\})', # Checks if the sentence starts with a lower case character.
r'^[A-Z][a-z]+[\sa-z0-9]+[A-Z]', # Checks if an upper case character exists
# after a lower case character when the first character is in upper case.
]]
]
DISALLOWED_REGEX = re.compile(r"|".join(DISALLOWED))

BANNED_WORDS = {
'realm': ('The term realm should not appear in user-facing strings. '
'Use organization instead.'),
'realm': 'The term realm should not appear in user-facing strings. Use organization instead.',
}

def get_safe_phrase(phrase: str) -> str:
Expand Down Expand Up @@ -232,18 +232,7 @@ def get_safe_text(text: str) -> str:

def is_capitalized(safe_text: str) -> bool:
sentences = SPLIT_BOUNDARY_REGEX.split(safe_text)
sentences = [sentence.strip()
for sentence in sentences if sentence.strip()]

if not sentences:
return False

for sentence in sentences:
for regex in DISALLOWED_REGEXES:
if regex.search(sentence):
return False

return True
return not any(DISALLOWED_REGEX.search(sentence.strip()) for sentence in sentences)

def check_banned_words(text: str) -> List[str]:
lower_cased_text = text.lower()
Expand Down
5 changes: 3 additions & 2 deletions tools/lib/gitlint-rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
'uses', 'using', 'used',
}

imperative_forms = sorted([
imperative_forms = [
'add', 'allow', 'amend', 'bump', 'calculate', 'change', 'clean', 'commit',
'correct', 'create', 'darken', 'disable', 'display', 'document', 'dry',
'end', 'enforce', 'enqueue', 'extract', 'finish', 'fix', 'format', 'guard',
Expand All @@ -81,7 +81,8 @@
'refactor', 'remove', 'rename', 'reorder', 'replace', 'require', 'restore',
'send', 'separate', 'set', 'show', 'simplify', 'skip', 'sort', 'speed',
'start', 'support', 'take', 'test', 'truncate', 'update', 'use',
])
]
imperative_forms.sort()


def head_binary_search(key: Text, words: List[str]) -> str:
Expand Down
13 changes: 8 additions & 5 deletions tools/test-backend
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import responses
from django.conf import settings
from django.test.utils import get_runner

target_fully_covered = {path for target in [
target_fully_covered = [
'analytics/lib/*.py',
'analytics/models.py',
'analytics/tests/*.py',
Expand Down Expand Up @@ -51,9 +51,9 @@ target_fully_covered = {path for target in [
# 'zerver/webhooks/*/*.py',
# 'zerver/webhooks/*/*/*.py',
'zerver/worker/*.py',
] for path in glob.glob(target)}
]

not_yet_fully_covered = {path for target in [
not_yet_fully_covered = [
# Analytics fixtures library is used to generate test fixtures;
# isn't properly accounted for in test coverage analysis since it
# runs before tests.
Expand Down Expand Up @@ -136,9 +136,12 @@ not_yet_fully_covered = {path for target in [
'zerver/webhooks/teamcity/view.py',
'zerver/webhooks/travis/view.py',
'zerver/webhooks/zapier/view.py',
] for path in glob.glob(target)}
]

enforce_fully_covered = sorted(target_fully_covered - not_yet_fully_covered)
enforce_fully_covered = sorted(
{path for target in target_fully_covered for path in glob.glob(target)}
- {path for target in not_yet_fully_covered for path in glob.glob(target)}
)

FAILED_TEST_PATH = 'var/last_test_failure.json'

Expand Down
5 changes: 2 additions & 3 deletions tools/tests/test_capitalization_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,9 @@ def test_process_text(self) -> None:

string = ""
capitalized = is_capitalized(string)
self.assertFalse(capitalized)
self.assertTrue(capitalized)

string = ("Please re-enter your password to confirm your identity."
" (Forgotten it?)")
string = "Please re-enter your password to confirm your identity. (Forgotten it?)"
capitalized = is_capitalized(string)
self.assertTrue(capitalized)

Expand Down
3 changes: 2 additions & 1 deletion zerver/data_import/import_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,8 @@ def build_recipient(type_id: int, recipient_id: int, type: int) -> ZerverFieldsT
recipient = Recipient(
type_id=type_id, # stream id
id=recipient_id,
type=type)
type=type,
)
recipient_dict = model_to_dict(recipient)
return recipient_dict

Expand Down
7 changes: 5 additions & 2 deletions zerver/lib/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -1534,7 +1534,8 @@ def do_send_messages(messages_maybe_none: Sequence[Optional[MutableMapping[str,
if message['stream'] is None:
stream_id = message['message'].recipient.type_id
message['stream'] = Stream.objects.select_related().get(id=stream_id)
assert message['stream'] is not None # assert needed because stubs for django are missing
# assert needed because stubs for django are missing
assert message['stream'] is not None
realm_id = message['stream'].realm_id

# Deliver events to the real-time push system, as well as
Expand Down Expand Up @@ -1592,7 +1593,9 @@ def do_send_messages(messages_maybe_none: Sequence[Optional[MutableMapping[str,
# notify new_message request if it's a public stream,
# ensuring that in the tornado server, non-public stream
# messages are only associated to their subscribed users.
assert message['stream'] is not None # assert needed because stubs for django are missing

# assert needed because stubs for django are missing
assert message['stream'] is not None
if message['stream'].is_public():
event['realm_id'] = message['stream'].realm_id
event['stream_name'] = message['stream'].name
Expand Down
3 changes: 1 addition & 2 deletions zerver/lib/hotspots.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@
},
'intro_gear': {
'title': _('Settings'),
'description': _('Go to Settings to configure your '
'notifications and display settings.'),
'description': _('Go to Settings to configure your notifications and display settings.'),
},
'intro_compose': {
'title': _('Compose'),
Expand Down
3 changes: 1 addition & 2 deletions zerver/lib/markdown/fenced_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -405,8 +405,7 @@ def format_quote(self, text: str) -> str:
def format_spoiler(self, header: str, text: str) -> str:
output = []
header_div_open_html = '<div class="spoiler-block"><div class="spoiler-header">'
end_header_start_content_html = '</div><div class="spoiler-content"' \
' aria-hidden="true">'
end_header_start_content_html = '</div><div class="spoiler-content" aria-hidden="true">'
footer_html = '</div></div>'

output.append(self.placeholder(header_div_open_html))
Expand Down
14 changes: 7 additions & 7 deletions zerver/lib/name_restrictions.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def is_disposable_domain(domain: str) -> bool:
return False
return domain.lower() in DISPOSABLE_DOMAINS

ZULIP_RESERVED_SUBDOMAINS = frozenset([
ZULIP_RESERVED_SUBDOMAINS = {
# zulip terms
'stream', 'channel', 'topic', 'thread', 'installation', 'organization', 'realm',
'team', 'subdomain', 'activity', 'octopus', 'acme', 'push',
Expand All @@ -40,12 +40,12 @@ def is_disposable_domain(domain: str) -> bool:
# Things that sound like security
'auth', 'authentication', 'security',
# tech blogs
'engineering', 'infrastructure', 'tooling', 'tools', 'javascript', 'python'])
'engineering', 'infrastructure', 'tooling', 'tools', 'javascript', 'python'}

# Most of this list was curated from the following sources:
# http://wiki.dwscoalition.org/notes/List_of_reserved_subdomains (license: CC-BY-SA 3.0)
# https://stackoverflow.com/questions/11868191/which-saas-subdomains-to-block (license: CC-BY-SA 2.5)
GENERIC_RESERVED_SUBDOMAINS = frozenset([
GENERIC_RESERVED_SUBDOMAINS = {
'about', 'abuse', 'account', 'ad', 'admanager', 'admin', 'admindashboard',
'administrator', 'adsense', 'adword', 'affiliate', 'alpha', 'anonymous',
'api', 'assets', 'audio', 'badges', 'beta', 'billing', 'biz', 'blog',
Expand Down Expand Up @@ -77,12 +77,12 @@ def is_disposable_domain(domain: str) -> bool:
'testers', 'ticket', 'tool', 'tos', 'trac', 'translate', 'update',
'upgrade', 'uploads', 'use', 'user', 'username', 'validation', 'videos',
'volunteer', 'web', 'webdisk', 'webmail', 'webmaster', 'whm', 'whois',
'wiki', 'www', 'www0', 'www8', 'www9', 'xml', 'xmpp', 'xoxo'])
'wiki', 'www', 'www0', 'www8', 'www9', 'xml', 'xmpp', 'xoxo'}

DISPOSABLE_DOMAINS = frozenset(blacklist)
DISPOSABLE_DOMAINS = set(blacklist)

WHITELISTED_EMAIL_DOMAINS = frozenset([
WHITELISTED_EMAIL_DOMAINS = {
# Controlled by https://www.abine.com; more legitimate than most
# disposable domains
'opayq.com', 'abinemail.com', 'blurmail.net', 'maskmemail.com',
])
}
7 changes: 4 additions & 3 deletions zerver/lib/test_fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,18 +94,19 @@ def run_db_migrations(self) -> None:
# what the database is as runtime.
# Also we export ZULIP_DB_NAME which is ignored by dev platform but
# recognised by test platform and used to migrate correct db.
env_prelude = [
manage_py = [
'env',
'DJANGO_SETTINGS_MODULE=' + self.settings,
'ZULIP_DB_NAME=' + self.database_name,
'./manage.py',
]

run([
*env_prelude, './manage.py', 'migrate', '--no-input',
*manage_py, 'migrate', '--no-input'
])

run([
*env_prelude, './manage.py', 'get_migration_status', '--output='+self.migration_status_file,
*manage_py, 'get_migration_status', '--output='+self.migration_status_file
])

def what_to_do_with_migrations(self) -> str:
Expand Down
6 changes: 3 additions & 3 deletions zerver/lib/test_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,9 @@ def addFailure(self, *args: Any, **kwargs: Any) -> None:

def addSkip(self, test: TestCase, reason: str) -> None:
TestResult.addSkip(self, test, reason)
self.stream.writeln("** Skipping {}: {}".format( # type: ignore[attr-defined] # https://github.com/python/typeshed/issues/3139
test.id(),
reason))
self.stream.writeln( # type: ignore[attr-defined] # https://github.com/python/typeshed/issues/3139
"** Skipping {}: {}".format(test.id(), reason)
)
self.stream.flush()

class RemoteTestResult(django_runner.RemoteTestResult):
Expand Down
10 changes: 5 additions & 5 deletions zerver/lib/upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,11 +182,11 @@ def resize_emoji(image_data: bytes, size: int=DEFAULT_EMOJI_SIZE) -> bytes:
# results in resized gifs being broken. To work around this we
# only resize under certain conditions to minimize the chance of
# creating ugly gifs.
should_resize = any((
im.size[0] != im.size[1], # not square
im.size[0] > MAX_EMOJI_GIF_SIZE, # dimensions too large
len(image_data) > MAX_EMOJI_GIF_FILE_SIZE_BYTES, # filesize too large
))
should_resize = (
im.size[0] != im.size[1] # not square
or im.size[0] > MAX_EMOJI_GIF_SIZE # dimensions too large
or len(image_data) > MAX_EMOJI_GIF_FILE_SIZE_BYTES # filesize too large
)
return resize_gif(im, size) if should_resize else image_data
else:
im = exif_rotate(im)
Expand Down
1 change: 0 additions & 1 deletion zerver/lib/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,6 @@ def chunkify(q: Any, i: int) -> Iterator[Tuple[int, int, Any]]:
q = q.order_by('id')
min_id = -1
while True:
assert db_chunk_size is not None # Hint for mypy, but also workaround for mypy bug #3442.
rows = list(q.filter(id__gt=min_id)[0:db_chunk_size])
if len(rows) == 0:
break
Expand Down
3 changes: 2 additions & 1 deletion zerver/management/commands/email_mirror.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ def get_imap_messages() -> Generator[EmailMessage, None, None]:
assert isinstance(msg_data[0], tuple)
msg_as_bytes = msg_data[0][1]
message = email.message_from_bytes(msg_as_bytes, policy=email.policy.default)
assert isinstance(message, EmailMessage) # https://github.com/python/typeshed/issues/2417
# https://github.com/python/typeshed/issues/2417
assert isinstance(message, EmailMessage)
yield message
mbox.store(message_id, '+FLAGS', '\\Deleted')
mbox.expunge()
Expand Down
3 changes: 2 additions & 1 deletion zerver/management/commands/send_to_email_mirror.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,8 @@ def _parse_email_fixture(self, fixture_path: str) -> EmailMessage:
else:
with open(fixture_path, "rb") as fp:
message = email.message_from_binary_file(fp, policy=email.policy.default)
assert isinstance(message, EmailMessage) # https://github.com/python/typeshed/issues/2417
# https://github.com/python/typeshed/issues/2417
assert isinstance(message, EmailMessage)
return message

def _prepare_message(self, message: EmailMessage, realm: Realm, stream_name: str) -> None:
Expand Down
3 changes: 2 additions & 1 deletion zerver/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,8 @@ def process_response(self, request: HttpRequest, response: HttpResponse) -> Http
def process_exception(self, request: HttpRequest,
exception: Exception) -> Optional[HttpResponse]:
if isinstance(exception, RateLimited):
secs_to_freedom = float(str(exception)) # secs_to_freedom is passed to RateLimited when raising
# secs_to_freedom is passed to RateLimited when raising
secs_to_freedom = float(str(exception))
resp = json_error(
_("API usage exceeded rate limit"),
data={'retry-after': secs_to_freedom},
Expand Down
5 changes: 3 additions & 2 deletions zerver/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1409,7 +1409,7 @@ def filter_to_valid_prereg_users(query: QuerySet) -> QuerySet:

class MultiuseInvite(models.Model):
id: int = models.AutoField(auto_created=True, primary_key=True, verbose_name='ID')
referred_by: UserProfile = models.ForeignKey(UserProfile, on_delete=CASCADE) # Optional[UserProfile]
referred_by: UserProfile = models.ForeignKey(UserProfile, on_delete=CASCADE)
streams: Manager = models.ManyToManyField('Stream')
realm: Realm = models.ForeignKey(Realm, on_delete=CASCADE)
invited_as: int = models.PositiveSmallIntegerField(default=PreregistrationUser.INVITE_AS['MEMBER'])
Expand Down Expand Up @@ -2599,7 +2599,8 @@ def to_dict(self) -> Dict[str, Any]:
@staticmethod
def status_from_string(status: str) -> Optional[int]:
if status == 'active':
status_val: Optional[int] = UserPresence.ACTIVE # See https://github.com/python/mypy/issues/2611
# See https://github.com/python/mypy/issues/2611
status_val: Optional[int] = UserPresence.ACTIVE
elif status == 'idle':
status_val = UserPresence.IDLE
else:
Expand Down

0 comments on commit f91d287

Please sign in to comment.