Repository navigation
Count Django's models, forms, commands, template tags and receivers as used, and add rules for Django REST framework - #283
Merged
Conversation
…d receivers as used, and add rules for Django REST framework Assisted-by: Claude:claude-opus-5-5
…amework # Conflicts: # tests/_cli/test_config.py
Assisted-by: Claude:claude-opus-5-5
Contributor
PerformanceTime to check CPython v3.15.0: the median of 15 runs of each binary, run in turns.
EcosystemFindings on 19 projects: 3 changed. django: +1 -1403- django/contrib/admin/helpers.py:32:5: unused-attribute 'select_across'
- django/contrib/admin/templatetags/admin_filters.py:14:5: unused-function 'to_object_display_value'
- django/contrib/admin/templatetags/admin_list.py:38:5: unused-function 'paginator_number'
- django/contrib/admin/templatetags/admin_list.py:77:5: unused-function 'pagination_tag'
- django/contrib/admin/templatetags/admin_list.py:368:5: unused-function 'result_list_tag'
- django/contrib/admin/templatetags/admin_list.py:489:5: unused-function 'date_hierarchy_tag'
- django/contrib/admin/templatetags/admin_list.py:514:5: unused-function 'search_form_tag'
- django/contrib/admin/templatetags/admin_list.py:526:5: unused-function 'admin_list_filter'
- django/contrib/admin/templatetags/admin_list.py:547:5: unused-function 'admin_actions_tag'
- django/contrib/admin/templatetags/admin_list.py:554:5: unused-function 'change_list_object_tools_tag'
- django/contrib/admin/templatetags/admin_modify.py:52:5: unused-function 'prepopulated_fields_js_tag'
- django/contrib/admin/templatetags/admin_modify.py:117:5: unused-function 'submit_row_tag'
- django/contrib/admin/templatetags/admin_modify.py:124:5: unused-function 'change_form_object_tools_tag'
- django/contrib/admin/templatetags/admin_modify.py:136:5: unused-function 'cell_count'
- django/contrib/admin/templatetags/admin_modify.py:156:5: unused-function 'admin_actions_tag'
- django/contrib/admin/templatetags/admin_urls.py:12:5: unused-function 'admin_urlname'
- django/contrib/admin/templatetags/admin_urls.py:17:5: unused-function 'admin_urlquote'
- django/contrib/admin/templatetags/log.py:27:5: unused-function 'get_admin_log'
- django/contrib/auth/forms.py:526:5: unused-attribute 'old_password'
- django/contrib/auth/hashers.py:141:5: unused-function 'reset_hashers'
- django/contrib/contenttypes/management/commands/remove_stale_contenttypes.py:10:7: unused-class 'Command'
- django/contrib/flatpages/forms.py:43:9: unused-method 'clean_url'
- django/contrib/flatpages/models.py:12:5: unused-attribute 'enable_comments'
- django/contrib/flatpages/templatetags/flatpages.py:47:5: unused-function 'get_flatpages'
- django/contrib/gis/db/backends/oracle/models.py:51:5: unused-attribute 'cs_name'
- django/contrib/gis/db/backends/oracle/models.py:58:5: unused-attribute 'cs_bounds'
- django/contrib/gis/db/backends/postgis/models.py:15:5: unused-attribute 'f_table_catalog'
- django/contrib/gis/db/backends/postgis/models.py:16:5: unused-attribute 'f_table_schema'
- django/contrib/gis/db/backends/spatialite/models.py:18:5: unused-attribute 'spatial_index_enabled'
- django/contrib/gis/db/backends/spatialite/models.py:60:5: unused-attribute 'ref_sys_name'
- django/contrib/gis/management/commands/inspectdb.py:4:7: unused-class 'Command'
- django/contrib/gis/management/commands/ogrinspect.py:35:7: unused-class 'Command'
- django/contrib/humanize/templatetags/humanize.py:24:5: unused-function 'ordinal'
- django/contrib/humanize/templatetags/humanize.py:73:5: unused-function 'intcomma'
- django/contrib/humanize/templatetags/humanize.py:131:5: unused-function 'intword'
- django/contrib/humanize/templatetags/humanize.py:158:5: unused-function 'apnumber'
- django/contrib/sessions/management/commands/clearsessions.py:7:7: unused-class 'Command'
- django/contrib/staticfiles/management/commands/findstatic.py:7:7: unused-class 'Command'
- django/core/management/commands/check.py:8:7: unused-class 'Command'
- django/core/management/commands/compilemessages.py:29:7: unused-class 'Command'
- django/core/management/commands/createcachetable.py:19:7: unused-class 'Command'
- django/core/management/commands/dbshell.py:7:7: unused-class 'Command'
- django/core/management/commands/diffsettings.py:9:7: unused-class 'Command'
- django/core/management/commands/dumpdata.py:30:7: unused-class 'Command'
- django/core/management/commands/flush.py:10:7: unused-class 'Command'
- django/core/management/commands/optimizemigration.py:15:7: unused-class 'Command'
- django/core/management/commands/sendtestemail.py:8:7: unused-class 'Command'
- django/core/management/commands/showmigrations.py:10:7: unused-class 'Command'
- django/core/management/commands/sqlflush.py:6:7: unused-class 'Command'
- django/core/management/commands/sqlmigrate.py:7:7: unused-class 'Command'
... and 1354 moresaleor: +0 -204- saleor/account/i18n.py:65:9: unused-attribute 'labels'
- saleor/account/management/commands/createsuperuser.py:28:7: unused-class 'Command'
- saleor/account/models.py:201:5: unused-attribute 'search_document'
- saleor/account/models.py:383:5: unused-attribute 'is_public'
- saleor/account/models.py:384:5: unused-attribute 'customer'
- saleor/account/models.py:475:9: unused-attribute 'verbose_name_plural'
- saleor/app/management/commands/app_delete_all.py:11:7: unused-class 'Command'
- saleor/app/management/commands/app_list.py:12:7: unused-class 'Command'
- saleor/app/management/commands/app_set_active.py:13:7: unused-class 'Command'
- saleor/app/management/commands/create_app.py:18:7: unused-class 'Command'
- saleor/app/management/commands/install_app.py:16:7: unused-class 'Command'
- saleor/app/management/commands/webhook_set_active.py:11:7: unused-class 'Command'
- saleor/attribute/management/commands/update_attribute_file_urls.py:15:7: unused-class 'Command'
- saleor/attribute/models/base.py:32:9: unused-attribute 'abstract'
- saleor/attribute/models/base.py:179:5: unused-attribute 'page_types'
- saleor/attribute/models/base.py:186:5: unused-attribute 'customer_types'
- saleor/attribute/models/base.py:202:5: unused-attribute 'is_variant_only'
- saleor/attribute/models/base.py:235:9: unused-attribute 'unique_together'
- saleor/attribute/models/base.py:447:9: unused-attribute 'unique_together'
- saleor/attribute/models/base.py:536:9: unused-attribute 'unique_together'
- saleor/attribute/models/customer.py:25:9: unused-attribute 'unique_together'
- saleor/attribute/models/customer.py:44:9: unused-attribute 'unique_together'
- saleor/attribute/models/page.py:25:9: unused-attribute 'unique_together'
- saleor/attribute/models/page.py:44:9: unused-attribute 'unique_together'
- saleor/attribute/models/product.py:25:9: unused-attribute 'unique_together'
- saleor/attribute/models/product.py:46:9: unused-attribute 'unique_together'
- saleor/attribute/models/product_variant.py:28:9: unused-attribute 'unique_together'
- saleor/attribute/models/product_variant.py:52:9: unused-attribute 'unique_together'
- saleor/attribute/models/product_variant.py:62:5: unused-attribute 'assigned_variants'
- saleor/attribute/models/product_variant.py:74:9: unused-attribute 'unique_together'
- saleor/checkout/models.py:123:5: unused-attribute 'last_automatic_completion_attempt'
- saleor/core/management/commands/check_mptt_trees.py:110:7: unused-class 'Command'
- saleor/core/management/commands/clean_editorjs_fields.py:58:7: unused-class 'Command'
- saleor/core/management/commands/cleardb.py:26:7: unused-class 'Command'
- saleor/core/management/commands/clearorders.py:44:7: unused-class 'Command'
- saleor/core/management/commands/delete_event_payloads.py:6:7: unused-class 'Command'
- saleor/core/management/commands/generate_json_schemas.py:14:7: unused-class 'Command'
- saleor/core/management/commands/populatedb.py:34:7: unused-class 'Command'
- saleor/core/management/commands/remove_invalid_files.py:17:7: unused-class 'Command'
- saleor/core/management/commands/update_search_indexes.py:10:7: unused-class 'Command'
- saleor/core/models.py:20:9: unused-attribute 'abstract'
- saleor/core/models.py:76:9: unused-attribute 'abstract'
- saleor/core/models.py:99:9: unused-attribute 'abstract'
- saleor/core/models.py:144:9: unused-attribute 'abstract'
- saleor/core/models.py:156:9: unused-attribute 'abstract'
- saleor/core/utils/translations.py:34:9: unused-attribute 'abstract'
- saleor/csv/models.py:28:5: unused-attribute 'export_file'
- saleor/discount/models.py:308:9: unused-attribute 'unique_together'
- saleor/discount/models.py:326:9: unused-attribute 'unique_together'
- saleor/discount/models.py:337:9: unused-attribute 'unique_together'
... and 154 morezulip: +1 -282- analytics/management/commands/check_analytics_state.py:29:7: unused-class 'Command'
- analytics/management/commands/clear_analytics_tables.py:11:7: unused-class 'Command'
- analytics/management/commands/clear_single_stat.py:11:7: unused-class 'Command'
- analytics/management/commands/populate_analytics_db.py:34:7: unused-class 'Command'
- analytics/management/commands/update_analytics_counts.py:19:7: unused-class 'Command'
- analytics/models.py:43:11: unused-class 'Meta'
- analytics/models.py:48:11: unused-class 'Meta'
- analytics/models.py:71:11: unused-class 'Meta'
- analytics/models.py:101:11: unused-class 'Meta'
- analytics/models.py:133:11: unused-class 'Meta'
- confirmation/models.py:245:11: unused-class 'Meta'
- corporate/lib/stripe.py:710:5: unused-attribute 'website'
- corporate/lib/stripe.py:713:5: unused-attribute 'organization_type'
- corporate/management/commands/initialize_fixed_price_plan.py:15:7: unused-class 'Command'
- corporate/management/commands/link_customer_to_stripe_id.py:13:7: unused-class 'Command'
- corporate/models/customers.py:47:11: unused-class 'Meta'
- corporate/models/plans.py:18:11: unused-class 'Meta'
- corporate/models/plans.py:48:11: unused-class 'Meta'
- corporate/models/plans.py:198:11: unused-class 'Meta'
- corporate/models/sponsorships.py:20:5: unused-attribute 'requested_by'
- corporate/models/stripe_state.py:12:5: unused-attribute 'stripe_event_id'
- corporate/models/stripe_state.py:99:5: unused-attribute 'stripe_payment_intent_id'
- corporate/models/stripe_state.py:110:5: unused-attribute 'last_payment_error'
- corporate/views/support.py:85:5: unused-attribute 'request_subject'
- corporate/views/support.py:86:5: unused-attribute 'request_message'
- corporate/views/support.py:102:5: unused-attribute 'organization_name'
- corporate/views/support.py:103:5: unused-attribute 'organization_type'
- corporate/views/support.py:104:5: unused-attribute 'organization_website'
- corporate/views/support.py:105:5: unused-attribute 'expected_user_count'
- corporate/views/support.py:106:5: unused-attribute 'type_of_hosting'
- corporate/views/support.py:112:5: unused-attribute 'organization_website'
- corporate/views/support.py:113:5: unused-attribute 'expected_user_count'
- zerver/forms.py:128:5: unused-attribute 'realm_subdomain'
- zerver/forms.py:129:5: unused-attribute 'realm_type'
- zerver/forms.py:132:5: unused-attribute 'realm_default_language'
- zerver/forms.py:133:5: unused-attribute 'realm_name'
- zerver/forms.py:149:9: unused-method 'clean_realm_subdomain'
- zerver/forms.py:175:5: unused-attribute 'how_realm_creator_found_zulip'
- zerver/forms.py:178:5: unused-attribute 'how_realm_creator_found_zulip_other_text'
- zerver/forms.py:179:5: unused-attribute 'how_realm_creator_found_zulip_where_ad'
- zerver/forms.py:180:5: unused-attribute 'how_realm_creator_found_zulip_which_organization'
- zerver/forms.py:183:5: unused-attribute 'how_realm_creator_found_zulip_review_site'
- zerver/forms.py:184:5: unused-attribute 'how_realm_creator_found_zulip_which_ai_chatbot'
- zerver/forms.py:211:9: unused-method 'clean_full_name'
- zerver/forms.py:219:9: unused-method 'clean_password'
- zerver/forms.py:230:5: unused-attribute 'terms'
- zerver/forms.py:231:5: unused-attribute 'realm_type'
- zerver/forms.py:234:5: unused-attribute 'realm_default_language'
- zerver/forms.py:248:5: unused-attribute 'terms'
- zerver/forms.py:274:9: unused-method 'clean_email'
... and 233 more |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Extends the
djangorules to models, forms, class-based views, management commands, template libraries, signal receivers anddjango-filter's filter sets, and adds arest-frameworkframework for Django REST framework. The framework option isrest-framework, after therest_frameworkpackage, asflask-restfulis named. Its enum variant isDrf, since clippy rejects a variant that ends with the enum's name (Framework::RestFramework).What was already covered
I first ran
mainon a sample project with Django 6.1, DRF 3.18 and django-filter 26.2 installed in the--pythonenvironment.external_membersalready counts as used what a base class outside the project defines and a subclass overrides:save,clean,Serializer.validate,ModelAdminoptions such aslist_display, thequeryset/serializer_class/permission_classesof a generic view, and the actions of aModelViewSet, which its mixins define.It does not cover:
Meta,Media, fields,clean_<field>,validate_<field>,get_<field>;get_queryset:Manager's base is the callBaseManager.from_queryset(QuerySet), which the hierarchy cannot follow;The rules below cover the conventions. Where it is cheap, they also cover what
external_membersgives only with the environment, such as the attributes ofAPIViewand ofBaseCommand, so the rules also work without Django installed.The rules
Each was checked against the source of the installed version (file in parentheses):
djangoModel, or ofAbstractUser/AbstractBaseUserwhen Django is not installed):django.whose name ends withField,ForeignKey,ForeignObject,GenericRelationorCompositePrimaryKey, or of a subclass of one.Manager(or a subclass) or toQuerySet.as_manager().Options.default_managertakes the first manager. Other managers are used only by name, so they are still reported.Metaclass and its options.Options.contribute_to_classraisesTypeErrorfor any other attribute, so every attribute is an option.get_absolute_url, which the admin,redirect()and the sitemaps call.get_querysetof subclasses ofManager.DeclarativeFieldsMetaclass) andclean_<field>methods (BaseForm._clean_fields). TheMetaclass of aModelForm(ModelFormMetaclass).Mediaof forms, widgets and the admin'sModelAdminand inlines (MediaDefiningClass).django.views.Viewor of a view ofdjango.views.generic(View.dispatch).Commandof a module of amanagement/commandspackage whose name does not start with_(find_commands,load_command_class), when it subclassesBaseCommand.BaseCommand:handle,add_arguments,create_parser,handle_label,handle_app_config, and attributes such ashelp.filter,simple_tag,simple_block_tag,inclusion_tagortagof a module-levelLibrary(), through theInstances/registershelpers, so an unrelatedx.filterdoes not match.registervariable set to aLibrary(), whichimport_libraryreads.django.dispatch.receiver.django_filtersending withFilter) and theMetaof aFilterSet(FilterSetMetaclass,FilterSetOptions). It is a Django app, used both by Django views and by DRF, so I put it underdjangoinstead of adding a third framework.rest-frameworkrest_frameworkending withSerializer):rest_frameworkending withFieldorSerializer, which covers nested serializers (SerializerMetaclass._get_declared_fields).Metaclass.validate_<field>methods (Serializer.to_internal_value).get_<field>when the serializer has an attribute<field>set toSerializerMethodField()(SerializerMethodField.bind).APIView, and classes ofrest_framework.genericsandrest_framework.viewsets):APIViewandGenericAPIViewdefine, such asserializer_class,permission_classesandpagination_class.getattr(view, ...):search_fields,ordering_fieldsandordering(DRF), andfilterset_classandfilterset_fields(django-filter'sDjangoFilterBackend). Alsothrottle_scope(ScopedRateThrottle).list,create,retrieve,update,partial_updateanddestroy, the actions that the routers map. Methods with@action(get_extra_actions).Base classes, decorators and calls resolve through the class hierarchy to the dotted names of the framework, so aliases, re-exports and project subclasses work, and look-alike names do not match. The test cases have negative lines for each rule.
Left out
SerializerMethodField(method_name="..."),@<action>.mapping.<method>andregister.filter("name", func)written as a call.external_memberscovers them with the environment.Findings
Binaries: the release binary of
main(b785eca) against this branch. Projects: the Django projects ofscripts/ecosystem.py(django, saleor and zulip), plus netbox, a well-known DRF and django-filter project, because none of the ecosystem projects uses DRF. The environment has django, djangorestframework and django-filter.The +1s are not false positives. With
--show-transitive, every project shows +0. They are unused attributes that were hidden as transitive, and are shown now that their class is used:PartialDefNode.partial_namein django, a template tag node, set and never read;self.strictandself.modifiedin two zulip commands, both set and never read.The other 15 ecosystem projects are unchanged, byte for byte after sorting, with and without the environment, with
--show-transitiveand without. The run time is the same within noise.What was removed, by cause, with the environment:
tests/, and the forms ofcontrib), 128 template tags and filters, 49 commands, 25 receivers, 33GenericRelation/ForeignObjectfields, 10 managers, 9clean_<field>, 7Metaoptions.Metaoptions, 23 model fields, 19 commands.Metaclasses, 8clean_<field>, 3Metaoptions, 2 receivers.clean_<field>, 22get_<field>, 4validate_<field>, 4@action, 3 viewset actions;Metaoptions.I went through a random sample of about 50 removed findings across the four projects, and the ones behind each cause listed above. Each is a name that Django, DRF or django-filter reads or calls.
Remaining Django false positives, not done here
Counted on the same runs, with the environment:
MIDDLEWARE,AUTHENTICATION_BACKENDSand storage backends: about 23 in zulip, 15 in saleor and 4 in netbox. These are classes that are only named in a string in a settings file, a rough count. This needs the settings' string values resolved to project classes.class X(ViewTestCases.PrimaryObjectViewTestCase): 231 in netbox. The class hierarchy does not follow a base class that is an attribute of a class. This is an engine limitation, not a Django rule.django.contrib.admin.helpers: 59 in django. They cannot be known from the Python code.Metaofdjango-tables2tables: about 290 findings undertables/in netbox. A third-party package, a candidate for its own rules.@admin.register: 3 in django, and none in the other projects.django_rq'sCommand: 1 in netbox, which is fine once that package is installed.class Arguments(198 in saleor) and the strawberry filters of netbox (about 660 undergraphql/).Verification
cargo test,uv run pytest -c tests/pytest.ini testsanduv run prek run -apass. Tests:frameworks_djangois extended (models, forms, filters, views, commands, template tags, signals),frameworks_rest_frameworkis new, andframeworks_selectedturnsrest-frameworkoff and checks that nothing is counted as used.