Skip to content

Commit f5fa7ab

Browse files
committed
chore: noqa for mccabe complexity in sa.editor
I plan on a rewrite, not a refactor here #596 (comment)
1 parent 6f8e272 commit f5fa7ab

4 files changed

Lines changed: 230 additions & 163 deletions

File tree

scratchattach/editor/block.py

Lines changed: 51 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,27 @@ class Block(base.SpriteSubComponent):
1212
"""
1313
Represents a block in the scratch editor, as a subcomponent of a sprite.
1414
"""
15+
1516
_id: Optional[str] = None
16-
def __init__(self, _opcode: str, _shadow: bool = False, _top_level: Optional[bool] = None,
17-
_mutation: Optional[mutation.Mutation] = None, _fields: Optional[dict[str, field.Field]] = None,
18-
_inputs: Optional[dict[str, inputs.Input]] = None, x: int = 0, y: int = 0, pos: Optional[tuple[int, int]] = None,
1917

20-
_next: Optional[Block] = None, _parent: Optional[Block] = None,
21-
*, _next_id: Optional[str] = None, _parent_id: Optional[str] = None, _sprite: commons.SpriteInput = build_defaulting.SPRITE_DEFAULT):
18+
def __init__(
19+
self,
20+
_opcode: str,
21+
_shadow: bool = False,
22+
_top_level: Optional[bool] = None,
23+
_mutation: Optional[mutation.Mutation] = None,
24+
_fields: Optional[dict[str, field.Field]] = None,
25+
_inputs: Optional[dict[str, inputs.Input]] = None,
26+
x: int = 0,
27+
y: int = 0,
28+
pos: Optional[tuple[int, int]] = None,
29+
_next: Optional[Block] = None,
30+
_parent: Optional[Block] = None,
31+
*,
32+
_next_id: Optional[str] = None,
33+
_parent_id: Optional[str] = None,
34+
_sprite: commons.SpriteInput = build_defaulting.SPRITE_DEFAULT,
35+
):
2236
# Defaulting for args
2337
if _fields is None:
2438
_fields = {}
@@ -166,7 +180,7 @@ def id(self) -> str:
166180
# Let's just automatically assign ourselves an id
167181
self.sprite.add_block(self)
168182
return self._id
169-
183+
170184
@id.setter
171185
def id(self, value: str) -> None:
172186
self._id = value
@@ -229,7 +243,7 @@ def previous_chain(self):
229243
Recursive getter method to get all previous blocks in the blockchain (until hitting a top-level block)
230244
"""
231245
# TODO: check if this hits the recursion limit
232-
if self.parent is None: # TODO: use is_top_level?
246+
if self.parent is None: # TODO: use is_top_level?
233247
return [self]
234248

235249
return [self] + self.parent.previous_chain
@@ -289,7 +303,7 @@ def category(self):
289303
"""
290304
Works out what category of block this is as a string, using the opcode. Does not perform validation
291305
"""
292-
return self.opcode.split('_')[0]
306+
return self.opcode.split("_")[0]
293307

294308
@property
295309
def is_input(self):
@@ -333,7 +347,7 @@ def comment(self) -> comment.Comment | None:
333347
return None
334348

335349
@property
336-
def turbowarp_block_opcode(self):
350+
def turbowarp_block_opcode(self): # noqa: C901
337351
"""
338352
:return: The 'opcode' if this is a turbowarp block: e.g.
339353
- log
@@ -349,13 +363,13 @@ def turbowarp_block_opcode(self):
349363
if self.mutation:
350364
if self.mutation.proc_code:
351365
# \u200B is a zero-width space
352-
if self.mutation.proc_code == "\u200B\u200Bbreakpoint\u200B\u200B":
366+
if self.mutation.proc_code == "\u200b\u200bbreakpoint\u200b\u200b":
353367
return "breakpoint"
354-
elif self.mutation.proc_code == "\u200B\u200Blog\u200B\u200B %s":
368+
elif self.mutation.proc_code == "\u200b\u200blog\u200b\u200b %s":
355369
return "log"
356-
elif self.mutation.proc_code == "\u200B\u200Berror\u200B\u200B %s":
370+
elif self.mutation.proc_code == "\u200b\u200berror\u200b\u200b %s":
357371
return "error"
358-
elif self.mutation.proc_code == "\u200B\u200Bwarn\u200B\u200B %s":
372+
elif self.mutation.proc_code == "\u200b\u200bwarn\u200b\u200b %s":
359373
return "warn"
360374

