Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Commit

Permalink
Replace all remaining six usage with native Python 3 equivalents (#7704)
Browse files Browse the repository at this point in the history
  • Loading branch information
ilmari authored Jun 16, 2020
1 parent 98c4e35 commit a3f1156
Show file tree
Hide file tree
Showing 73 changed files with 111 additions and 237 deletions.
1 change: 1 addition & 0 deletions changelog.d/7704.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Replace all remaining uses of `six` with native Python 3 equivalents. Contributed by @ilmari.
4 changes: 1 addition & 3 deletions contrib/graph/graph3.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@
from synapse.events import FrozenEvent
from synapse.util.frozenutils import unfreeze

from six import string_types


def make_graph(file_name, room_id, file_prefix, limit):
print("Reading lines")
Expand Down Expand Up @@ -62,7 +60,7 @@ def make_graph(file_name, room_id, file_prefix, limit):
for key, value in unfreeze(event.get_dict()["content"]).items():
if value is None:
value = "<null>"
elif isinstance(value, string_types):
elif isinstance(value, str):
pass
else:
value = json.dumps(value)
Expand Down
3 changes: 1 addition & 2 deletions scripts-dev/federation_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@
import base64
import json
import sys

from six.moves.urllib import parse as urlparse
from urllib import parse as urlparse

import nacl.signing
import requests
Expand Down
4 changes: 1 addition & 3 deletions scripts/synapse_port_db
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ import sys
import time
import traceback

from six import string_types

import yaml

from twisted.internet import defer, reactor
Expand Down Expand Up @@ -635,7 +633,7 @@ class Porter(object):
return bool(col)
if isinstance(col, bytes):
return bytearray(col)
elif isinstance(col, string_types) and "\0" in col:
elif isinstance(col, str) and "\0" in col:
logger.warning(
"DROPPING ROW: NUL value in table %s col %s: %r",
table,
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ sections=FUTURE,STDLIB,COMPAT,THIRDPARTY,TWISTED,FIRSTPARTY,TESTS,LOCALFOLDER
default_section=THIRDPARTY
known_first_party = synapse
known_tests=tests
known_compat = mock,six
known_compat = mock
known_twisted=twisted,OpenSSL
multi_line_output=3
include_trailing_comma=true
Expand Down
2 changes: 0 additions & 2 deletions synapse/_scripts/register_new_matrix_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@
import logging
import sys

from six.moves import input

import requests as _requests
import yaml

Expand Down
7 changes: 3 additions & 4 deletions synapse/api/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,9 @@
"""Contains exceptions and error codes."""

import logging
from http import HTTPStatus
from typing import Dict, List

from six.moves import http_client

from canonicaljson import json

from twisted.web import http
Expand Down Expand Up @@ -173,7 +172,7 @@ def __init__(self, msg, consent_uri):
consent_url (str): The URL where the user can give their consent
"""
super(ConsentNotGivenError, self).__init__(
code=http_client.FORBIDDEN, msg=msg, errcode=Codes.CONSENT_NOT_GIVEN
code=HTTPStatus.FORBIDDEN, msg=msg, errcode=Codes.CONSENT_NOT_GIVEN
)
self._consent_uri = consent_uri

Expand All @@ -193,7 +192,7 @@ def __init__(self, msg):
msg (str): The human-readable error message
"""
super(UserDeactivatedError, self).__init__(
code=http_client.FORBIDDEN, msg=msg, errcode=Codes.USER_DEACTIVATED
code=HTTPStatus.FORBIDDEN, msg=msg, errcode=Codes.USER_DEACTIVATED
)


Expand Down
4 changes: 1 addition & 3 deletions synapse/api/filtering.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
# limitations under the License.
from typing import List

from six import text_type

import jsonschema
from canonicaljson import json
from jsonschema import FormatChecker
Expand Down Expand Up @@ -313,7 +311,7 @@ def check(self, event):

content = event.get("content", {})
# check if there is a string url field in the content for filtering purposes
contains_url = isinstance(content.get("url"), text_type)
contains_url = isinstance(content.get("url"), str)
labels = content.get(EventContentFields.LABELS, [])

return self.check_fields(room_id, sender, ev_type, labels, contains_url)
Expand Down
3 changes: 1 addition & 2 deletions synapse/api/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@
"""Contains the URL paths to prefix various aspects of the server with. """
import hmac
from hashlib import sha256

from six.moves.urllib.parse import urlencode
from urllib.parse import urlencode

from synapse.config import ConfigError

Expand Down
4 changes: 1 addition & 3 deletions synapse/appservice/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
import logging
import re

from six import string_types

from twisted.internet import defer

from synapse.api.constants import EventTypes
Expand Down Expand Up @@ -156,7 +154,7 @@ def _check_namespaces(self, namespaces):
)

regex = regex_obj.get("regex")
if isinstance(regex, string_types):
if isinstance(regex, str):
regex_obj["regex"] = re.compile(regex) # Pre-compile regex
else:
raise ValueError("Expected string for 'regex' in ns '%s'" % ns)
Expand Down
3 changes: 1 addition & 2 deletions synapse/appservice/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import logging

from six.moves import urllib
import urllib

from prometheus_client import Counter

Expand Down
6 changes: 2 additions & 4 deletions synapse/config/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@
from textwrap import dedent
from typing import Any, MutableMapping, Optional

from six import integer_types

import yaml


Expand Down Expand Up @@ -117,7 +115,7 @@ def __getattr__(self, item: str) -> Any:

@staticmethod
def parse_size(value):
if isinstance(value, integer_types):
if isinstance(value, int):
return value
sizes = {"K": 1024, "M": 1024 * 1024}
size = 1
Expand All @@ -129,7 +127,7 @@ def parse_size(value):

@staticmethod
def parse_duration(value):
if isinstance(value, integer_types):
if isinstance(value, int):
return value
second = 1000
minute = 60 * second
Expand Down
13 changes: 4 additions & 9 deletions synapse/config/appservice.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@

import logging
from typing import Dict

from six import string_types
from six.moves.urllib import parse as urlparse
from urllib import parse as urlparse

import yaml
from netaddr import IPSet
Expand Down Expand Up @@ -98,17 +96,14 @@ def load_appservices(hostname, config_files):
def _load_appservice(hostname, as_info, config_filename):
required_string_fields = ["id", "as_token", "hs_token", "sender_localpart"]
for field in required_string_fields:
if not isinstance(as_info.get(field), string_types):
if not isinstance(as_info.get(field), str):
raise KeyError(
"Required string field: '%s' (%s)" % (field, config_filename)
)

# 'url' must either be a string or explicitly null, not missing
# to avoid accidentally turning off push for ASes.
if (
not isinstance(as_info.get("url"), string_types)
and as_info.get("url", "") is not None
):
if not isinstance(as_info.get("url"), str) and as_info.get("url", "") is not None:
raise KeyError(
"Required string field or explicit null: 'url' (%s)" % (config_filename,)
)
Expand Down Expand Up @@ -138,7 +133,7 @@ def _load_appservice(hostname, as_info, config_filename):
ns,
regex_obj,
)
if not isinstance(regex_obj.get("regex"), string_types):
if not isinstance(regex_obj.get("regex"), str):
raise ValueError("Missing/bad type 'regex' key in %s", regex_obj)
if not isinstance(regex_obj.get("exclusive"), bool):
raise ValueError(
Expand Down
4 changes: 1 addition & 3 deletions synapse/config/tls.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@
from hashlib import sha256
from typing import List

import six

from unpaddedbase64 import encode_base64

from OpenSSL import SSL, crypto
Expand Down Expand Up @@ -59,7 +57,7 @@ def read_config(self, config: dict, config_dir_path: str, **kwargs):
logger.warning(ACME_SUPPORT_ENABLED_WARN)

# hyperlink complains on py2 if this is not a Unicode
self.acme_url = six.text_type(
self.acme_url = str(
acme_config.get("url", "https://acme-v01.api.letsencrypt.org/directory")
)
self.acme_port = acme_config.get("port", 80)
Expand Down
6 changes: 2 additions & 4 deletions synapse/crypto/keyring.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,9 @@
# limitations under the License.

import logging
import urllib
from collections import defaultdict

import six
from six.moves import urllib

import attr
from signedjson.key import (
decode_verify_key_bytes,
Expand Down Expand Up @@ -661,7 +659,7 @@ def get_server_verify_key_v2_indirect(self, keys_to_fetch, key_server):
for response in query_response["server_keys"]:
# do this first, so that we can give useful errors thereafter
server_name = response.get("server_name")
if not isinstance(server_name, six.string_types):
if not isinstance(server_name, str):
raise KeyLookupError(
"Malformed response from key notary server %s: invalid server_name"
% (perspective_name,)
Expand Down
4 changes: 1 addition & 3 deletions synapse/events/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
import re
from typing import Any, Mapping, Union

from six import string_types

from frozendict import frozendict

from twisted.internet import defer
Expand Down Expand Up @@ -318,7 +316,7 @@ def serialize_event(

if only_event_fields:
if not isinstance(only_event_fields, list) or not all(
isinstance(f, string_types) for f in only_event_fields
isinstance(f, str) for f in only_event_fields
):
raise TypeError("only_event_fields must be a list of strings")
d = only_fields(d, only_event_fields)
Expand Down
12 changes: 5 additions & 7 deletions synapse/events/validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from six import integer_types, string_types

from synapse.api.constants import MAX_ALIAS_LENGTH, EventTypes, Membership
from synapse.api.errors import Codes, SynapseError
from synapse.api.room_versions import EventFormatVersions
Expand Down Expand Up @@ -53,7 +51,7 @@ def validate_new(self, event, config):
event_strings = ["origin"]

for s in event_strings:
if not isinstance(getattr(event, s), string_types):
if not isinstance(getattr(event, s), str):
raise SynapseError(400, "'%s' not a string type" % (s,))

# Depending on the room version, ensure the data is spec compliant JSON.
Expand Down Expand Up @@ -90,7 +88,7 @@ def _validate_retention(self, event, config):
max_lifetime = event.content.get("max_lifetime")

if min_lifetime is not None:
if not isinstance(min_lifetime, integer_types):
if not isinstance(min_lifetime, int):
raise SynapseError(
code=400,
msg="'min_lifetime' must be an integer",
Expand Down Expand Up @@ -124,7 +122,7 @@ def _validate_retention(self, event, config):
)

if max_lifetime is not None:
if not isinstance(max_lifetime, integer_types):
if not isinstance(max_lifetime, int):
raise SynapseError(
code=400,
msg="'max_lifetime' must be an integer",
Expand Down Expand Up @@ -183,7 +181,7 @@ def validate_builder(self, event):
strings.append("state_key")

for s in strings:
if not isinstance(getattr(event, s), string_types):
if not isinstance(getattr(event, s), str):
raise SynapseError(400, "Not '%s' a string type" % (s,))

RoomID.from_string(event.room_id)
Expand Down Expand Up @@ -223,7 +221,7 @@ def _ensure_strings(self, d, keys):
for s in keys:
if s not in d:
raise SynapseError(400, "'%s' not in content" % (s,))
if not isinstance(d[s], string_types):
if not isinstance(d[s], str):
raise SynapseError(400, "'%s' not a string type" % (s,))

def _ensure_state_event(self, event):
Expand Down
4 changes: 1 addition & 3 deletions synapse/federation/federation_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
from collections import namedtuple
from typing import Iterable, List

import six

from twisted.internet import defer
from twisted.internet.defer import Deferred, DeferredList
from twisted.python.failure import Failure
Expand Down Expand Up @@ -294,7 +292,7 @@ def event_from_pdu_json(
assert_params_in_dict(pdu_json, ("type", "depth"))

depth = pdu_json["depth"]
if not isinstance(depth, six.integer_types):
if not isinstance(depth, int):
raise SynapseError(400, "Depth %r not an intger" % (depth,), Codes.BAD_JSON)

if depth < 0:
Expand Down
4 changes: 1 addition & 3 deletions synapse/federation/federation_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
import logging
from typing import Any, Callable, Dict, List, Match, Optional, Tuple, Union

import six

from canonicaljson import json
from prometheus_client import Counter

Expand Down Expand Up @@ -751,7 +749,7 @@ def server_matches_acl_event(server_name: str, acl_event: EventBase) -> bool:


def _acl_entry_matches(server_name: str, acl_entry: str) -> Match:
if not isinstance(acl_entry, six.string_types):
if not isinstance(acl_entry, str):
logger.warning(
"Ignoring non-str ACL entry '%s' (is %s)", acl_entry, type(acl_entry)
)
Expand Down
3 changes: 1 addition & 2 deletions synapse/federation/transport/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,9 @@
# limitations under the License.

import logging
import urllib
from typing import Any, Dict, Optional

from six.moves import urllib

from twisted.internet import defer

from synapse.api.constants import Membership
Expand Down
4 changes: 1 addition & 3 deletions synapse/groups/groups_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@

import logging

from six import string_types

from synapse.api.errors import Codes, SynapseError
from synapse.types import GroupID, RoomID, UserID, get_domain_from_id
from synapse.util.async_helpers import concurrently_execute
Expand Down Expand Up @@ -513,7 +511,7 @@ async def update_group_profile(self, group_id, requester_user_id, content):
for keyname in ("name", "avatar_url", "short_description", "long_description"):
if keyname in content:
value = content[keyname]
if not isinstance(value, string_types):
if not isinstance(value, str):
raise SynapseError(400, "%r value is not a string" % (keyname,))
profile[keyname] = value

Expand Down
Loading

0 comments on commit a3f1156

Please sign in to comment.