Allow ConditionField to reflect dynamically-added condition choices.

Review Request #14279 — Created Dec. 18, 2024 and updated

Information

Djblets
release-5.x

Reviewers

The Conditions system was designed with the intent that conditions could
be dynamically registered and unregistered as needed, offering
extensibility in the types of condition choices presented and processed.

This was never tested until now, and we've found a few flaws. One of
those is that ConditionField was calculating the valid choices at
field definition time, and held on to that when copying for a new form
instance. The choices present initially would remain present, and we'd
never recalculate.

To work around this, we now defer any processing of choices until we
need them, and we always refer back to the ConditionSet provided at
the latest initialization time (such as when initializing fields for a
form instance).

We remember the initial choices provided to the field and to the widget,
and refer back to that whenever we need to start fresh. If this is a
class, it's instantiated as late as possible. Same if it's a callback
providing an instance. This state is never deep-copied over to a new
field, allowing new instances to reflect the available choices at that
moment in time.

ConditionsWidget is now the source of truth for the condition choices.
ConditionsField has a wrapper that forwards on to that one. This
simplifies lifecycles considerably.

To take full advantage of this, callers should pass in an instance and
not a class when caling the field.

There's also one fix in here for managing widgets. We were accessing
the value field's widget attribute, which didn't exist. Initially,
this code was built to work with form fields, but we moved to a wrapper
class. We now define a forwarding attribute to get the inner field's
widget.

Unit tests pass.

Tested this with other in-progress work for more dynamic condition
choices. Verified that I could enable an extension and see all the
choices.

Summary ID
Allow ConditionField to reflect dynamically-added condition choices.
The Conditions system was designed with the intent that conditions could be dynamically registered and unregistered as needed, offering extensibility in the types of condition choices presented and processed. This was never tested until now, and we've found a few flaws. One of those is that `ConditionField` was calculating the valid choices at field definition time, and held on to that when copying for a new form instance. The choices present initially would remain present, and we'd never recalculate. To work around this, we now defer any processing of choices until we need them, and we always refer back to the `ConditionSet` provided at the latest initialization time (such as when initializing fields for a form instance). We remember the initial choices provided to the field and to the widget, and refer back to that whenever we need to start fresh. If this is a class, it's instantiated as late as possible. Same if it's a callback providing an instance. This state is never deep-copied over to a new field, allowing new instances to reflect the available choices at that moment in time. `ConditionsWidget` is now the source of truth for the condition choices. `ConditionsField` has a wrapper that forwards on to that one. This simplifies lifecycles considerably. To take full advantage of this, callers should pass in an instance and not a class when caling the field.
6544d8f2cf61d4c35514d56c5c541fc337aa5fe9
Description From Last Updated

line too long (88 > 79 characters) Column: 80 Error code: E501

reviewbotreviewbot

redefinition of unused 'Self' from line 11 Column: 1 Error code: F811

reviewbotreviewbot

More and more throughout the python ecosystem I'm seeing that setters are documented like regular methods ("Set the condition choices...") …

daviddavid

Can you add a module docstring?

daviddavid
Checks run (1 failed, 1 succeeded)
flake8 failed.
JSHint passed.

flake8

chipx86
Review request changed
Change Summary:
  • Fixed bad imports on Python 3.8.
  • Fixed a line length issue.
Commits:
Summary ID
Allow ConditionField to reflect dynamically-added condition choices.
The Conditions system was designed with the intent that conditions could be dynamically registered and unregistered as needed, offering extensibility in the types of condition choices presented and processed. This was never tested until now, and we've found a few flaws. One of those is that `ConditionField` was calculating the valid choices at field definition time, and held on to that when copying for a new form instance. The choices present initially would remain present, and we'd never recalculate. To work around this, we now defer any processing of choices until we need them, and we always refer back to the `ConditionSet` provided at the latest initialization time (such as when initializing fields for a form instance). We remember the initial choices provided to the field and to the widget, and refer back to that whenever we need to start fresh. If this is a class, it's instantiated as late as possible. Same if it's a callback providing an instance. This state is never deep-copied over to a new field, allowing new instances to reflect the available choices at that moment in time. `ConditionsWidget` is now the source of truth for the condition choices. `ConditionsField` has a wrapper that forwards on to that one. This simplifies lifecycles considerably. To take full advantage of this, callers should pass in an instance and not a class when caling the field.
92c51990f40415f0136fef7a8d459cad0291a0f7
Allow ConditionField to reflect dynamically-added condition choices.
The Conditions system was designed with the intent that conditions could be dynamically registered and unregistered as needed, offering extensibility in the types of condition choices presented and processed. This was never tested until now, and we've found a few flaws. One of those is that `ConditionField` was calculating the valid choices at field definition time, and held on to that when copying for a new form instance. The choices present initially would remain present, and we'd never recalculate. To work around this, we now defer any processing of choices until we need them, and we always refer back to the `ConditionSet` provided at the latest initialization time (such as when initializing fields for a form instance). We remember the initial choices provided to the field and to the widget, and refer back to that whenever we need to start fresh. If this is a class, it's instantiated as late as possible. Same if it's a callback providing an instance. This state is never deep-copied over to a new field, allowing new instances to reflect the available choices at that moment in time. `ConditionsWidget` is now the source of truth for the condition choices. `ConditionsField` has a wrapper that forwards on to that one. This simplifies lifecycles considerably. To take full advantage of this, callers should pass in an instance and not a class when caling the field.
6544d8f2cf61d4c35514d56c5c541fc337aa5fe9

Checks run (2 succeeded)

flake8 passed.
JSHint passed.
david
  1. 
      
  2. djblets/forms/fields.py (Diff revision 2)
     
     
    Show all issues

    More and more throughout the python ecosystem I'm seeing that setters are documented like regular methods ("Set the condition choices...") while the property itself is documented like an attribute. You actually do that for ConditionsWidget.choices later in this change.

  3. Show all issues

    Can you add a module docstring?

  4.