@@ -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,27 @@ 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 (
1666
+ token_type == tokenize .STRING or
1667
+ token_type == FSTRING_START or
1668
+ token_type == TSTRING_START
1669
+ ):
1665
1670
# Extract string modifiers (e.g. u or r)
1666
1671
prefixes .append (text [:text .index (text [- 1 ])].lower ())
1667
1672
1668
- if token_type in {tokenize .STRING , FSTRING_MIDDLE , TSTRING_MIDDLE }:
1673
+ if (
1674
+ token_type == tokenize .STRING or
1675
+ token_type == FSTRING_MIDDLE or
1676
+ token_type == TSTRING_MIDDLE
1677
+ ):
1669
1678
if 'r' not in prefixes [- 1 ]:
1670
1679
start_line , start_col = start
1671
1680
pos = text .find ('\\ ' )
1672
1681
while pos >= 0 :
1673
1682
pos += 1
1674
- if text [pos ] not in valid :
1683
+ if text [pos ] not in _PYTHON_3000_VALID_ESC :
1675
1684
line = start_line + text .count ('\n ' , 0 , pos )
1676
1685
if line == start_line :
1677
1686
col = start_col + pos
@@ -1683,7 +1692,11 @@ def python_3000_invalid_escape_sequence(logical_line, tokens, noqa):
1683
1692
)
1684
1693
pos = text .find ('\\ ' , pos + 1 )
1685
1694
1686
- if token_type in {tokenize .STRING , FSTRING_END , TSTRING_END }:
1695
+ if (
1696
+ token_type == tokenize .STRING or
1697
+ token_type == FSTRING_END or
1698
+ token_type == TSTRING_END
1699
+ ):
1687
1700
prefixes .pop ()
1688
1701
1689
1702
0 commit comments