Skip to content

Commit 06cd02e

Browse files
committed
perf: Avoid creating new sets when handling labels
1 parent e21851e commit 06cd02e

3 files changed

Lines changed: 30 additions & 19 deletions

File tree

‎packages/griffelib/src/griffe/_internal/agents/inspector.py‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -491,8 +491,7 @@ def handle_function(self, node: ObjectNode, labels: set | None = None) -> None:
491491
lineno, endlineno = self._get_linenos(node)
492492

493493
obj: Attribute | Function
494-
labels = labels or set()
495-
if "property" in labels:
494+
if labels and "property" in labels:
496495
obj = Attribute(
497496
name=node.name,
498497
value=None,
@@ -515,7 +514,8 @@ def handle_function(self, node: ObjectNode, labels: set | None = None) -> None:
515514
endlineno=endlineno,
516515
analysis="dynamic",
517516
)
518-
obj.labels |= labels
517+
if labels:
518+
obj.labels.update(labels)
519519
self.current.set_member(node.name, obj)
520520
self.extensions.call("on_instance", node=node, obj=obj, agent=self)
521521
if obj.is_attribute:
@@ -596,7 +596,7 @@ def handle_attribute(self, node: ObjectNode, annotation: str | Expr | None = Non
596596
docstring=docstring,
597597
analysis="dynamic",
598598
)
599-
attribute.labels |= labels
599+
attribute.labels.update(labels)
600600
parent.set_member(node.name, attribute) # ty:ignore[unresolved-attribute]
601601

602602
if node.name == "__all__":

‎packages/griffelib/src/griffe/_internal/agents/visitor.py‎

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,8 @@ def visit_classdef(self, node: ast.ClassDef) -> None:
363363
runtime=not self.type_guarded,
364364
analysis="static",
365365
)
366-
class_.labels |= self.decorators_to_labels(decorators)
366+
if decorators and (labels := self.decorators_to_labels(decorators)):
367+
class_.labels.update(labels)
367368

368369
self.current.set_member(node.name, class_)
369370
self.current = class_
@@ -426,8 +427,6 @@ def handle_function(self, node: ast.AsyncFunctionDef | ast.FunctionDef, labels:
426427
self.extensions.call("on_node", node=node, agent=self)
427428
self.extensions.call("on_function_node", node=node, agent=self)
428429

429-
labels = labels or set()
430-
431430
# Handle decorators.
432431
decorators = []
433432
overload = False
@@ -447,9 +446,13 @@ def handle_function(self, node: ast.AsyncFunctionDef | ast.FunctionDef, labels:
447446
else:
448447
lineno = node.lineno
449448

450-
labels |= self.decorators_to_labels(decorators)
449+
if decorators and (decorator_labels := self.decorators_to_labels(decorators)):
450+
if labels:
451+
labels.update(decorator_labels)
452+
else:
453+
labels = decorator_labels
451454

452-
if "property" in labels:
455+
if labels and "property" in labels:
453456
attribute = Attribute(
454457
name=node.name,
455458
value=None,
@@ -460,7 +463,7 @@ def handle_function(self, node: ast.AsyncFunctionDef | ast.FunctionDef, labels:
460463
runtime=not self.type_guarded,
461464
analysis="static",
462465
)
463-
attribute.labels |= labels
466+
attribute.labels.update(labels)
464467
self.current.set_member(node.name, attribute)
465468
self.extensions.call("on_instance", node=node, obj=attribute, agent=self)
466469
self.extensions.call("on_attribute_instance", node=node, attr=attribute, agent=self)
@@ -513,7 +516,8 @@ def handle_function(self, node: ast.AsyncFunctionDef | ast.FunctionDef, labels:
513516
function.overloads = self.current.overloads[function.name]
514517
del self.current.overloads[function.name]
515518

516-
function.labels |= labels
519+
if labels:
520+
function.labels.update(labels)
517521

518522
self.extensions.call("on_instance", node=node, obj=function, agent=self)
519523
self.extensions.call("on_function_instance", node=node, func=function, agent=self)
@@ -729,7 +733,7 @@ def handle_attribute(
729733
runtime=not self.type_guarded,
730734
analysis="static",
731735
)
732-
attribute.labels |= labels
736+
attribute.labels.update(labels)
733737
parent.set_member(name, attribute)
734738

735739
if name == "__all__":

‎packages/griffelib/src/griffe/_internal/encoders.py‎

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -234,9 +234,11 @@ def _load_module(obj_dict: dict[str, Any]) -> Module:
234234
for module_member in members:
235235
module.set_member(module_member.name, module_member)
236236
_attach_parent_to_exprs(module_member, module)
237-
module.labels |= set(obj_dict.get("labels", ()))
237+
if labels := obj_dict.get("labels"):
238+
module.labels.update(labels)
238239
module.exports = obj_dict.get("exports")
239-
module.imports = obj_dict.get("imports", {})
240+
if imports := obj_dict.get("imports"):
241+
module.imports = imports
240242
module.deprecated = obj_dict.get("deprecated")
241243
module.public = obj_dict.get("public")
242244
module.source_link = obj_dict.get("source_link")
@@ -262,8 +264,10 @@ def _load_class(obj_dict: dict[str, Any]) -> Class:
262264
for class_member in members:
263265
class_.set_member(class_member.name, class_member)
264266
_attach_parent_to_exprs(class_member, class_)
265-
class_.labels |= set(obj_dict.get("labels", ()))
266-
class_.imports = obj_dict.get("imports", {})
267+
if labels := obj_dict.get("labels"):
268+
class_.labels.update(labels)
269+
if imports := obj_dict.get("imports"):
270+
class_.imports = imports
267271
class_.deprecated = obj_dict.get("deprecated")
268272
class_.public = obj_dict.get("public")
269273
class_.source_link = obj_dict.get("source_link")
@@ -286,7 +290,8 @@ def _load_function(obj_dict: dict[str, Any]) -> Function:
286290
runtime=obj_dict.get("runtime", True),
287291
analysis=obj_dict.get("analysis"),
288292
)
289-
function.labels |= set(obj_dict.get("labels", ()))
293+
if labels := obj_dict.get("labels"):
294+
function.labels.update(labels)
290295
function.deprecated = obj_dict.get("deprecated")
291296
function.public = obj_dict.get("public")
292297
function.source_link = obj_dict.get("source_link")
@@ -305,7 +310,8 @@ def _load_attribute(obj_dict: dict[str, Any]) -> Attribute:
305310
annotation=obj_dict.get("annotation"),
306311
analysis=obj_dict.get("analysis"),
307312
)
308-
attribute.labels |= set(obj_dict.get("labels", ()))
313+
if labels := obj_dict.get("labels"):
314+
attribute.labels.update(labels)
309315
attribute.runtime = obj_dict.get("runtime", True)
310316
attribute.deprecated = obj_dict.get("deprecated")
311317
attribute.public = obj_dict.get("public")
@@ -340,7 +346,8 @@ def _load_type_alias(obj_dict: dict[str, Any]) -> TypeAlias:
340346
docstring=_load_docstring(obj_dict),
341347
analysis=obj_dict.get("analysis"),
342348
)
343-
type_alias.labels |= set(obj_dict.get("labels", ()))
349+
if labels := obj_dict.get("labels"):
350+
type_alias.labels.update(labels)
344351
type_alias.runtime = obj_dict.get("runtime", True)
345352
type_alias.deprecated = obj_dict.get("deprecated")
346353
type_alias.public = obj_dict.get("public")

0 commit comments

Comments
 (0)