Skip to content

Commit 2c2c9b7

Browse files
florimondmancasethmlarson
authored andcommitted
Rename class names in type forward references (#53)
* Unasync any string, test using forward ref to class itself * Honor initial formatting
1 parent 974d0fb commit 2c2c9b7

File tree

3 files changed

+37
-9
lines changed

3 files changed

+37
-9
lines changed

src/unasync/__init__.py

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,19 @@ def tokenize(f):
6060
last_end = (tok.end[0] + 1, 0)
6161

6262

63+
def unasync_name(name):
64+
if name in ASYNC_TO_SYNC:
65+
return ASYNC_TO_SYNC[name]
66+
# Convert classes prefixed with 'Async' into 'Sync'
67+
elif (
68+
len(name) > 5
69+
and name.startswith("Async")
70+
and name[5].isupper()
71+
):
72+
return "Sync" + name[5:]
73+
return name
74+
75+
6376
def unasync_tokens(tokens):
6477
# TODO __await__, ...?
6578
used_space = None
@@ -72,15 +85,10 @@ def unasync_tokens(tokens):
7285
used_space = space
7386
else:
7487
if toknum == std_tokenize.NAME:
75-
if tokval in ASYNC_TO_SYNC:
76-
tokval = ASYNC_TO_SYNC[tokval]
77-
# Convert classes prefixed with 'Async' into 'Sync'
78-
elif (
79-
len(tokval) > 5
80-
and tokval.startswith("Async")
81-
and tokval[5].isupper()
82-
):
83-
tokval = "Sync" + tokval[5:]
88+
tokval = unasync_name(tokval)
89+
elif toknum == std_tokenize.STRING:
90+
left_quote, name, right_quote = tokval[0], tokval[1:-1], tokval[-1]
91+
tokval = left_quote + unasync_name(name) + right_quote
8492
if used_space is None:
8593
used_space = space
8694
yield (used_space, tokval)

tests/data/async/classes.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
class AsyncLock(object):
2+
...
3+
4+
15
class AsyncSocket(object):
6+
def __init__(self, send_lock: AsyncLock):
7+
...
8+
9+
async def __aenter__(self) -> "AsyncSocket":
10+
...
11+
212
async def send_all(self, data):
313
...

tests/data/sync/classes.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
class SyncLock(object):
2+
...
3+
4+
15
class SyncSocket(object):
6+
def __init__(self, send_lock: SyncLock):
7+
...
8+
9+
def __enter__(self) -> "SyncSocket":
10+
...
11+
212
def send_all(self, data):
313
...

0 commit comments

Comments
 (0)