@@ -182,15 +182,50 @@ def _replace_import(self, tokens, token_counter, new_tokens: list):
182182
183183 full_lib_name = ''
184184 lib_name_counter = token_counter + 2
185- if len (_IMPORTS_REPLACE .keys ()) == 0 :
186- return lib_name_counter
187-
188- while True :
189- if tokens [lib_name_counter ].src == " " :
190- break
185+
186+ # Handle special case for "from asyncio import sleep" -> "from time import sleep"
187+ # First check if this is an asyncio import with just sleep
188+ if (lib_name_counter < len (tokens ) and
189+ tokens [lib_name_counter ].src == "asyncio" ):
190+
191+ # Look ahead to see if it's "import sleep" specifically
192+ check_counter = lib_name_counter + 1
193+ # Skip whitespace
194+ while check_counter < len (tokens ) and tokens [check_counter ].src == " " :
195+ check_counter += 1
196+ # Check if next is "import"
197+ if check_counter < len (tokens ) and tokens [check_counter ].src == "import" :
198+ check_counter += 1
199+ # Skip whitespace after "import"
200+ while check_counter < len (tokens ) and tokens [check_counter ].src == " " :
201+ check_counter += 1
202+ # Check if next is exactly "sleep"
203+ if (check_counter < len (tokens ) and tokens [check_counter ].src == "sleep" ):
204+ # Check what comes after "sleep" to determine if this is a standalone sleep import
205+ next_check = check_counter + 1
206+ is_sleep_only = True
207+
208+ # Skip any whitespace
209+ while next_check < len (tokens ) and tokens [next_check ].src == " " :
210+ next_check += 1
211+
212+ if next_check < len (tokens ):
213+ next_token = tokens [next_check ]
214+ # If there's a comma, "as", or other import items, it's not a standalone sleep import
215+ if next_token .src in ["," , "as" ] or next_token .name == "NAME" :
216+ is_sleep_only = False
217+
218+ if is_sleep_only :
219+ # Replace "asyncio" with "time" only for standalone "sleep" import
220+ new_tokens .append (tokenize_rt .Token ("NAME" , "time" ))
221+ return lib_name_counter + 1 # Skip past "asyncio"
222+
223+ # Parse the full module name for other cases
224+ while lib_name_counter < len (tokens ) and tokens [lib_name_counter ].src != " " :
191225 full_lib_name = full_lib_name + tokens [lib_name_counter ].src
192226 lib_name_counter = lib_name_counter + 1
193-
227+
228+ # Handle existing _IMPORTS_REPLACE logic
194229 for key , value in _IMPORTS_REPLACE .items ():
195230 if key in full_lib_name :
196231 updated_lib_name = full_lib_name .replace (key , value )
@@ -201,7 +236,14 @@ def _replace_import(self, tokens, token_counter, new_tokens: list):
201236 new_tokens .pop ()
202237 return lib_name_counter
203238
204- lib_name_counter = token_counter + 2
239+ # For all other cases, add the original module name tokens
240+ for lib_name_part in full_lib_name .split ("." ):
241+ lib_name_part = self ._class_rename (lib_name_part )
242+ new_tokens .append (tokenize_rt .Token ("NAME" , lib_name_part ))
243+ new_tokens .append (tokenize_rt .Token ("OP" , "." ))
244+ if full_lib_name : # Only remove the last dot if we added tokens
245+ new_tokens .pop ()
246+
205247 return lib_name_counter
206248
207249 def _class_rename (self , name ):
0 commit comments