mirror of
https://github.com/davidhalter/django-stubs.git
synced 2025-12-19 02:15:59 +08:00
Fixes CI
This commit is contained in:
@@ -1,3 +1,3 @@
|
||||
from .monkeypatch import monkeypatch
|
||||
from .patch import monkeypatch as monkeypatch
|
||||
|
||||
__all__ = ["monkeypatch"]
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
from typing import Any, Generic, List, Optional, Tuple, Type, TypeVar
|
||||
|
||||
import django
|
||||
from django import VERSION as VERSION
|
||||
from django.contrib.admin import ModelAdmin
|
||||
from django.contrib.admin.options import BaseModelAdmin
|
||||
from django.db.models.manager import BaseManager
|
||||
@@ -21,9 +21,6 @@ class MPGeneric(Generic[_T]):
|
||||
possible issues we may run into with this method.
|
||||
"""
|
||||
|
||||
version: Optional[int]
|
||||
cls: Type[_T]
|
||||
|
||||
def __init__(self, cls: Type[_T], version: Optional[_VersionSpec] = None):
|
||||
"""Set the data fields, basic constructor."""
|
||||
self.version = version
|
||||
@@ -31,7 +28,7 @@ class MPGeneric(Generic[_T]):
|
||||
|
||||
def __repr__(self) -> str:
|
||||
"""Better representation in tests and debug."""
|
||||
return "<MPGeneric: {0}, versions={1}>".format(self.cls, self.version or "all")
|
||||
return "<MPGeneric: {}, versions={}>".format(self.cls, self.version or "all")
|
||||
|
||||
|
||||
# certain django classes need to be generic, but lack the __class_getitem__ dunder needed to
|
||||
@@ -51,7 +48,7 @@ _need_generic: List[MPGeneric[Any]] = [
|
||||
def monkeypatch() -> None:
|
||||
"""Monkey patch django as necessary to work properly with mypy."""
|
||||
suited_for_this_version = filter(
|
||||
spec.version is None or django.VERSION[:2] <= spec.version,
|
||||
lambda spec: spec.version is None or VERSION[:2] <= spec.version,
|
||||
_need_generic,
|
||||
)
|
||||
for el in suited_for_this_version:
|
||||
@@ -1,25 +1,43 @@
|
||||
from contextlib import suppress
|
||||
from typing import Optional
|
||||
|
||||
import pytest
|
||||
from _pytest.fixtures import FixtureRequest
|
||||
from _pytest.monkeypatch import MonkeyPatch
|
||||
from typing_extensions import Protocol
|
||||
|
||||
import django_stubs_ext
|
||||
from django_stubs_ext.monkeypatch import _need_generic, _VersionSpec, django
|
||||
from django_stubs_ext import patch
|
||||
from django_stubs_ext.patch import _need_generic, _VersionSpec
|
||||
|
||||
|
||||
class _MakeGenericClasses(Protocol):
|
||||
"""Used to represent a type of ``make_generic_classes`` fixture."""
|
||||
|
||||
def __call__(self, django_version: Optional[_VersionSpec] = None) -> None:
|
||||
...
|
||||
|
||||
|
||||
@pytest.fixture(scope="function")
|
||||
def make_generic_classes(request, monkeypatch):
|
||||
def fin():
|
||||
def make_generic_classes(
|
||||
request: FixtureRequest,
|
||||
monkeypatch: MonkeyPatch,
|
||||
) -> _MakeGenericClasses:
|
||||
def fin() -> None:
|
||||
for el in _need_generic:
|
||||
delattr(el.cls, "__class_getitem__")
|
||||
with suppress(AttributeError):
|
||||
delattr(el.cls, "__class_getitem__")
|
||||
|
||||
def factory(django_version=None):
|
||||
def factory(django_version: Optional[_VersionSpec] = None) -> None:
|
||||
if django_version is not None:
|
||||
monkeypatch.setattr(django, "VERSION", django_version)
|
||||
monkeypatch.setattr(patch, "VERSION", django_version)
|
||||
django_stubs_ext.monkeypatch()
|
||||
|
||||
request.addfinalizer(fin)
|
||||
return factory
|
||||
|
||||
|
||||
def test_patched_generics(make_generic_classes) -> None:
|
||||
def test_patched_generics(make_generic_classes: _MakeGenericClasses) -> None:
|
||||
"""Test that the generics actually get patched."""
|
||||
make_generic_classes()
|
||||
|
||||
@@ -39,11 +57,11 @@ def test_patched_generics(make_generic_classes) -> None:
|
||||
)
|
||||
def test_patched_version_specific(
|
||||
django_version: _VersionSpec,
|
||||
make_generic_classes,
|
||||
make_generic_classes: _MakeGenericClasses,
|
||||
) -> None:
|
||||
"""Test version speicific types."""
|
||||
make_generic_classes(django_version)
|
||||
|
||||
for el in _need_generic:
|
||||
if el.version is not None and el.version[:2] <= django_version:
|
||||
if el.version is not None and django_version <= el.version:
|
||||
assert el.cls[int] is el.cls
|
||||
|
||||
Reference in New Issue
Block a user