Skip to content

groupby filter returns _GroupTuple instead of tuple #654

@major

Description

@major

Expected Behavior

In jinja < 2.9, the groupby filter returns its data as a tuple.

Actual Behavior

In jinja >= 2.9, the groupby filter returns its data as a namedtuple.

Template Code

Here's what I'm using along with Ansible right now:

---

- hosts: all
  vars:
    fruits:
      - name: apple
        enjoy: yes
      - name: orange
        enjoy: no
      - name: strawberry
        enjoy: yes
  tasks:

    - name: Loop through my fruits
      debug:
        msg: |
          {% set fruit_list = [] %}
          {% for fruit_dict in item[1] %}
          {{ fruit_dict.name }}
          {% endfor %}
      with_items:
        - "{{ fruits | groupby('enjoy') }}"

In jinja 2.8, the result of the groupby() is a tuple and Ansible is able to parse it with python's AST module without an issue. In jinja 2.9 and later, the groupby() returns a namedtuple and this cannot be parsed with AST. This causes an exception in Ansible.

This may not be considered a bug within jinja, but this change is fairly disruptive for downstream users.

Ansible bug: ansible/ansible#20098

Your Environment

  • Python version: 2.7.12
  • Jinja version: 2.9.4

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions