From f3d9324648f7bcbf596ead8f397d027b8c5f4d6d Mon Sep 17 00:00:00 2001 From: Patrick Cloke Date: Thu, 6 Jan 2022 12:21:02 -0500 Subject: [PATCH 1/4] Remove a Python 2-ism. --- synapse/types.py | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/synapse/types.py b/synapse/types.py index 42aeaf6270e1..8c71c1c9c6e4 100644 --- a/synapse/types.py +++ b/synapse/types.py @@ -21,6 +21,7 @@ ClassVar, Dict, Mapping, + Match, MutableMapping, Optional, Tuple, @@ -380,7 +381,7 @@ def map_username_to_mxid_localpart( onto different mxids Returns: - unicode: string suitable for a mxid localpart + string suitable for a mxid localpart """ if not isinstance(username, bytes): username = username.encode("utf-8") @@ -388,21 +389,16 @@ def map_username_to_mxid_localpart( # first we sort out upper-case characters if case_sensitive: - def f1(m): + def f1(m: Match) -> bytes: return b"_" + m.group().lower() username = UPPER_CASE_PATTERN.sub(f1, username) else: username = username.lower() - # then we sort out non-ascii characters - def f2(m): - g = m.group()[0] - if isinstance(g, str): - # on python 2, we need to do a ord(). On python 3, the - # byte itself will do. - g = ord(g) - return b"=%02x" % (g,) + # then we sort out non-ascii characters by converting to the hex equivalent. + def f2(m: Match) -> bytes: + return b"=%02x" % (m.group()[0],) username = NON_MXID_CHARACTER_PATTERN.sub(f2, username) From c81ed74fd2f76c96b42d8294b401fb29ae7fd648 Mon Sep 17 00:00:00 2001 From: Patrick Cloke Date: Thu, 6 Jan 2022 12:23:34 -0500 Subject: [PATCH 2/4] Newsfragment --- changelog.d/11699.misc | 1 + 1 file changed, 1 insertion(+) create mode 100644 changelog.d/11699.misc diff --git a/changelog.d/11699.misc b/changelog.d/11699.misc new file mode 100644 index 000000000000..ffae5f296061 --- /dev/null +++ b/changelog.d/11699.misc @@ -0,0 +1 @@ +Remove fallback code for Python 2. From 99cc440ef9342d203d7ca2e2032cc6eb07368c40 Mon Sep 17 00:00:00 2001 From: Patrick Cloke Date: Thu, 6 Jan 2022 12:26:23 -0500 Subject: [PATCH 3/4] More specific type hints. --- synapse/types.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/synapse/types.py b/synapse/types.py index 8c71c1c9c6e4..32e196603225 100644 --- a/synapse/types.py +++ b/synapse/types.py @@ -389,7 +389,7 @@ def map_username_to_mxid_localpart( # first we sort out upper-case characters if case_sensitive: - def f1(m: Match) -> bytes: + def f1(m: Match[bytes]) -> bytes: return b"_" + m.group().lower() username = UPPER_CASE_PATTERN.sub(f1, username) @@ -397,7 +397,7 @@ def f1(m: Match) -> bytes: username = username.lower() # then we sort out non-ascii characters by converting to the hex equivalent. - def f2(m: Match) -> bytes: + def f2(m: Match[bytes]) -> bytes: return b"=%02x" % (m.group()[0],) username = NON_MXID_CHARACTER_PATTERN.sub(f2, username) From 9172235a0c172f39671d0d1a8a98de8162278ec3 Mon Sep 17 00:00:00 2001 From: Patrick Cloke Date: Thu, 6 Jan 2022 12:42:03 -0500 Subject: [PATCH 4/4] Update comment. --- synapse/types.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/synapse/types.py b/synapse/types.py index 32e196603225..74a2c5185705 100644 --- a/synapse/types.py +++ b/synapse/types.py @@ -405,8 +405,7 @@ def f2(m: Match[bytes]) -> bytes: # we also do the =-escaping to mxids starting with an underscore. username = re.sub(b"^_", b"=5f", username) - # we should now only have ascii bytes left, so can decode back to a - # unicode. + # we should now only have ascii bytes left, so can decode back to a string. return username.decode("ascii")