mirror of
https://github.com/davidhalter/django-stubs.git
synced 2026-01-09 13:02:22 +08:00
Generic sitemap (#1198)
* Add test for both issue cases * Use generic type and add new django 4.1 `get_latest_lastmod` method * Add Sitemap to monkeypatch * Update test case to use generic + add failing case * Test GenericSitemap too
This commit is contained in:
80
tests/typecheck/contrib/sitemaps/test_generic_sitemap.yml
Normal file
80
tests/typecheck/contrib/sitemaps/test_generic_sitemap.yml
Normal file
@@ -0,0 +1,80 @@
|
||||
- case: test_items_custom_model
|
||||
main: |
|
||||
from django.contrib.sitemaps import GenericSitemap, Sitemap
|
||||
from django.contrib.sitemaps.views import sitemap
|
||||
from django.db.models import QuerySet
|
||||
from django.urls import path, reverse
|
||||
from myapp.models import Offer
|
||||
|
||||
class OfferSitemap(Sitemap[Offer]):
|
||||
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,
|
||||
"offer_name": item.trc, # E: "Offer" has no attribute "trc"
|
||||
},
|
||||
)
|
||||
|
||||
class WrongOfferSitemap(Sitemap[Offer]):
|
||||
def items(self) -> str:
|
||||
return "Yes"
|
||||
def location(self, item: str) -> int:
|
||||
return 1
|
||||
|
||||
info_dict = {"queryset": Offer.objects.all()}
|
||||
broken_info_dict = {"queryset": [1, 2]}
|
||||
|
||||
urlpatterns = [
|
||||
path(
|
||||
'sitemap.xml', sitemap,
|
||||
{'sitemaps': {'offers': GenericSitemap[Offer](info_dict, priority=0.6)}},
|
||||
name='django.contrib.sitemaps.views.sitemap'
|
||||
),
|
||||
path(
|
||||
'broken_sitemap.xml', sitemap,
|
||||
{'sitemaps': {'offers': GenericSitemap[Offer](broken_info_dict, priority=0.6)}},
|
||||
name='django.contrib.sitemaps.views.broken_sitemap'
|
||||
),
|
||||
]
|
||||
out: |
|
||||
main:24: error: Return type "str" of "items" incompatible with return type "Iterable[Offer]" in supertype "Sitemap"
|
||||
main:26: error: Argument 1 of "location" is incompatible with supertype "Sitemap"; supertype defines the argument type as "Offer"
|
||||
main:26: note: This violates the Liskov substitution principle
|
||||
main:26: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#incompatible-overrides
|
||||
main:26: error: Return type "int" of "location" incompatible with return type "str" in supertype "Sitemap"
|
||||
main:40: error: Argument 1 to "GenericSitemap" has incompatible type "Dict[str, List[int]]"; expected "Mapping[str, Union[datetime, _QuerySet[Offer, Offer], str]]"
|
||||
|
||||
installed_apps:
|
||||
- myapp
|
||||
files:
|
||||
- path: myapp/__init__.py
|
||||
- path: myapp/models.py
|
||||
content: |
|
||||
from django.db import models
|
||||
|
||||
class Offer(models.Model):
|
||||
provider = models.ForeignKey("self", on_delete=models.CASCADE)
|
||||
url_name = models.CharField()
|
||||
|
||||
- case: test_items_string_sequence
|
||||
main: |
|
||||
from django.contrib.sitemaps import Sitemap
|
||||
from typing import List
|
||||
from django.urls import reverse
|
||||
|
||||
class StaticViewSitemap(Sitemap[str]):
|
||||
priority = 1
|
||||
changefreq = "always"
|
||||
|
||||
def items(self) -> List[str]:
|
||||
return ["home", "about", "contact", "recommendations", "privacy-policy", "blog"]
|
||||
|
||||
def location(self, item: str) -> str:
|
||||
return reverse(item)
|
||||
Reference in New Issue
Block a user