361375
elif self.opcode == "argument_reporter_boolean":
@@ -414,8 +428,9 @@ def from_json(data: dict) -> Block:
414428
else:
415429
_mutation = None
416430

417-
return Block(_opcode, _shadow, _top_level, _mutation, _fields, _inputs, _x, _y, _next_id=_next_id,
418-
_parent_id=_parent_id)
431+
return Block(
432+
_opcode, _shadow, _top_level, _mutation, _fields, _inputs, _x, _y, _next_id=_next_id, _parent_id=_parent_id
433+
)
419434

420435
def to_json(self) -> dict:
421436
"""
@@ -434,24 +449,28 @@ def to_json(self) -> dict:
434449
}
435450
_comment = self.comment
436451
if _comment:
437-
commons.noneless_update(_json, {
438-
"comment": _comment.id
439-
})
452+
commons.noneless_update(_json, {"comment": _comment.id})
440453

441454
if self.is_top_level:
442-
commons.noneless_update(_json, {
443-
"x": self.x,
444-
"y": self.y,
445-
})
455+
commons.noneless_update(
456+
_json,
457+
{
458+
"x": self.x,
459+
"y": self.y,
460+
},
461+
)
446462

447463
if self.mutation is not None:
448-
commons.noneless_update(_json, {
449-
"mutation": self.mutation.to_json(),
450-
})
464+
commons.noneless_update(
465+
_json,
466+
{
467+
"mutation": self.mutation.to_json(),
468+
},
469+
)
451470

452471
return _json
453472

454-
def link_using_sprite(self, link_subs: bool = True):
473+
def link_using_sprite(self, link_subs: bool = True): # noqa: C901
455474
"""
456475
Link this block to various other blocks once the sprite has been assigned
457476
"""
@@ -483,20 +502,18 @@ def link_using_sprite(self, link_subs: bool = True):
483502

484503
if _type == field.Types.VARIABLE:
485504
# Create a new variable
486-
new_value = vlb.Variable(commons.gen_id(),
487-
_field.value)
505+
new_value = vlb.Variable(commons.gen_id(), _field.value)
488506
elif _type == field.Types.LIST:
489507
# Create a list
490-
new_value = vlb.List(commons.gen_id(),
491-
_field.value)
508+
new_value = vlb.List(commons.gen_id(), _field.value)
492509
elif _type == field.Types.BROADCAST:
493510
# Create a broadcast
494-
new_value = vlb.Broadcast(commons.gen_id(),
495-
_field.value)
511+
new_value = vlb.Broadcast(commons.gen_id(), _field.value)
496512
else:
497513
# Something probably went wrong
498514
warnings.warn(
499-
f"Could not find {_field.id!r} in {self.sprite}. Can't create a new {_type} so we gave a warning")
515+
f"Could not find {_field.id!r} in {self.sprite}. Can't create a new {_type} so we gave a warning"
516+
)
500517

501518
if new_value is not None:
502519
self.sprite.add_local_global(new_value)
@@ -540,9 +557,7 @@ def attach_chain(self, *chain: Block) -> Block:
540557
return attaching_block
541558

542559
def duplicate_chain(self) -> Block:
543-
return self.bottom_level_block.attach_chain(
544-
*map(Block.dcopy, self.attached_chain)
545-
)
560+
return self.bottom_level_block.attach_chain(*map(Block.dcopy, self.attached_chain))
546561

547562
def slot_above(self, new: Block) -> Block:
548563
"""

scratchattach/editor/mutation.py

Lines changed: 51 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def default(self) -> str | None:
3838
if self.proc_str == "%b":
3939
return "false"
4040
elif self.proc_str == "%s":
41-
return ''
41+
return ""
4242
else:
4343
return None
4444

@@ -48,19 +48,16 @@ class ArgSettings(base.Base):
4848
"""
4949
Contains whether the ids, names, and defaults of arguments in a mutation are None or not - i.e. the configuration of the arguments
5050
"""
51+
5152
ids: bool
5253
names: bool
5354
defaults: bool
5455

