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

Remove a Python 2-ism and improve type hints. #11699

Merged
merged 4 commits into from
Jan 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/11699.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Remove fallback code for Python 2.
19 changes: 7 additions & 12 deletions synapse/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
ClassVar,
Dict,
Mapping,
Match,
MutableMapping,
Optional,
Tuple,
Expand Down Expand Up @@ -380,37 +381,31 @@ 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")

# first we sort out upper-case characters
if case_sensitive:

def f1(m):
def f1(m: Match[bytes]) -> 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]
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

m.group() would be something like b"x" (which is equivalent to "x" on Python 2).

On Python 2, g would be something like "x", so you call ord(g) to get 120.

On Python 3, g would be something like 120 directly.

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]) -> bytes:
return b"=%02x" % (m.group()[0],)

username = NON_MXID_CHARACTER_PATTERN.sub(f2, username)

# 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")


Expand Down