Skip to content

RelatedField.get_queryset is wrongly typed #779

Open
@PedroPerpetua

Description

@PedroPerpetua

Bug report

What's wrong

The RelatedField.get_queryset method is missing a None typing.

The implementation:

def get_queryset(self):
    queryset = self.queryset
    if isinstance(queryset, (QuerySet, Manager)):
        # Ensure queryset is re-evaluated whenever used.
        # Note that actually a `Manager` class may also be used as the
        # queryset argument. This occurs on ModelSerializer fields,
        # as it allows us to generate a more expressive 'repr' output
        # for the field.
        # Eg: 'MyRelationship(queryset=ExampleModel.objects.all())'
        queryset = queryset.all()
    return queryset

The type of self.queryset is queryset: QuerySet[_MT] | Manager[_MT] | None. This means that if self.queryset is None, self.get_queryset will return None.

How is that should be

# relations.pyi
class RelatedField(...):
    # ...
    def get_queryset(self) -> QuerySet[_MT] | None: ...

System information

  • OS:
  • python version: 3.13.5
  • django version: 5.2.3
  • mypy version: 1.16.0
  • django-stubs version: 3.16.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions