From c3ee967442fd72621724ab31abb3f3cfccb5eed4 Mon Sep 17 00:00:00 2001 From: florimondmanca Date: Sat, 4 Jan 2020 23:45:15 +0100 Subject: [PATCH 1/3] Rename class names in type forward references --- src/unasync/__init__.py | 31 ++++++++++++++++++++++--------- tests/data/async/classes.py | 15 +++++++++++++-- tests/data/sync/classes.py | 15 +++++++++++++-- 3 files changed, 48 insertions(+), 13 deletions(-) diff --git a/src/unasync/__init__.py b/src/unasync/__init__.py index 148da41..1925f8d 100644 --- a/src/unasync/__init__.py +++ b/src/unasync/__init__.py @@ -60,9 +60,24 @@ def tokenize(f): last_end = (tok.end[0] + 1, 0) +def unasync_name(name): + if name in ASYNC_TO_SYNC: + return ASYNC_TO_SYNC[name] + # Convert classes prefixed with 'Async' into 'Sync' + elif ( + len(name) > 5 + and name.startswith("Async") + and name[5].isupper() + ): + return "Sync" + name[5:] + return name + + def unasync_tokens(tokens): # TODO __await__, ...? used_space = None + previous_tokval = None + for space, toknum, tokval in tokens: if tokval in ["async", "await"]: # When removing async or await, we want to use the whitespace that @@ -72,20 +87,18 @@ def unasync_tokens(tokens): used_space = space else: if toknum == std_tokenize.NAME: - if tokval in ASYNC_TO_SYNC: - tokval = ASYNC_TO_SYNC[tokval] - # Convert classes prefixed with 'Async' into 'Sync' - elif ( - len(tokval) > 5 - and tokval.startswith("Async") - and tokval[5].isupper() - ): - tokval = "Sync" + tokval[5:] + tokval = unasync_name(tokval) + elif previous_tokval == ":" and toknum == std_tokenize.STRING: + # Type hint using a forward reference. + left_quote, name, right_quote = tokval[0], tokval[1:-1], tokval[-1] + tokval = left_quote + unasync_name(name) + right_quote if used_space is None: used_space = space yield (used_space, tokval) used_space = None + previous_tokval = tokval + def untokenize(tokens): return "".join(space + tokval for space, tokval in tokens) diff --git a/tests/data/async/classes.py b/tests/data/async/classes.py index 6969e1e..ecb4559 100644 --- a/tests/data/async/classes.py +++ b/tests/data/async/classes.py @@ -1,3 +1,14 @@ -class AsyncSocket(object): - async def send_all(self, data): +class AsyncLock: + ... + + +class AsyncSocket: + def __init__(self, send_lock: AsyncLock): ... + + async def send_all(self, data: "AsyncData"): + ... + + +class AsyncData: + ... diff --git a/tests/data/sync/classes.py b/tests/data/sync/classes.py index 151a843..14c5945 100644 --- a/tests/data/sync/classes.py +++ b/tests/data/sync/classes.py @@ -1,3 +1,14 @@ -class SyncSocket(object): - def send_all(self, data): +class SyncLock: + ... + + +class SyncSocket: + def __init__(self, send_lock: SyncLock): ... + + def send_all(self, data: "SyncData"): + ... + + +class SyncData: + ... From 03eb9113bde6e17a16bd319985ac12429965e498 Mon Sep 17 00:00:00 2001 From: florimondmanca Date: Sun, 5 Jan 2020 00:01:42 +0100 Subject: [PATCH 2/3] Unasync any string, test using forward ref to class itself --- src/unasync/__init__.py | 6 +----- tests/data/async/classes.py | 7 +++---- tests/data/sync/classes.py | 7 +++---- 3 files changed, 7 insertions(+), 13 deletions(-) diff --git a/src/unasync/__init__.py b/src/unasync/__init__.py index 1925f8d..ab1beb6 100644 --- a/src/unasync/__init__.py +++ b/src/unasync/__init__.py @@ -76,7 +76,6 @@ def unasync_name(name): def unasync_tokens(tokens): # TODO __await__, ...? used_space = None - previous_tokval = None for space, toknum, tokval in tokens: if tokval in ["async", "await"]: @@ -88,8 +87,7 @@ def unasync_tokens(tokens): else: if toknum == std_tokenize.NAME: tokval = unasync_name(tokval) - elif previous_tokval == ":" and toknum == std_tokenize.STRING: - # Type hint using a forward reference. + elif toknum == std_tokenize.STRING: left_quote, name, right_quote = tokval[0], tokval[1:-1], tokval[-1] tokval = left_quote + unasync_name(name) + right_quote if used_space is None: @@ -97,8 +95,6 @@ def unasync_tokens(tokens): yield (used_space, tokval) used_space = None - previous_tokval = tokval - def untokenize(tokens): return "".join(space + tokval for space, tokval in tokens) diff --git a/tests/data/async/classes.py b/tests/data/async/classes.py index ecb4559..2ef9215 100644 --- a/tests/data/async/classes.py +++ b/tests/data/async/classes.py @@ -6,9 +6,8 @@ class AsyncSocket: def __init__(self, send_lock: AsyncLock): ... - async def send_all(self, data: "AsyncData"): + async def __aenter__(self) -> "AsyncSocket": ... - -class AsyncData: - ... + async def send_all(self, data): + ... diff --git a/tests/data/sync/classes.py b/tests/data/sync/classes.py index 14c5945..37ba775 100644 --- a/tests/data/sync/classes.py +++ b/tests/data/sync/classes.py @@ -6,9 +6,8 @@ class SyncSocket: def __init__(self, send_lock: SyncLock): ... - def send_all(self, data: "SyncData"): + def __enter__(self) -> "SyncSocket": ... - -class SyncData: - ... + def send_all(self, data): + ... From 6980c6f5301fcbaaef76ce341022069199afa426 Mon Sep 17 00:00:00 2001 From: florimondmanca Date: Sun, 5 Jan 2020 00:02:55 +0100 Subject: [PATCH 3/3] Honor initial formatting --- src/unasync/__init__.py | 1 - tests/data/async/classes.py | 4 ++-- tests/data/sync/classes.py | 4 ++-- 3 files changed, 4 insertions(+), 5 deletions(-) diff --git a/src/unasync/__init__.py b/src/unasync/__init__.py index ab1beb6..634e08c 100644 --- a/src/unasync/__init__.py +++ b/src/unasync/__init__.py @@ -76,7 +76,6 @@ def unasync_name(name): def unasync_tokens(tokens): # TODO __await__, ...? used_space = None - for space, toknum, tokval in tokens: if tokval in ["async", "await"]: # When removing async or await, we want to use the whitespace that diff --git a/tests/data/async/classes.py b/tests/data/async/classes.py index 2ef9215..5a298ce 100644 --- a/tests/data/async/classes.py +++ b/tests/data/async/classes.py @@ -1,8 +1,8 @@ -class AsyncLock: +class AsyncLock(object): ... -class AsyncSocket: +class AsyncSocket(object): def __init__(self, send_lock: AsyncLock): ... diff --git a/tests/data/sync/classes.py b/tests/data/sync/classes.py index 37ba775..845e2ab 100644 --- a/tests/data/sync/classes.py +++ b/tests/data/sync/classes.py @@ -1,8 +1,8 @@ -class SyncLock: +class SyncLock(object): ... -class SyncSocket: +class SyncSocket(object): def __init__(self, send_lock: SyncLock): ...