Skip to content

Commit 25d2f80

Browse files
authored
Add Black to Spotless check (#133)
Check Python file formatting using Spotless and Black. * Use print() instead of print. Black is complaining. * Format Python code with black. * Add black to CI. * Modify black config. * Format Python files. * Use Black version 24.1.1. * Apply spotless. * Use pip to install Black on Travis CI. * Upgrade Travis CI top Jammy. * Use pip to install Black on GH Actions. * Split Black and Python in GH Actions. Was getting an error. * Fix test. Due to formatting. * Try `requirements.txt`. * Install Python dependencies on GH Actions. * Explicitly install requirements on Travis CI. * Change Black config. * Remove direct Black calls.
1 parent 8f826d3 commit 25d2f80

File tree

220 files changed

+1806
-1087
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

220 files changed

+1806
-1087
lines changed

.github/workflows/continuous-integration.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,13 @@ jobs:
3737
pushd ${{ runner.temp }}/IDE/com.ibm.wala.cast.lsp
3838
mvn clean install -B -q -DskipTests
3939
popd
40+
- name: Install Python.
41+
uses: actions/setup-python@v5
42+
with:
43+
python-version: '3.10'
44+
cache: 'pip'
45+
- name: Install Python dependencies.
46+
run: pip install -r requirements.txt
4047
- name: Check formatting with spotless.
4148
run: mvn spotless:check -B
4249
- name: Build with Maven

.travis.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
os: linux
2-
dist: focal
2+
dist: jammy
33
language: java
44
jdk: openjdk11
55
before_install:
@@ -15,6 +15,7 @@ install:
1515
- pushd /tmp/IDE/com.ibm.wala.cast.lsp
1616
- mvn clean install -B -q -DskipTests
1717
- popd
18+
- pip install -r requirements.txt
1819
script:
1920
- mvn spotless:check -B
2021
- mvn -Dlogging.config.file=\${maven.multiModuleProjectDirectory}/logging.ci.properties clean verify -B
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
def greet(name: str, age: int) -> str:
2-
print('Hello {0}, you are {1} years old'.format(name, age))
3-
2+
print("Hello {0}, you are {1} years old".format(name, age))
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import sys
22

33
try:
4-
f = open('myfile.txt')
4+
f = open("myfile.txt")
55
s = f.readline()
66
i = int(s.strip())
77
except IOError as e:
8-
print "I/O error:"
9-
print "{0}: {1}".format(e.errno, e.strerror)
8+
print("I/O error:")
9+
print("{0}: {1}".format(e.errno, e.strerror))
1010
except ValueError:
11-
print "Could not convert data to an integer."
11+
print("Could not convert data to an integer.")
1212
except:
13-
print "Unexpected error:", sys.exc_info()[0]
13+
print("Unexpected error:", sys.exc_info()[0])
1414
raise
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
def greet(name: str, age: int) -> str:
2-
print('Hello {0}, you are {1} years old'.format(name, age))
3-
2+
print("Hello {0}, you are {1} years old".format(name, age))
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import sys
22

33
try:
4-
f = open('myfile.txt')
4+
f = open("myfile.txt")
55
s = f.readline()
66
i = int(s.strip())
77
except IOError as e:
8-
print "I/O error:"
9-
print "{0}: {1}".format(e.errno, e.strerror)
8+
print("I/O error:")
9+
print("{0}: {1}".format(e.errno, e.strerror))
1010
except ValueError:
11-
print "Could not convert data to an integer."
11+
print("Could not convert data to an integer.")
1212
except:
13-
print "Unexpected error:", sys.exc_info()[0]
13+
print("Unexpected error:", sys.exc_info()[0])
1414
raise

com.ibm.wala.cast.python.ml.test/source/com/ibm/wala/cast/python/ml/test/TestTensorflowModelParsing.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ public void testParsing()
107107
Position parameterPosition = method.getParameterPosition(paramIndex);
108108

109109
// check the line.
110-
assertEquals(3, parameterPosition.getFirstLine());
110+
assertEquals(4, parameterPosition.getFirstLine());
111111

112112
// check the columns.
113113
if (lpk.getValueNumber() == 2) {
Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
11
def MAGIC_EQ(x, y):
2-
return x == y
2+
return x == y
3+
34

45
def Banana():
5-
c = 5
6-
b = c + 7
7-
d = 10
8-
a = 0
9-
if (MAGIC_EQ(c, b)):
10-
a = 2 * c
11-
else:
12-
a = c + 6
6+
c = 5
7+
b = c + 7
8+
d = 10
9+
a = 0
10+
if MAGIC_EQ(c, b):
11+
a = 2 * c
12+
else:
13+
a = c + 6
14+
15+
a = b + c
16+
if MAGIC_EQ(a, d):
17+
raise ("Oh no")
18+
1319

14-
a = b + c
15-
if (MAGIC_EQ(a, d)):
16-
raise("Oh no")
1720
Apple()
18-
Banana()
21+
Banana()

com.ibm.wala.cast.python.test/data/annotations1.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,27 @@
33

44
app = Flask(__name__)
55

6+
67
def outer(outer_arg):
7-
outer_ret_val = outer_arg + 'hey'
8+
outer_ret_val = outer_arg + "hey"
89
return outer_ret_val
910

11+
1012
def inner(inner_arg):
11-
inner_ret_val = inner_arg + 'hey'
13+
inner_ret_val = inner_arg + "hey"
1214
return inner_ret_val
1315

14-
@app.route('/menu', methods=['POST'])
16+
17+
@app.route("/menu", methods=["POST"])
1518
def menu():
16-
req_param = request.form['suggestion']
19+
req_param = request.form["suggestion"]
1720
result = outer(inner("self isolating string"))
1821
subprocess.call(result, shell=True)
1922

2023
result = outer(inner(req_param))
2124
subprocess.call(result, shell=True)
2225

23-
with open('menu.txt','r') as f:
26+
with open("menu.txt", "r") as f:
2427
menu = f.read()
2528

26-
return render_template('command_injection.html', menu=menu)
29+
return render_template("command_injection.html", menu=menu)

com.ibm.wala.cast.python.test/data/annotations2.py

Lines changed: 61 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,36 @@
11
import pytest
22

3+
34
@pytest.mark.parametrize(
45
"index", [[1, 2], [1.0, 2.0], ["a", "b"], ["1", "2"], ["1.", "2."]]
56
)
67
def test_param_one(index):
78
def foo(x):
89
print(type(x[0]))
9-
10+
1011
foo(index)
1112

13+
1214
def test_no_params():
1315
def none():
1416
print("none")
1517

1618
none()
17-
19+
20+
1821
def some_callee():
1922
return "some callee"
2023

24+
2125
@pytest.fixture
2226
def testparam():
2327
return some_callee
2428

29+
2530
def test_param_local(testparam):
2631
print("testparam is " + str(testparam()))
27-
32+
33+
2834
class TestTestClass:
2935
def show(self, x):
3036
print(str(x))
@@ -35,91 +41,118 @@ def test_param_two(self, value):
3541

3642

3743
class notATestClass:
38-
3944
@pytest.mark.parametrize("value", [1, "a", True])
4045
def test_param_three(self, value):
4146
print(value)
4247

48+
4349
@pytest.mark.parametrize(
4450
"index", [[1, 2], [1.0, 2.0], ["a", "b"], ["1", "2"], ["1.", "2."]]
4551
)
46-
@pytest.mark.parametrize(
47-
"columns", [["a", "b"], ["1", "2"], ["1.", "2."]]
48-
)
52+
@pytest.mark.parametrize("columns", [["a", "b"], ["1", "2"], ["1.", "2."]])
4953
def test_param_three(index, columns):
5054
def f(x, y):
5155
print("(" + str(x) + "," + str(y) + ")")
52-
56+
5357
for x in index:
5458
for y in columns:
5559
f(x, y)
5660

61+
5762
import pandas_shim as pd
5863

64+
5965
@pytest.mark.parametrize(
60-
"name, expected_obj",[("pandas.isnull", pd.isnull), ("pandas.DataFrame", pd.DataFrame)]
66+
"name, expected_obj",
67+
[("pandas.isnull", pd.isnull), ("pandas.DataFrame", pd.DataFrame)],
6168
)
6269
def test_param_four(name, expected_obj):
6370
print("(" + str(name) + "," + str(expected_obj(name)) + ")")
6471

72+
6573
@pytest.mark.parametrize(
66-
"values", [pd.Categorical([]), pd.Categorical([]).dtype, pd.Series(pd.Categorical([]))]
74+
"values",
75+
[pd.Categorical([]), pd.Categorical([]).dtype, pd.Series(pd.Categorical([]))],
6776
)
6877
def test_param_five(values):
6978
def f(x):
7079
print(str(x))
71-
80+
7281
f(values)
73-
82+
83+
7484
@pytest.mark.parametrize("ascending", [True, False])
7585
def test_param_six(ascending):
7686
def f(x):
7787
print(str(x))
7888

7989
f(ascending)
80-
90+
91+
8192
@pytest.mark.parametrize("box", [pd.Series, lambda x: x])
8293
def test_box(box):
8394
def f(x):
8495
print(str(x(0)))
8596

8697
f(box)
87-
98+
99+
88100
@pytest.mark.parametrize("repeats", [0, 1, 2, [1, 2, 3]])
89101
def test_repeat(repeats):
90102
def f(x):
91103
print(str(x))
92104

93105
f(repeats)
94106

107+
95108
from pandas_shim import DecimalArrayWithoutFromSequence, DecimalArrayWithoutCoercion
96109

97-
@pytest.mark.parametrize("class_", [DecimalArrayWithoutFromSequence, DecimalArrayWithoutCoercion])
110+
111+
@pytest.mark.parametrize(
112+
"class_", [DecimalArrayWithoutFromSequence, DecimalArrayWithoutCoercion]
113+
)
98114
def test_class(class_):
99-
print(class_().foo())
115+
print(class_().foo())
116+
100117

101-
@pytest.mark.parametrize("columns",[["A", "B"], pd.MultiIndex.from_tuples([("A", "a"), ("A", "b")], names=["outer", "inner"])])
118+
@pytest.mark.parametrize(
119+
"columns",
120+
[
121+
["A", "B"],
122+
pd.MultiIndex.from_tuples([("A", "a"), ("A", "b")], names=["outer", "inner"]),
123+
],
124+
)
102125
def test_cols(columns):
103-
print("(" + str(columns) + ")")
126+
print("(" + str(columns) + ")")
127+
104128

105-
#@pytest.mark.parametrize("repeats, kwargs, error, msg",[
106-
#(2, dict(axis=1), ValueError, "'axis"),
107-
#(-1, dict(), ValueError, "negative"),
108-
#([1, 2], dict(), ValueError, "shape")])
109-
#def test_dict(repeats, kwargs, error, msg):
129+
# @pytest.mark.parametrize("repeats, kwargs, error, msg",[
130+
# (2, dict(axis=1), ValueError, "'axis"),
131+
# (-1, dict(), ValueError, "negative"),
132+
# ([1, 2], dict(), ValueError, "shape")])
133+
# def test_dict(repeats, kwargs, error, msg):
110134
# print("(" + str(repeats) + " " + str(kwargs) + " " + str(error) + " " + msg + ")")
111135

112136
from pandas_shim import DataFrame
113137
import np_shim as np
114138

115-
@pytest.mark.parametrize("header,expected",[(None, DataFrame([0] + [np.nan] * 4)), (0, DataFrame([np.nan] * 4))])
139+
140+
@pytest.mark.parametrize(
141+
"header,expected",
142+
[(None, DataFrame([0] + [np.nan] * 4)), (0, DataFrame([np.nan] * 4))],
143+
)
116144
def test_head_expected(header, expected):
117-
print("(" + str(header) + " " + str(expected))
145+
print("(" + str(header) + " " + str(expected))
118146

119147

120-
@pytest.mark.parametrize("option,result,expected",[ (None, lambda df: df.to_html(), "1"), (None, lambda df: df.to_html(border=0), "0"), (0, lambda df: df.to_html(), "0")])
148+
@pytest.mark.parametrize(
149+
"option,result,expected",
150+
[
151+
(None, lambda df: df.to_html(), "1"),
152+
(None, lambda df: df.to_html(border=0), "0"),
153+
(0, lambda df: df.to_html(), "0"),
154+
],
155+
)
121156
def test_lamda_call(option, result, expected):
122157
df = DataFrame({"A": [1, 2]})
123158
print("(" + str(option) + " " + str(result(df)) + " " + str(expected))
124-
125-

0 commit comments

Comments
 (0)