mirror of
https://github.com/netbox-community/netbox.git
synced 2026-04-10 03:13:48 +02:00
chore(ruff): Enable RET rules and add explicit fallbacks
Adopt Ruff `RET` to improve return-flow consistency across the codebase. Simplify control flow by removing redundant `else` blocks after `return`, and add explicit `return None` (or equivalent) fallbacks where appropriate to preserve existing behavior. Fixes #21411
This commit is contained in:
committed by
Jeremy Stretch
parent
b22e490847
commit
ef52ac4203
@@ -39,7 +39,7 @@ class ChoiceFieldFix(OpenApiSerializerFieldExtension):
|
||||
if direction == 'request':
|
||||
return build_cf
|
||||
|
||||
elif direction == "response":
|
||||
if direction == "response":
|
||||
value = build_cf
|
||||
label = {
|
||||
**build_basic_type(OpenApiTypes.STR),
|
||||
@@ -53,6 +53,10 @@ class ChoiceFieldFix(OpenApiSerializerFieldExtension):
|
||||
}
|
||||
)
|
||||
|
||||
# TODO: This function should never implicitly/explicitly return `None`
|
||||
# The fallback should be well-defined (drf-spectacular expects request/response naming).
|
||||
return None
|
||||
|
||||
|
||||
def viewset_handles_bulk_create(view):
|
||||
"""Check if view automatically provides list-based bulk create"""
|
||||
@@ -75,8 +79,7 @@ class NetBoxAutoSchema(AutoSchema):
|
||||
def is_bulk_action(self):
|
||||
if hasattr(self.view, "action") and self.view.action in BULK_ACTIONS:
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
return False
|
||||
|
||||
def get_operation_id(self):
|
||||
"""
|
||||
@@ -316,8 +319,7 @@ class FixSerializedPKRelatedField(OpenApiSerializerFieldExtension):
|
||||
if direction == "response":
|
||||
component = auto_schema.resolve_serializer(self.target.serializer, direction)
|
||||
return component.ref if component else None
|
||||
else:
|
||||
return build_basic_type(OpenApiTypes.INT)
|
||||
return build_basic_type(OpenApiTypes.INT)
|
||||
|
||||
|
||||
class FixIntegerRangeSerializerSchema(OpenApiSerializerExtension):
|
||||
|
||||
@@ -34,14 +34,14 @@ class ObjectTypeSerializer(BaseModelSerializer):
|
||||
@extend_schema_field(OpenApiTypes.STR)
|
||||
def get_rest_api_endpoint(self, obj):
|
||||
if not (model := obj.model_class()):
|
||||
return
|
||||
return None
|
||||
try:
|
||||
return get_action_url(model, action='list', rest_api=True)
|
||||
except NoReverseMatch:
|
||||
return
|
||||
return None
|
||||
|
||||
@extend_schema_field(OpenApiTypes.STR)
|
||||
def get_description(self, obj):
|
||||
if not (model := obj.model_class()):
|
||||
return
|
||||
return None
|
||||
return inspect.getdoc(model)
|
||||
|
||||
@@ -285,5 +285,4 @@ class BackgroundTaskViewSet(BaseRQViewSet):
|
||||
stopped_jobs = stop_rq_job(id)
|
||||
if len(stopped_jobs) == 1:
|
||||
return HttpResponse(status=200)
|
||||
else:
|
||||
return HttpResponse(status=204)
|
||||
return HttpResponse(status=204)
|
||||
|
||||
@@ -144,7 +144,7 @@ class Command(BaseCommand):
|
||||
# If Python code has been passed, execute it and exit.
|
||||
if options['command']:
|
||||
exec(options['command'], namespace)
|
||||
return
|
||||
return None
|
||||
|
||||
# Try to enable tab-complete
|
||||
try:
|
||||
|
||||
@@ -98,6 +98,7 @@ class DataSource(JobsMixin, PrimaryModel):
|
||||
def get_type_display(self):
|
||||
if backend := registry['data_backends'].get(self.type):
|
||||
return backend.label
|
||||
return None
|
||||
|
||||
def get_status_color(self):
|
||||
return DataSourceStatusChoices.colors.get(self.status)
|
||||
|
||||
@@ -79,8 +79,7 @@ class ManagedFile(SyncedDataMixin, models.Model):
|
||||
'scripts': settings.SCRIPTS_ROOT,
|
||||
'reports': settings.REPORTS_ROOT,
|
||||
}[self.file_root]
|
||||
else:
|
||||
return ""
|
||||
return ""
|
||||
|
||||
def sync_data(self):
|
||||
if self.data_file:
|
||||
|
||||
@@ -146,7 +146,7 @@ class Job(models.Model):
|
||||
if self.object_type:
|
||||
if self.object_type.model == 'reportmodule':
|
||||
return reverse('extras:report_result', kwargs={'job_pk': self.pk})
|
||||
elif self.object_type.model == 'scriptmodule':
|
||||
if self.object_type.model == 'scriptmodule':
|
||||
return reverse('extras:script_result', kwargs={'job_pk': self.pk})
|
||||
return reverse('core:job', args=[self.pk])
|
||||
|
||||
|
||||
@@ -218,19 +218,22 @@ class ObjectType(ContentType):
|
||||
def app_verbose_name(self):
|
||||
if model := self.model_class():
|
||||
return model._meta.app_config.verbose_name
|
||||
return None
|
||||
|
||||
@property
|
||||
def model_verbose_name(self):
|
||||
if model := self.model_class():
|
||||
return model._meta.verbose_name
|
||||
return None
|
||||
|
||||
@property
|
||||
def model_verbose_name_plural(self):
|
||||
if model := self.model_class():
|
||||
return model._meta.verbose_name_plural
|
||||
return None
|
||||
|
||||
@property
|
||||
def is_plugin_model(self):
|
||||
if not (model := self.model_class()):
|
||||
return # Return null if model class is invalid
|
||||
return None # Return null if model class is invalid
|
||||
return isinstance(model._meta.app_config, PluginConfig)
|
||||
|
||||
Reference in New Issue
Block a user