Skip to content

Commit 87b9817

Browse files
committed
empty libmodernize/__init__.py
1 parent e3d12e1 commit 87b9817

File tree

8 files changed

+123
-122
lines changed

8 files changed

+123
-122
lines changed

libmodernize/__init__.py

Lines changed: 0 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -1,113 +1,3 @@
11
from __future__ import generator_stop
22

3-
from fissix import fixer_util
4-
from fissix.pgen2 import token
5-
from fissix.pygram import python_symbols as syms
6-
from fissix.pytree import Leaf, Node
7-
83
__version__ = "0.8.1.dev0"
9-
10-
11-
def _check_future_import(node):
12-
"""If this is a future import, return set of symbols that are imported,
13-
else return None."""
14-
# node should be the import statement here
15-
if not (node.type == syms.simple_stmt and node.children):
16-
return set()
17-
node = node.children[0]
18-
# now node is the import_from node
19-
if not (
20-
node.type == syms.import_from
21-
and node.children[1].type == token.NAME
22-
and node.children[1].value == "__future__"
23-
):
24-
return set()
25-
26-
if node.children[3].type == token.LPAR:
27-
# from __future__ import (..
28-
node = node.children[4]
29-
else:
30-
# from __future__ import ...
31-
node = node.children[3]
32-
# now node is the import_as_name[s]
33-
34-
# print(python_grammar.number2symbol[node.type])
35-
if node.type == syms.import_as_names:
36-
result = set()
37-
for n in node.children:
38-
if n.type == token.NAME:
39-
result.add(n.value)
40-
elif n.type == syms.import_as_name:
41-
n = n.children[0]
42-
assert n.type == token.NAME
43-
result.add(n.value)
44-
return result
45-
elif node.type == syms.import_as_name:
46-
node = node.children[0]
47-
assert node.type == token.NAME
48-
return {node.value}
49-
elif node.type == token.NAME:
50-
return {node.value}
51-
else: # pragma: no cover
52-
assert 0, "strange import"
53-
54-
55-
def add_future(node, symbol):
56-
57-
root = fixer_util.find_root(node)
58-
59-
for idx, node in enumerate(root.children):
60-
if (
61-
node.type == syms.simple_stmt
62-
and len(node.children) > 0
63-
and node.children[0].type == token.STRING
64-
):
65-
# skip over docstring
66-
continue
67-
names = _check_future_import(node)
68-
if not names:
69-
# not a future statement; need to insert before this
70-
break
71-
if symbol in names:
72-
# already imported
73-
return
74-
75-
import_ = fixer_util.FromImport(
76-
"__future__", [Leaf(token.NAME, symbol, prefix=" ")]
77-
)
78-
79-
# Place after any comments or whitespace. (copyright, shebang etc.)
80-
import_.prefix = node.prefix
81-
node.prefix = ""
82-
83-
children = [import_, fixer_util.Newline()]
84-
root.insert_child(idx, Node(syms.simple_stmt, children))
85-
86-
87-
def is_listcomp(node):
88-
def _is_listcomp(node):
89-
return (
90-
isinstance(node, Node)
91-
and node.type == syms.atom
92-
and len(node.children) >= 2
93-
and isinstance(node.children[0], Leaf)
94-
and node.children[0].value == "["
95-
and isinstance(node.children[-1], Leaf)
96-
and node.children[-1].value == "]"
97-
)
98-
99-
def _is_noop_power_node(node):
100-
"""https://github.com/python/cpython/pull/2235 changed the node
101-
structure for fix_map / fix_filter to contain a top-level `power` node
102-
"""
103-
return (
104-
isinstance(node, Node)
105-
and node.type == syms.power
106-
and len(node.children) == 1
107-
)
108-
109-
return (
110-
_is_listcomp(node)
111-
or _is_noop_power_node(node)
112-
and _is_listcomp(node.children[0])
113-
)

libmodernize/fixes/fix_classic_division.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from fissix import fixer_base, pytree
44
from fissix.pgen2 import token
55

6-
import libmodernize
6+
from .. import utils
77

88

99
class FixClassicDivision(fixer_base.BaseFix):
@@ -21,7 +21,7 @@ def match(self, node):
2121
def transform(self, node, results):
2222
if self.skip:
2323
return
24-
libmodernize.add_future(node, "division")
24+
utils.add_future(node, "division")
2525

2626
if node.value == "/":
2727
return pytree.Leaf(token.DOUBLESLASH, "//", prefix=node.prefix)

libmodernize/fixes/fix_filter.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from fissix import fixer_util
77
from fissix.fixes import fix_filter
88

9-
import libmodernize
9+
from .. import utils
1010

1111

1212
class FixFilter(fix_filter.FixFilter):
@@ -15,7 +15,7 @@ class FixFilter(fix_filter.FixFilter):
1515

1616
def transform(self, node, results):
1717
result = super().transform(node, results)
18-
if not libmodernize.is_listcomp(result):
18+
if not utils.is_listcomp(result):
1919
# Keep performance improvement from six.moves.filter in iterator
2020
# contexts on Python 2.7.
2121
fixer_util.touch_import("six.moves", "filter", node)

libmodernize/fixes/fix_import.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from fissix.fixer_util import syms
44
from fissix.fixes import fix_import
55

6-
import libmodernize
6+
from .. import utils
77

88

