Skip to content

Commit 82c76af

Browse files
committed
Scope import and from as "keyword.control.import.python"
This fixes handling of unused imports by flake8
1 parent f6369e4 commit 82c76af

File tree

8 files changed

+86
-52
lines changed

8 files changed

+86
-52
lines changed

grammars/MagicPython.cson

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ repository:
2727
statement:
2828
patterns: [
2929
{
30-
include: "#from-import"
30+
include: "#import"
3131
}
3232
{
3333
include: "#class-declaration"
@@ -259,7 +259,7 @@ repository:
259259
(?x)
260260
\\b(?<!\\.)(
261261
as | async | continue | del | assert | break | finally | for
262-
| from | elif | else | if | import | except | pass | raise
262+
| from | elif | else | if | except | pass | raise
263263
| return | try | while | with
264264
)\\b
265265
@@ -926,20 +926,29 @@ repository:
926926
]
927927
}
928928
]
929-
"from-import":
929+
import:
930930
comment: '''
931-
The main purpose of this rule is to consume '...', so that
932-
it's not tokenized as ellipsis.
931+
Import statements
933932
934933
'''
935-
match: '''
936-
(?x)
937-
\\s* \\b(from)\\b (\\s*\\.+\\s*)
938-
939-
'''
940-
captures:
941-
"1":
942-
name: "keyword.control.flow.python"
934+
patterns: [
935+
{
936+
match: '''
937+
(?x)
938+
\\s* \\b(from)\\b (\\s*\\.+\\s*) (import)?
939+
940+
'''
941+
captures:
942+
"1":
943+
name: "keyword.control.import.python"
944+
"3":
945+
name: "keyword.control.import.python"
946+
}
947+
{
948+
name: "keyword.control.import.python"
949+
match: "\\b(?<!\\.)import\\b"
950+
}
951+
]
943952
"class-declaration":
944953
patterns: [
945954
{

grammars/MagicPython.tmLanguage

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
<array>
4545
<dict>
4646
<key>include</key>
47-
<string>#from-import</string>
47+
<string>#import</string>
4848
</dict>
4949
<dict>
5050
<key>include</key>
@@ -420,7 +420,7 @@
420420
<string>(?x)
421421
\b(?&lt;!\.)(
422422
as | async | continue | del | assert | break | finally | for
423-
| from | elif | else | if | import | except | pass | raise
423+
| from | elif | else | if | except | pass | raise
424424
| return | try | while | with
425425
)\b
426426
</string>
@@ -1480,24 +1480,39 @@
14801480
</dict>
14811481
</array>
14821482
</dict>
1483-
<key>from-import</key>
1483+
<key>import</key>
14841484
<dict>
14851485
<key>comment</key>
1486-
<string>The main purpose of this rule is to consume '...', so that
1487-
it's not tokenized as ellipsis.
1486+
<string>Import statements
14881487
</string>
1489-
<key>match</key>
1490-
<string>(?x)
1491-
\s* \b(from)\b (\s*\.+\s*)
1488+
<key>patterns</key>
1489+
<array>
1490+
<dict>
1491+
<key>match</key>
1492+
<string>(?x)
1493+
\s* \b(from)\b (\s*\.+\s*) (import)?
14921494
</string>
1493-
<key>captures</key>
1494-
<dict>
1495-
<key>1</key>
1495+
<key>captures</key>
1496+
<dict>
1497+
<key>1</key>
1498+
<dict>
1499+
<key>name</key>
1500+
<string>keyword.control.import.python</string>
1501+
</dict>
1502+
<key>3</key>
1503+
<dict>
1504+
<key>name</key>
1505+
<string>keyword.control.import.python</string>
1506+
</dict>
1507+
</dict>
1508+
</dict>
14961509
<dict>
14971510
<key>name</key>
1498-
<string>keyword.control.flow.python</string>
1511+
<string>keyword.control.import.python</string>
1512+
<key>match</key>
1513+
<string>\b(?&lt;!\.)import\b</string>
14991514
</dict>
1500-
</dict>
1515+
</array>
15011516
</dict>
15021517
<key>class-declaration</key>
15031518
<dict>

grammars/src/MagicPython.syntax.yaml

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ repository:
7272

7373
statement:
7474
patterns:
75-
- include: '#from-import'
75+
- include: '#import'
7676
- include: '#class-declaration'
7777
- include: '#function-declaration'
7878
- include: '#statement-keyword'
@@ -219,7 +219,7 @@ repository:
219219
(?x)
220220
\b(?<!\.)(
221221
as | async | continue | del | assert | break | finally | for
222-
| from | elif | else | if | import | except | pass | raise
222+
| from | elif | else | if | except | pass | raise
223223
| return | try | while | with
224224
)\b
225225
- name: storage.modifier.declaration.python
@@ -650,15 +650,18 @@ repository:
650650
- match: |
651651
(?x) \{ [^'"}\n]*? \} (?=.*?\})
652652
653-
from-import:
653+
import:
654654
comment: |
655-
The main purpose of this rule is to consume '...', so that
656-
it's not tokenized as ellipsis.
657-
match: |
658-
(?x)
659-
\s* \b(from)\b (\s*\.+\s*)
660-
captures:
661-
'1': {name: keyword.control.flow.python}
655+
Import statements
656+
patterns:
657+
- match: |
658+
(?x)
659+
\s* \b(from)\b (\s*\.+\s*) (import)?
660+
captures:
661+
'1': {name: keyword.control.import.python}
662+
'3': {name: keyword.control.import.python}
663+
- name: keyword.control.import.python
664+
match: \b(?<!\.)import\b
662665

663666
class-declaration:
664667
patterns:

misc/scopes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ invalid.illegal.operator.python
4040
invalid.illegal.prefix.python
4141
keyword.codetag.notation.python
4242
keyword.control.flow.python
43+
keyword.control.import.python
4344
keyword.illegal.name.python
4445
keyword.operator.arithmetic.python
4546
keyword.operator.assignment.python

test/expressions/keywords.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
: source.python
3232
if : keyword.control.flow.python, source.python
3333
: source.python
34-
import : keyword.control.flow.python, source.python
34+
import : keyword.control.import.python, source.python
3535
: source.python
3636
except : keyword.control.flow.python, source.python
3737
: source.python

test/statements/import1.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33

44

55

6-
from : keyword.control.flow.python, source.python
6+
from : keyword.control.import.python, source.python
77
... : source.python
88
foo : source.python
99
: source.python
10-
import : keyword.control.flow.python, source.python
10+
import : keyword.control.import.python, source.python
1111
: source.python
1212
bar : source.python
1313
: source.python
@@ -16,7 +16,7 @@
1616
spam : source.python
1717
, : source.python
1818
baz : source.python
19-
import : keyword.control.flow.python, source.python
19+
import : keyword.control.import.python, source.python
2020
: source.python
2121
time : source.python
2222
: source.python

test/statements/import2.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,18 @@
44

55

66

7-
from : keyword.control.flow.python, source.python
7+
from : keyword.control.import.python, source.python
88
.... : source.python
9-
import : keyword.control.flow.python, source.python
9+
import : keyword.control.import.python, source.python
1010
: source.python
1111
a : source.python
12-
from : keyword.control.flow.python, source.python
12+
from : keyword.control.import.python, source.python
1313
... : source.python
14-
import : keyword.control.flow.python, source.python
14+
import : keyword.control.import.python, source.python
1515
: source.python
1616
b : source.python
17-
from : keyword.control.flow.python, source.python
17+
from : keyword.control.import.python, source.python
1818
.. : source.python
19-
import : keyword.control.flow.python, source.python
19+
import : keyword.control.import.python, source.python
2020
: source.python
2121
c : source.python

test/statements/import3.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,27 @@
11
from....import a
22
from...import b
33
from..import c
4+
from.import d
45

56

67

7-
from : keyword.control.flow.python, source.python
8+
from : keyword.control.import.python, source.python
89
.... : source.python
9-
import : keyword.control.flow.python, source.python
10+
import : keyword.control.import.python, source.python
1011
: source.python
1112
a : source.python
12-
from : keyword.control.flow.python, source.python
13+
from : keyword.control.import.python, source.python
1314
... : source.python
14-
import : keyword.control.flow.python, source.python
15+
import : keyword.control.import.python, source.python
1516
: source.python
1617
b : source.python
17-
from : keyword.control.flow.python, source.python
18+
from : keyword.control.import.python, source.python
1819
.. : source.python
19-
import : keyword.control.flow.python, source.python
20+
import : keyword.control.import.python, source.python
2021
: source.python
2122
c : source.python
23+
from : keyword.control.import.python, source.python
24+
. : source.python
25+
import : keyword.control.import.python, source.python
26+
: source.python
27+
d : source.python

0 commit comments

Comments
 (0)