Skip to content

Commit 4c4bf5e

Browse files
sethmlarsonpquentin
authored andcommitted
Support renaming custom classes (python-trio#49)
1 parent e41656e commit 4c4bf5e

File tree

5 files changed

+29
-2
lines changed

5 files changed

+29
-2
lines changed

src/unasync/__init__.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
"__aiter__": "__iter__",
1919
"__anext__": "__next__",
2020
"asynccontextmanager": "contextmanager",
21+
"AsyncIterable": "Iterable",
22+
"AsyncIterator": "Iterator",
23+
"AsyncGenerator": "Generator",
2124
# TODO StopIteration is still accepted in Python 2, but the right change
2225
# is 'raise StopAsyncIteration' -> 'return' since we want to use unasynced
2326
# code in Python 3.7+
@@ -68,8 +71,16 @@ def unasync_tokens(tokens):
6871
# `print( stuff)`
6972
used_space = space
7073
else:
71-
if toknum == std_tokenize.NAME and tokval in ASYNC_TO_SYNC:
72-
tokval = ASYNC_TO_SYNC[tokval]
74+
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:]
7384
if used_space is None:
7485
used_space = space
7586
yield (used_space, tokval)

tests/data/async/classes.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class AsyncSocket(object):
2+
async def send_all(self, data):
3+
...

tests/data/async/typing.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import typing
2+
3+
typing.AsyncIterable[bytes]
4+
typing.AsyncIterator[bytes]
5+
typing.AsyncGenerator[bytes]

tests/data/sync/classes.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class SyncSocket(object):
2+
def send_all(self, data):
3+
...

tests/data/sync/typing.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import typing
2+
3+
typing.Iterable[bytes]
4+
typing.Iterator[bytes]
5+
typing.Generator[bytes]

0 commit comments

Comments
 (0)