Skip to content

Add support for Subquery expressions #182

@nfantone

Description

@nfantone

Related to #181.

After introducing changes from #181 to properly mock MockSet.query, the following breaks because utils.get_attribute doesn't currently handle Subquery expressions.

ms = MockSet()

ms.annotate(
    my_field=Subquery(ms.filter(value=OuterRef("value")))
)
# Raises AttributeError: Mock object has no attribute 'split'

I've been playing around the codebase, trying to see if I could fit this in, but hit a wall. In contrast to all other cases (F, Case, Value and Coalesce), a Subquery cannot be immediately resolved so I'm not too sure how the logic should go.

def get_attribute(obj, attr, default=None):
    # ...
    elif isinstance(attr, Subquery):
        expr = attr.get_source_expressions()[0] # <--- This returns a Query mock
        return get_attribute(obj, expr)

    parts = attr.split('__') # <--- This is what raises the exception if Subquery is unhandled

Happy to help and contribute if you point me in the right direction.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions