Skip to content

Commit f81179b

Browse files
authored
Add Black format checking (#113)
* Run black on all python files * Add black check to CI * Fix CI order
1 parent 4d610f6 commit f81179b

27 files changed

+1940
-1740
lines changed

.github/workflows/python-package.yml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,15 @@ jobs:
1111
python-version: [3.7, 3.8]
1212

1313
steps:
14+
1415
- uses: actions/checkout@v2
1516
with:
1617
path: scanpy-scripts
17-
18+
19+
- uses: psf/black@stable
20+
with:
21+
options: '--check --verbose --include="\.pyi?$" .'
22+
1823
- uses: actions/checkout@v2
1924
with:
2025
repository: theislab/scanpy

scanpy_scripts/__init__.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@
33
"""
44
import pkg_resources
55

6-
__version__ = pkg_resources.get_distribution('scanpy-scripts').version
6+
__version__ = pkg_resources.get_distribution("scanpy-scripts").version
77

8-
__author__ = ', '.join([
9-
'Ni Huang',
10-
'Pablo Moreno',
11-
'Jonathan Manning',
12-
'Philipp Angerer',
13-
])
8+
__author__ = ", ".join(
9+
[
10+
"Ni Huang",
11+
"Pablo Moreno",
12+
"Jonathan Manning",
13+
"Philipp Angerer",
14+
]
15+
)
1416

1517
from . import lib

scanpy_scripts/cli.py

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -42,20 +42,20 @@
4242

4343
@click.group(cls=NaturalOrderGroup)
4444
@click.option(
45-
'--debug',
45+
"--debug",
4646
is_flag=True,
4747
default=False,
48-
help='Print debug information',
48+
help="Print debug information",
4949
)
5050
@click.option(
51-
'--verbosity',
51+
"--verbosity",
5252
type=click.INT,
5353
default=3,
54-
help='Set scanpy verbosity',
54+
help="Set scanpy verbosity",
5555
)
5656
@click.version_option(
57-
version='0.2.0',
58-
prog_name='scanpy',
57+
version="0.2.0",
58+
prog_name="scanpy",
5959
)
6060
def cli(debug=False, verbosity=3):
6161
"""
@@ -64,11 +64,12 @@ def cli(debug=False, verbosity=3):
6464
log_level = logging.DEBUG if debug else logging.INFO
6565
logging.basicConfig(
6666
level=log_level,
67-
format=('%(asctime)s; %(levelname)s; %(filename)s; '
68-
'%(funcName)s(): %(message)s'),
69-
datefmt='%y-%m-%d %H:%M:%S',
67+
format=(
68+
"%(asctime)s; %(levelname)s; %(filename)s; " "%(funcName)s(): %(message)s"
69+
),
70+
datefmt="%y-%m-%d %H:%M:%S",
7071
)
71-
logging.debug('debugging')
72+
logging.debug("debugging")
7273
sc.settings.verbosity = verbosity
7374
return 0
7475

@@ -112,15 +113,18 @@ def cluster():
112113
def integrate():
113114
"""Integrate cells from different experimental batches."""
114115

116+
115117
integrate.add_command(HARMONY_INTEGRATE_CMD)
116118
integrate.add_command(BBKNN_CMD)
117119
integrate.add_command(MNN_CORRECT_CMD)
118120
integrate.add_command(COMBAT_CMD)
119121

122+
120123
@cli.group(cls=NaturalOrderGroup)
121124
def multiplet():
122125
"""Execute methods for multiplet removal."""
123126

127+
124128
multiplet.add_command(SCRUBLET_MULTIPLET_CMD)
125129
multiplet.add_command(SCRUBLET_MULTIPLET_SIMULATE_CMD)
126130

scanpy_scripts/click_utils.py

Lines changed: 42 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ class NaturalOrderGroup(click.Group):
1313
1414
@click.group(cls=NaturalOrderGroup)
1515
"""
16+
1617
def list_commands(self, ctx):
1718
"""List command names as they are in commands dict.
1819
@@ -26,15 +27,16 @@ class CommaSeparatedText(click.ParamType):
2627
"""
2728
Comma separated text
2829
"""
30+
2931
def __init__(self, dtype=click.STRING, simplify=False, length=None):
3032
self.dtype = dtype
3133
self.dtype_name = _get_type_name(dtype)
3234
self.simplify = simplify
3335
self.length = length
3436
if length and length <= 3:
35-
self.name = ','.join([f'{self.dtype_name}'] * length)
37+
self.name = ",".join([f"{self.dtype_name}"] * length)
3638
else:
37-
self.name = '{}[,{}...]'.format(self.dtype_name, self.dtype_name)
39+
self.name = "{}[,{}...]".format(self.dtype_name, self.dtype_name)
3840

3941
def convert(self, value, param, ctx):
4042
"""
@@ -67,23 +69,25 @@ def convert(self, value, param, ctx):
6769
if value is None:
6870
converted = None
6971
else:
70-
converted = list(map(self.dtype, str(value).split(',')))
72+
converted = list(map(self.dtype, str(value).split(",")))
7173
if self.simplify and len(converted) == 1:
7274
converted = converted[0]
7375
except ValueError:
7476
self.fail(
75-
'{} is not a valid comma separated list of {}'.format(
76-
value, self.dtype_name),
77+
"{} is not a valid comma separated list of {}".format(
78+
value, self.dtype_name
79+
),
7780
param,
78-
ctx
81+
ctx,
7982
)
8083
if self.length:
8184
if len(converted) != self.length:
8285
self.fail(
83-
'{} is not a valid comma separated list of length {}'.format(
84-
value, self.length),
86+
"{} is not a valid comma separated list of length {}".format(
87+
value, self.length
88+
),
8589
param,
86-
ctx
90+
ctx,
8791
)
8892
return converted
8993

@@ -92,8 +96,9 @@ class Dictionary(click.ParamType):
9296
"""
9397
Text to be parsed as a python dict definition
9498
"""
99+
95100
def __init__(self, keys=None):
96-
self.name = 'TEXT:VAL[,TEXT:VAL...]'
101+
self.name = "TEXT:VAL[,TEXT:VAL...]"
97102
self.keys = keys
98103

99104
def convert(self, value, param, ctx):
@@ -122,19 +127,19 @@ def convert(self, value, param, ctx):
122127
"""
123128
try:
124129
converted = dict()
125-
for token in value.split(','):
126-
if ':' not in token:
130+
for token in value.split(","):
131+
if ":" not in token:
127132
raise ValueError
128-
key, _, value = token.partition(':')
133+
key, _, value = token.partition(":")
129134
if not key:
130135
raise ValueError
131136
if isinstance(self.keys, (list, tuple)) and key not in self.keys:
132-
self.fail(f'{key} is not a valid key ({self.keys})')
133-
if value == 'None':
137+
self.fail(f"{key} is not a valid key ({self.keys})")
138+
if value == "None":
134139
value = None
135-
elif value.lower() == 'true':
140+
elif value.lower() == "true":
136141
value = True
137-
elif value.lower() == 'false':
142+
elif value.lower() == "false":
138143
value = False
139144
else:
140145
try:
@@ -144,19 +149,15 @@ def convert(self, value, param, ctx):
144149
converted[key] = value
145150
return converted
146151
except ValueError:
147-
self.fail(
148-
f'{value} is not a valid python dict definition',
149-
param,
150-
ctx
151-
)
152+
self.fail(f"{value} is not a valid python dict definition", param, ctx)
152153

153154

154155
def _get_type_name(obj):
155-
name = 'text'
156+
name = "text"
156157
try:
157-
name = getattr(obj, 'name')
158+
name = getattr(obj, "name")
158159
except AttributeError:
159-
name = getattr(obj, '__name__')
160+
name = getattr(obj, "__name__")
160161
return name
161162

162163

@@ -181,8 +182,7 @@ def valid_limit(ctx, param, value):
181182
[0.0125, 0.0125]
182183
"""
183184
if value[0] > value[1]:
184-
param.type.fail(
185-
'lower limit must not exceed upper limit', param, ctx)
185+
param.type.fail("lower limit must not exceed upper limit", param, ctx)
186186
return value
187187

188188

@@ -212,13 +212,13 @@ def valid_parameter_limits(ctx, param, value):
212212
"""
213213
for val in value:
214214
if val[1] > val[2]:
215-
param.type.fail(
216-
'lower limit must not exceed upper limit', param, ctx)
215+
param.type.fail("lower limit must not exceed upper limit", param, ctx)
217216
return value
218217

219218

220219
def mutually_exclusive_with(param_name):
221-
internal_name = param_name.strip('-').replace('-', '_').lower()
220+
internal_name = param_name.strip("-").replace("-", "_").lower()
221+
222222
def valid_mutually_exclusive(ctx, param, value):
223223
try:
224224
other_value = ctx.params[internal_name]
@@ -227,27 +227,35 @@ def valid_mutually_exclusive(ctx, param, value):
227227
if (value is None) == (other_value is None):
228228
param.type.fail(
229229
'mutually exclusive with "{}", one and only one must be '
230-
'specified.'.format(param_name),
230+
"specified.".format(param_name),
231231
param,
232232
ctx,
233233
)
234234
return value
235+
235236
return valid_mutually_exclusive
236237

237238

238239
def required_by(param_name):
239-
internal_name = param_name.strip('-').replace('-', '_').lower()
240+
internal_name = param_name.strip("-").replace("-", "_").lower()
241+
240242
def required(ctx, param, value):
241243
try:
242244
other_value = ctx.params[internal_name]
243245
except KeyError:
244246
return value
245247
if other_value and not value:
246-
param.type.fail('required by "{}".'.format(param_name), param, ctx,)
248+
param.type.fail(
249+
'required by "{}".'.format(param_name),
250+
param,
251+
ctx,
252+
)
247253
return value
254+
248255
return required
249256

250257

251-
if __name__ == '__main__':
258+
if __name__ == "__main__":
252259
import doctest
260+
253261
sys.exit(doctest.testmod(verbose=True)[0])

0 commit comments

Comments
 (0)