@@ -1627,6 +1627,29 @@ def ambiguous_identifier(logical_line, tokens):
1627
1627
prev_start = start
1628
1628
1629
1629
1630
+ # https://docs.python.org/3/reference/lexical_analysis.html#string-and-bytes-literals
1631
+ _PYTHON_3000_VALID_ESC = frozenset ([
1632
+ '\n ' ,
1633
+ '\\ ' ,
1634
+ '\' ' ,
1635
+ '"' ,
1636
+ 'a' ,
1637
+ 'b' ,
1638
+ 'f' ,
1639
+ 'n' ,
1640
+ 'r' ,
1641
+ 't' ,
1642
+ 'v' ,
1643
+ '0' , '1' , '2' , '3' , '4' , '5' , '6' , '7' ,
1644
+ 'x' ,
1645
+
1646
+ # Escape sequences only recognized in string literals
1647
+ 'N' ,
1648
+ 'u' ,
1649
+ 'U' ,
1650
+ ])
1651
+
1652
+
1630
1653
@register_check
1631
1654
def python_3000_invalid_escape_sequence (logical_line , tokens , noqa ):
1632
1655
r"""Invalid escape sequences are deprecated in Python 3.6.
@@ -1637,41 +1660,23 @@ def python_3000_invalid_escape_sequence(logical_line, tokens, noqa):
1637
1660
if noqa :
1638
1661
return
1639
1662
1640
- # https://docs.python.org/3/reference/lexical_analysis.html#string-and-bytes-literals
1641
- valid = [
1642
- '\n ' ,
1643
- '\\ ' ,
1644
- '\' ' ,
1645
- '"' ,
1646
- 'a' ,
1647
- 'b' ,
1648
- 'f' ,
1649
- 'n' ,
1650
- 'r' ,
1651
- 't' ,
1652
- 'v' ,
1653
- '0' , '1' , '2' , '3' , '4' , '5' , '6' , '7' ,
1654
- 'x' ,
1655
-
1656
- # Escape sequences only recognized in string literals
1657
- 'N' ,
1658
- 'u' ,
1659
- 'U' ,
1660
- ]
1661
-
1662
1663
prefixes = []
1663
1664
for token_type , text , start , _ , _ in tokens :
1664
- if token_type in {tokenize .STRING , FSTRING_START , TSTRING_START }:
1665
+ if token_type == tokenize .STRING or \
1666
+ token_type == FSTRING_START or \
1667
+ token_type == TSTRING_START :
1665
1668
# Extract string modifiers (e.g. u or r)
1666
1669
prefixes .append (text [:text .index (text [- 1 ])].lower ())
1667
1670
1668
- if token_type in {tokenize .STRING , FSTRING_MIDDLE , TSTRING_MIDDLE }:
1671
+ if token_type == tokenize .STRING or \
1672
+ token_type == FSTRING_MIDDLE or \
1673
+ token_type == TSTRING_MIDDLE :
1669
1674
if 'r' not in prefixes [- 1 ]:
1670
1675
start_line , start_col = start
1671
1676
pos = text .find ('\\ ' )
1672
1677
while pos >= 0 :
1673
1678
pos += 1
1674
- if text [pos ] not in valid :
1679
+ if text [pos ] not in _PYTHON_3000_VALID_ESC :
1675
1680
line = start_line + text .count ('\n ' , 0 , pos )
1676
1681
if line == start_line :
1677
1682
col = start_col + pos
@@ -1683,7 +1688,9 @@ def python_3000_invalid_escape_sequence(logical_line, tokens, noqa):
1683
1688
)
1684
1689
pos = text .find ('\\ ' , pos + 1 )
1685
1690
1686
- if token_type in {tokenize .STRING , FSTRING_END , TSTRING_END }:
1691
+ if token_type == tokenize .STRING or \
1692
+ token_type == FSTRING_END or \
1693
+ token_type == TSTRING_END :
1687
1694
prefixes .pop ()
1688
1695
1689
1696
0 commit comments