5556
def __int__(self):
56-
return (int(self.ids) +
57-
int(self.names) +
58-
int(self.defaults))
57+
return int(self.ids) + int(self.names) + int(self.defaults)
5958

6059
def __eq__(self, other):
61-
return (self.ids == other.ids and
62-
self.names == other.names and
63-
self.defaults == other.defaults)
60+
return self.ids == other.ids and self.names == other.names and self.defaults == other.defaults
6461

6562
def __gt__(self, other):
6663
return int(self) > int(other)
@@ -72,7 +69,7 @@ def __lt__(self, other):
7269
@dataclass
7370
class Argument(base.MutationSubComponent):
7471
name: str
75-
default: str = ''
72+
default: str = ""
7673
_type: Optional[ArgumentType] = None
7774

7875
_id: str = None
@@ -91,8 +88,9 @@ def index(self):
9188
def type(self) -> Optional[ArgumentType]:
9289
if not self._type:
9390
if not self.mutation:
94-
raise ValueError(f"Cannot infer 'type' of {self} when there is no mutation attached. "
95-
f"Consider providing a type manually.")
91+
raise ValueError(
92+
f"Cannot infer 'type' of {self} when there is no mutation attached. Consider providing a type manually."
93+
)
9694

9795
i = 0
9896
goal = self.index
@@ -130,22 +128,22 @@ def parse_proc_code(_proc_code: str) -> Optional[list[str | ArgumentType]]:
130128

131129
if _proc_code is None:
132130
return None
133-
token = ''
131+
token = ""
134132
tokens = []
135133

136-
last_char = ''
134+
last_char = ""
137135
for char in _proc_code:
138-
if last_char == '%':
136+
if last_char == "%":
139137
if char in "sb":
140138
# If we've hit an %s or %b
141139
token = token[:-1]
142140
# Clip the % sign off the token
143141

144-
if token.endswith(' '):
142+
if token.endswith(" "):
145143
# A space is required before params, but this should not be part of the parsed output
146144
token = token[:-1]
147145

148-
if token != '':
146+
if token != "":
149147
# Make sure not to append an empty token
150148
tokens.append(token)
151149

@@ -156,17 +154,18 @@ def parse_proc_code(_proc_code: str) -> Optional[list[str | ArgumentType]]:
156154
elif token == "%s":
157155
tokens.append(ArgTypes.NUMBER_OR_TEXT.value.dcopy())
158156

159-
token = ''
157+
token = ""
160158
continue
161159

162160
token += char
163161
last_char = char
164162

165-
if token != '':
163+
if token != "":
166164
tokens.append(token)
167165

168166
return tokens
169167