99
class FixImport(fix_import.FixImport):
@@ -23,5 +23,5 @@ def transform(self, node, results):
2323
return
2424

2525
# If there are any non-future imports, add absolute_import
26-
libmodernize.add_future(node, "absolute_import")
26+
utils.add_future(node, "absolute_import")
2727
return super().transform(node, results)

libmodernize/fixes/fix_map.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from fissix import fixer_util
77
from fissix.fixes import fix_map
88

9-
import libmodernize
9+
from .. import utils
1010

1111

1212
class FixMap(fix_map.FixMap):
@@ -15,7 +15,7 @@ class FixMap(fix_map.FixMap):
1515

1616
def transform(self, node, results):
1717
result = super().transform(node, results)
18-
if not libmodernize.is_listcomp(result):
18+
if not utils.is_listcomp(result):
1919
# Always use the import even if no change is required so as to have
2020
# improved performance in iterator contexts even on Python 2.7.
2121
fixer_util.touch_import("six.moves", "map", node)

libmodernize/fixes/fix_print.py

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

33
from fissix.fixes import fix_print
44

5-
import libmodernize
5+
from .. import utils
66

77

88
class FixPrint(fix_print.FixPrint):
99
def transform(self, node, results):
1010
result = super().transform(node, results)
11-
libmodernize.add_future(node, "print_function")
11+
utils.add_future(node, "print_function")
1212
return result

libmodernize/fixes/fix_unicode_future.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22

33
from fissix.fixes import fix_unicode
44

5-
import libmodernize
5+
from .. import utils
66

77

88
class FixUnicodeFuture(fix_unicode.FixUnicode):
99
def transform(self, node, results):
1010
res = super().transform(node, results)
1111
if res:
12-
libmodernize.add_future(node, "unicode_literals")
12+
utils.add_future(node, "unicode_literals")
1313
return res

libmodernize/utils.py

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
from __future__ import generator_stop
2+
3+
from fissix import fixer_util
4+
from fissix.pgen2 import token
5+
from fissix.pygram import python_symbols as syms
6+
from fissix.pytree import Leaf, Node
7+
8+
9+
def _check_future_import(node):
10+
"""If this is a future import, return set of symbols that are imported,
11+
else return None."""
12+
# node should be the import statement here
13+
if not (node.type == syms.simple_stmt and node.children):
14+
return set()
15+
node = node.children[0]
16+
# now node is the import_from node
17+
if not (
18+
node.type == syms.import_from
19+
and node.children[1].type == token.NAME
20+
and node.children[1].value == "__future__"
21+
):
22+
return set()
23+
24+
if node.children[3].type == token.LPAR:
25+
# from __future__ import (..
26+
node = node.children[4]
27+
else:
28+
# from __future__ import ...
29+
node = node.children[3]
30+
# now node is the import_as_name[s]
31+
32+
# print(python_grammar.number2symbol[node.type])
33+
if node.type == syms.import_as_names:
34+
result = set()
35+
for n in node.children:
36+
if n.type == token.NAME:
37+
result.add(n.value)
38+
elif n.type == syms.import_as_name:
39+
n = n.children[0]
40+
assert n.type == token.NAME
41+
result.add(n.value)
42+
return result
43+
elif node.type == syms.import_as_name:
44+
node = node.children[0]
45+
assert node.type == token.NAME
46+
return {node.value}
47+
elif node.type == token.NAME:
48+
return {node.value}
49+
else: # pragma: no cover
50+
assert 0, "strange import"
51+
52+
53+
def add_future(node, symbol):
54+
55+
root = fixer_util.find_root(node)
56+
57+
for idx, node in enumerate(root.children):
58+
if (
59+
node.type == syms.simple_stmt
60+
and len(node.children) > 0
61+
and node.children[0].type == token.STRING
62+
):
63+
# skip over docstring
64+
continue
65+
names = _check_future_import(node)
66+
if not names:
67+
# not a future statement; need to insert before this
68+
break
69+
if symbol in names:
70+
# already imported
71+
return
72+
73+
import_ = fixer_util.FromImport(
74+
"__future__", [Leaf(token.NAME, symbol, prefix=" ")]
75+
)
76+
77+
# Place after any comments or whitespace. (copyright, shebang etc.)
78+
import_.prefix = node.prefix
79+
node.prefix = ""
80+
81+
children = [import_, fixer_util.Newline()]
82+
root.insert_child(idx, Node(syms.simple_stmt, children))
83+
84+
85+
def is_listcomp(node):
86+
def _is_listcomp(node):
87+
return (
88+
isinstance(node, Node)
89+
and node.type == syms.atom
90+
and len(node.children) >= 2
91+
and isinstance(node.children[0], Leaf)
92+
and node.children[0].value == "["
93+
and isinstance(node.children[-1], Leaf)
94+
and node.children[-1].value == "]"
95+
)
96+
97+
def _is_noop_power_node(node):
98+
"""https://github.com/python/cpython/pull/2235 changed the node
99+
structure for fix_map / fix_filter to contain a top-level `power` node
100+
"""
101+
return (
102+
isinstance(node, Node)
103+
and node.type == syms.power
104+
and len(node.children) == 1
105+
)
106+
107+
return (
108+
_is_listcomp(node)
109+
or _is_noop_power_node(node)
110+
and _is_listcomp(node.children[0])
111+
)

0 commit comments

Comments
 (0)