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