Script variables inherited from superclass are not included in form rendering #4400

Closed
opened 2025-12-29 18:35:37 +01:00 by adam · 7 comments
Owner

Originally created by @glennmatthews on GitHub (Dec 23, 2020).

Environment

  • Python version: 3.7.7
  • NetBox version: 2.9.11

Steps to Reproduce

Create a Script class which defines one or more variables; subclass this script and add extra variables to the subclass. When accessing the input form for the subclass script, only the added variables are present in the form - the variables inherited from the parent class are missing.

from extras.scripts import Script, StringVar

class SimpleScript(Script):
    class Meta:
        field_order = ["simple_variable"]

    simple_variable = StringVar()

class ExtendedScript(SimpleScript):
    class Meta:
        field_order = ["simple_variable", "extended_variable"]

    extended_variable = StringVar()

image

Expected Behavior

Both inherited and locally defined variables should be present in the input form. I believe that inherited variables were included in forms prior to commit eaaaaec5a5, as we were using field_order and getattr to look up the variables in _get_vars, instead of __dict__ (which doesn't include inherited attributes).

Observed Behavior

Only variables locally defined on the subclass are included in the input form, regardless of their inclusion in field_order.

Originally created by @glennmatthews on GitHub (Dec 23, 2020). <!-- NOTE: IF YOUR ISSUE DOES NOT FOLLOW THIS TEMPLATE, IT WILL BE CLOSED. This form is only for reporting reproducible bugs. If you need assistance with NetBox installation, or if you have a general question, please start a discussion instead: https://github.com/netbox-community/netbox/discussions Please describe the environment in which you are running NetBox. Be sure that you are running an unmodified instance of the latest stable release before submitting a bug report, and that any plugins have been disabled. --> ### Environment * Python version: 3.7.7 * NetBox version: 2.9.11 <!-- Describe in detail the exact steps that someone else can take to reproduce this bug using the current stable release of NetBox. Begin with the creation of any necessary database objects and call out every operation being performed explicitly. If reporting a bug in the REST API, be sure to reconstruct the raw HTTP request(s) being made: Don't rely on a client library such as pynetbox. --> ### Steps to Reproduce Create a Script class which defines one or more variables; subclass this script and add extra variables to the subclass. When accessing the input form for the subclass script, only the added variables are present in the form - the variables inherited from the parent class are missing. ```python from extras.scripts import Script, StringVar class SimpleScript(Script): class Meta: field_order = ["simple_variable"] simple_variable = StringVar() class ExtendedScript(SimpleScript): class Meta: field_order = ["simple_variable", "extended_variable"] extended_variable = StringVar() ``` ![image](https://user-images.githubusercontent.com/5603551/103017677-f7422580-4511-11eb-85db-7ea3a6fc4845.png) <!-- What did you expect to happen? --> ### Expected Behavior Both inherited and locally defined variables should be present in the input form. I believe that inherited variables were included in forms prior to commit eaaaaec5a524c76ff2e76192b2cbea5e7445705d, as we were using `field_order` and `getattr` to look up the variables in `_get_vars`, instead of `__dict__` (which doesn't include inherited attributes). <!-- What happened instead? --> ### Observed Behavior Only variables locally defined on the subclass are included in the input form, regardless of their inclusion in `field_order`.
adam added the type: featurepending closurestatus: under review labels 2025-12-29 18:35:37 +01:00
adam closed this issue 2025-12-29 18:35:37 +01:00
Author
Owner

@glennmatthews commented on GitHub (Dec 23, 2020):

Workaround is to update the script to manually include the parent class's variables, i.e.:

class ExtendedScript(SimpleScript):
    class Meta:
        field_order = ["simple_variable", "extended_variable"]

    simple_variable = SimpleScript.simple_variable
    extended_variable = StringVar()

But it would be better if field_order were respected.

@glennmatthews commented on GitHub (Dec 23, 2020): Workaround is to update the script to manually include the parent class's variables, i.e.: ```python class ExtendedScript(SimpleScript): class Meta: field_order = ["simple_variable", "extended_variable"] simple_variable = SimpleScript.simple_variable extended_variable = StringVar() ``` But it would be better if `field_order` were respected.
Author
Owner

@drmsoffall commented on GitHub (Dec 24, 2020):

@glennmatthews,
This is a behavior of Python, not of NetBox. Inner classes of a subclass do not automatically inherit from inner classes of a parent class. See https://www.codespeedy.com/understanding-inheritance-in-python-inner-class/ for an example.

You could possibly come up with a more clear way to take advantage of this, like inheriting from the parent class' inner class and then using += instead of =, or some variation thereof.

@drmsoffall commented on GitHub (Dec 24, 2020): @glennmatthews, This is a behavior of Python, not of NetBox. Inner classes of a subclass do not automatically inherit from inner classes of a parent class. See https://www.codespeedy.com/understanding-inheritance-in-python-inner-class/ for an example. You could possibly come up with a more clear way to take advantage of this, like inheriting from the parent class' inner class and then using += instead of =, or some variation thereof.
Author
Owner

@glennmatthews commented on GitHub (Dec 30, 2020):

Thanks for the comment, but I believe that the behavior of inner classes is not relevant to this issue. simple_variable and extended_variable are not inner classes, and I'm explicitly setting the field_order Meta attribute in ExtendedScript, not inheriting anything from the SimpleScript Meta inner class.

@glennmatthews commented on GitHub (Dec 30, 2020): Thanks for the comment, but I believe that the behavior of inner classes is not relevant to this issue. `simple_variable` and `extended_variable` are not inner classes, and I'm explicitly setting the `field_order` `Meta` attribute in `ExtendedScript`, not inheriting anything from the `SimpleScript` `Meta` inner class.
Author
Owner

@lampwins commented on GitHub (Jan 19, 2021):

I agree with @glennmatthews, and there is a pretty standard pattern for this in the Django world. Django Rest Framework and Django Filters both use a Metaclass that defines a method to traverse all base classes and collect defined fields for Serialiers and Filtersets, respectively. We could do the same thing for Script variables. See https://github.com/encode/django-rest-framework/blob/master/rest_framework/serializers.py#L281

@lampwins commented on GitHub (Jan 19, 2021): I agree with @glennmatthews, and there is a pretty standard pattern for this in the Django world. Django Rest Framework and Django Filters both use a Metaclass that defines a method to traverse all base classes and collect defined fields for Serialiers and Filtersets, respectively. We could do the same thing for Script variables. See https://github.com/encode/django-rest-framework/blob/master/rest_framework/serializers.py#L281
Author
Owner

@jeremystretch commented on GitHub (Jan 19, 2021):

Reclassifying this as a proposed feature since the described behavior has never been documented as supported.

This needs a well-formed proposed implementation before any work can proceed.

@jeremystretch commented on GitHub (Jan 19, 2021): Reclassifying this as a proposed feature since the described behavior has never been documented as supported. This needs a well-formed proposed implementation before any work can proceed.
Author
Owner

@stale[bot] commented on GitHub (Mar 5, 2021):

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. NetBox is governed by a small group of core maintainers which means not all opened issues may receive direct feedback. Please see our contributing guide.

@stale[bot] commented on GitHub (Mar 5, 2021): This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. NetBox is governed by a small group of core maintainers which means not all opened issues may receive direct feedback. Please see our [contributing guide](https://github.com/netbox-community/netbox/blob/develop/CONTRIBUTING.md).
Author
Owner

@stale[bot] commented on GitHub (Mar 21, 2021):

This issue has been automatically closed due to lack of activity. In an effort to reduce noise, please do not comment any further. Note that the core maintainers may elect to reopen this issue at a later date if deemed necessary.

@stale[bot] commented on GitHub (Mar 21, 2021): This issue has been automatically closed due to lack of activity. In an effort to reduce noise, please do not comment any further. Note that the core maintainers may elect to reopen this issue at a later date if deemed necessary.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#4400