@@ -1430,7 +1430,6 @@ def _find_keyword_typos(self):
14301430 line , offset , source = self ._exc_metadata
14311431 end_line = int (self .lineno ) if self .lineno is not None else 0
14321432 lines = None
1433- from_filename = False
14341433
14351434 if source is None :
14361435 if self .filename :
@@ -1439,8 +1438,6 @@ def _find_keyword_typos(self):
14391438 lines = f .read ().splitlines ()
14401439 except Exception :
14411440 line , end_line , offset = 0 ,1 ,0
1442- else :
1443- from_filename = True
14441441 lines = lines if lines is not None else self .text .splitlines ()
14451442 else :
14461443 lines = source .splitlines ()
@@ -1462,17 +1459,19 @@ def _find_keyword_typos(self):
14621459 return # Original code compiles or is incomplete - can't validate fixes
14631460
14641461 error_lines = error_code .splitlines ()
1465- tokens = tokenize .generate_tokens (io .StringIO (error_code ).readline )
1462+ tokens = [
1463+ token
1464+ for token in tokenize .generate_tokens (io .StringIO (error_code ).readline )
1465+ if token .type == tokenize .NAME
1466+ ]
1467+ # Look for typos on the reported error line first. If the parser only
1468+ # fails after an earlier typo, the remaining lines are still searched.
1469+ the_end = end_line if line == 0 else end_line + 1
1470+ tokens .sort (key = lambda token : token .start [0 ] + line != the_end )
14661471 tokens_left_to_process = 10
14671472 import difflib
14681473 for token in tokens :
14691474 start , end = token .start , token .end
1470- if token .type != tokenize .NAME :
1471- continue
1472- # Only consider NAME tokens on the same line as the error
1473- the_end = end_line if line == 0 else end_line + 1
1474- if from_filename and token .start [0 ]+ line != the_end :
1475- continue
14761475 wrong_name = token .string
14771476 if wrong_name in keyword .kwlist :
14781477 continue
0 commit comments