Skip to content

Sitemap type anotation is too restrictive #1192

@UnknownPlatypus

Description

@UnknownPlatypus

Bug report

What's wrong

I'm having a similar issue to #1000 when type annotating django sitemaps.

The following is not passing:

class OfferSitemap(Sitemap):
    priority = 1
    changefreq = "always"

    def items(self) -> QuerySet[Offer]:
        return Offer.objects.all()

    def location(self, item: Offer) -> str:
        return reverse(
            "myapp:detail-offer",
            kwargs={
                "provider_name": item.provider.url_name,
                "offer_name": item.url_name,
            },
        )

# error: Argument 1 of "location" is incompatible with supertype "Sitemap"; supertype defines the argument type as "Model"
# note: This violates the Liskov substitution principle

How is that should be

From the docs:

items() is a method that returns a sequence or QuerySet of objects. The objects returned will get passed to any callable methods corresponding to a sitemap property (location, lastmod, changefreq, and priority).

I think the type could be more generic because the items method is the one that determines the type of the item variable every other methods are using. Maybe taking a similar approach to #800 with an _Item Typevar ?

Let me know if you think this is a correct approach and I'll be happy to submit a PR to address that.

Cheers

System information

  • OS: ubuntu20.04
  • python version: 3.8
  • django version: 3.2.15
  • mypy version: 0.950
  • django-stubs version: 1.11.0
  • django-stubs-ext version: 0.5.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