• 
      

    Add a as_type= argument for {% definevar %} for casting to types.

    Review Request #14698 — Created Nov. 18, 2025 and submitted

    Information

    Djblets
    release-5.x

    Reviewers

    {% definevar %} can now store a value as a boolean or integer through
    the as_type= argument. By default, this stored as a string
    (equivalent to str).

    When passing as_type=bool, a true (lowercased) or 1 will
    result in a stored True. Anything else will be False.

    When passing as_type=int, the value will be converted to an integer.
    If the value can't be parsed as an integer, a TemplateSyntaxError will
    be raised.

    There's also an optimization around the options. We now convert to a
    set so we don't repeatedly iterate through the list for each option.

    Unit tests pass.

    Made use of this in some in-progress code in Review Board for setting
    options.

    Summary ID
    Add a as_type= argument for {% definevar %} for casting to types.
    `{% definevar %}` can now store a value as a boolean or integer through the `as_type=` argument. By default, this stored as a string (equivalent to `str`). When passing `as_type=bool`, a `true` (lowercased) or `1` will result in a stored `True`. Anything else will be `False`. When passing `as_type=int`, the value will be converted to an integer. If the value can't be parsed as an integer, a `TemplateSyntaxError` will be raised. There's also an optimization around the options. We now convert to a `set` so we don't repeatedly iterate through the list for each option.
    da691fa6551cc7af6020df8536c503ad461fc4ef
    Description From Last Updated

    The end sounds a bit weird, maybe say "... before use."

    maubinmaubin

    Shouldn't here and the "Version Added"s below all be "Version Changed" instead?

    maubinmaubin

    Shouldn't the result of this always be str, not Any? Given that this is essentially a parse step, it would …

    daviddavid

    Perhaps we should raise TemplateSyntaxError if 'spaceless', 'strip', or 'unsafe' are provided but we're casting to bool or int?

    daviddavid

    You're stripping twice here.

    daviddavid

    Can we add tests for "1", "TRUE", "0", "false", as well as a few including whitespace (" true", " 1 …

    daviddavid

    Can we add tests with whitespace, 0, and negative numbers?

    daviddavid

    'typing.Any' imported but unused Column: 5 Error code: F401

    reviewbotreviewbot
    maubin
    1. 
        
    2. djblets/util/templatetags/djblets_utils.py (Diff revision 1)
       
       
       
      Show all issues

      The end sounds a bit weird, maybe say "... before use."

      1. Good catch. Definitely a typo. Fixed.

    3. djblets/util/templatetags/djblets_utils.py (Diff revision 1)
       
       
       
       
       
       
       
       
       
       
       
      Show all issues

      Shouldn't here and the "Version Added"s below all be "Version Changed" instead?

    4. 
        
    david
    1. 
        
    2. Show all issues

      Shouldn't the result of this always be str, not Any?

      Given that this is essentially a parse step, it would be nice to assign the result after the parse to a separate variable which is explicitly typed as str | SafeString | int | bool instead of reassigning to result

      1. That call is str, but I was aiming to keep this flexible with types since the stored result actually could be anything (I flirted with JSON-compatible types as a result as well). But yeah, I can go for explicit here.

    3. djblets/util/templatetags/djblets_utils.py (Diff revision 1)
       
       
       
       
       
      Show all issues

      Perhaps we should raise TemplateSyntaxError if 'spaceless', 'strip', or 'unsafe' are provided but we're casting to bool or int?

    4. djblets/util/templatetags/djblets_utils.py (Diff revision 1)
       
       
       
       
       
      Show all issues

      You're stripping twice here.

    5. Show all issues

      Can we add tests for "1", "TRUE", "0", "false", as well as a few including whitespace (" true", " 1 ")?

    6. Show all issues

      Can we add tests with whitespace, 0, and negative numbers?

    7. 
        
    chipx86
    Review request changed
    Change Summary:
    • Added valid option checking for all types.
    • Improved typing.
    • Removed an extra strip() call.
    • Added more thorough unit testing.
    Commits:
    Summary ID
    Add a as_type= argument for {% definevar %} for casting to types.
    `{% definevar %}` can now store a value as a boolean or integer through the `as_type=` argument. By default, this stored as a string (equivalent to `str`). When passing `as_type=bool`, a `true` (lowercased) or `1` will result in a stored `True`. Anything else will be `False`. When passing `as_type=int`, the value will be converted to an integer. If the value can't be parsed as an integer, a `TemplateSyntaxError` will be raised. There's also an optimization around the options. We now convert to a `set` so we don't repeatedly iterate through the list for each option.
    b07678ceb1eb5292b075300623226a4663108dc6
    Add a as_type= argument for {% definevar %} for casting to types.
    `{% definevar %}` can now store a value as a boolean or integer through the `as_type=` argument. By default, this stored as a string (equivalent to `str`). When passing `as_type=bool`, a `true` (lowercased) or `1` will result in a stored `True`. Anything else will be `False`. When passing `as_type=int`, the value will be converted to an integer. If the value can't be parsed as an integer, a `TemplateSyntaxError` will be raised. There's also an optimization around the options. We now convert to a `set` so we don't repeatedly iterate through the list for each option.
    da691fa6551cc7af6020df8536c503ad461fc4ef

    Checks run (1 failed, 1 succeeded)

    flake8 failed.
    JSHint passed.

    flake8

    david
    1. Ship It!
    2. 
        
    maubin
    1. Ship It!
    2. 
        
    chipx86
    Review request changed
    Status:
    Completed
    Change Summary:
    Pushed to release-5.x (f6b636f)