168+
170169
def construct_proccode(*components: ArgumentType | ArgTypes | Argument | str) -> str:
171170
"""
172171
Create a proccode from strings/ArgumentType enum members/Argument instances
@@ -194,16 +193,24 @@ def construct_proccode(*components: ArgumentType | ArgTypes | Argument | str) ->
194193
else:
195194
raise TypeError(f"Unsupported component type: {type(comp)}")
196195

197-
result += ' '
196+
result += " "
198197

199198
return result
200199

201200

202201
class Mutation(base.BlockSubComponent):
203-
def __init__(self, _tag_name: str = "mutation", _children: Optional[list] = None, _proc_code: Optional[str] = None,
204-
_is_warp: Optional[bool] = None, _arguments: Optional[list[Argument]] = None, _has_next: Optional[bool] = None,
205-
_argument_settings: Optional[ArgSettings] = None, *,
206-
_block: Optional[block.Block] = None):
202+
def __init__(
203+
self,
204+
_tag_name: str = "mutation",
205+
_children: Optional[list] = None,
206+
_proc_code: Optional[str] = None,
207+
_is_warp: Optional[bool] = None,
208+
_arguments: Optional[list[Argument]] = None,
209+
_has_next: Optional[bool] = None,
210+
_argument_settings: Optional[ArgSettings] = None,
211+
*,
212+
_block: Optional[block.Block] = None,
213+
):
207214
"""
208215
Mutation for Control:stop block and procedures
209216
https://en.scratch-wiki.info/wiki/Scratch_File_Format#Mutations
@@ -215,9 +222,7 @@ def __init__(self, _tag_name: str = "mutation", _children: Optional[list] = None
215222
if _argument_settings is None:
216223
if _arguments:
217224
_argument_settings = ArgSettings(
218-
_arguments[0]._id is None,
219-
_arguments[0].name is None,
220-
_arguments[0].default is None
225+
_arguments[0]._id is None, _arguments[0].name is None, _arguments[0].default is None
221226
)
222227
else:
223228
_argument_settings = ArgSettings(False, False, False)
@@ -263,9 +268,11 @@ def argument_defaults(self):
263268

264269
@property
265270
def argument_settings(self) -> ArgSettings:
266-
return ArgSettings(bool(commons.safe_get(self.argument_ids, 0)),
267-
bool(commons.safe_get(self.argument_names, 0)),
268-
bool(commons.safe_get(self.argument_defaults, 0)))
271+
return ArgSettings(
272+
bool(commons.safe_get(self.argument_ids, 0)),
273+
bool(commons.safe_get(self.argument_names, 0)),
274+
bool(commons.safe_get(self.argument_defaults, 0)),
275+
)
269276

270277
@property
271278
def parsed_proc_code(self) -> list[str | ArgumentType] | None:
@@ -275,7 +282,7 @@ def parsed_proc_code(self) -> list[str | ArgumentType] | None:
275282
return parse_proc_code(self.proc_code)
276283

277284
@staticmethod
278-
def from_json(data: dict) -> Mutation:
285+
def from_json(data: dict) -> Mutation: # noqa: C901
279286
assert isinstance(data, dict)
280287

281288
_tag_name = data.get("tagName", "mutation")
@@ -302,9 +309,9 @@ def from_json(data: dict) -> Mutation:
302309
if _argument_defaults is not None:
303310
assert isinstance(_argument_defaults, str)
304311
_argument_defaults = json.loads(_argument_defaults)
305-
_argument_settings = ArgSettings(_argument_ids is not None,
306-
_argument_names is not None,
307-
_argument_defaults is not None)
312+
_argument_settings = ArgSettings(
313+
_argument_ids is not None, _argument_names is not None, _argument_defaults is not None
314+
)
308315

309316
# control_stop attrs
310317
_has_next = data.get("hasnext")
@@ -337,15 +344,17 @@ def to_json(self) -> dict | None:
337344
"tagName": self.tag_name,
338345
"children": self.children,
339346
}
340-
commons.noneless_update(_json, {
341-
"proccode": self.proc_code,
342-
"warp": commons.dumps_ifnn(self.is_warp),
343-
"argumentids": commons.dumps_ifnn(self.argument_ids),
344-
"argumentnames": commons.dumps_ifnn(self.argument_names),
345-
"argumentdefaults": commons.dumps_ifnn(self.argument_defaults),
346-
347-
"hasNext": commons.dumps_ifnn(self.has_next)
348-
})
347+
commons.noneless_update(
348+
_json,
349+
{
350+
"proccode": self.proc_code,
351+
"warp": commons.dumps_ifnn(self.is_warp),
352+
"argumentids": commons.dumps_ifnn(self.argument_ids),
353+
"argumentnames": commons.dumps_ifnn(self.argument_names),
354+
"argumentdefaults": commons.dumps_ifnn(self.argument_defaults),
355+
"hasNext": commons.dumps_ifnn(self.has_next),
356+
},
357+
)
349358

350359
return _json
351360

@@ -370,12 +379,10 @@ def link_arguments(self):
370379
# We can still work out argument defaults from parsing the proc code
371380
if self.arguments[0].default is None:
372381
_parsed = self.parsed_proc_code
373-
_arg_phs: Iterable[ArgumentType] = filter(lambda tkn: isinstance(tkn, ArgumentType),
374-
_parsed)
382+
_arg_phs: Iterable[ArgumentType] = filter(lambda tkn: isinstance(tkn, ArgumentType), _parsed)
375383
for i, _arg_ph in enumerate(_arg_phs):
376384
self.arguments[i].default = _arg_ph.default
377385

378386
for _argument in self.arguments:
379387
_argument.mutation = self
380388
_argument.link_using_mutation()
381-

0 commit comments

Comments
 (0)