mirror of
https://github.com/davidhalter/django-stubs.git
synced 2025-12-19 02:15:59 +08:00
Support passing extra classes to monkeypatch (#953)
* Support passing extra classes to monkeypatch Closes https://github.com/typeddjango/django-stubs/issues/946#issuecomment-1122895190 * Move extra classes into separate test * Avoid mutable default * Fix protocol arguments
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
from typing import Any, Generic, List, Optional, Tuple, Type, TypeVar
|
||||
from typing import Any, Generic, Iterable, List, Optional, Tuple, Type, TypeVar
|
||||
|
||||
from django import VERSION as VERSION
|
||||
from django.contrib.admin import ModelAdmin
|
||||
@@ -58,7 +58,7 @@ _need_generic: List[MPGeneric[Any]] = [
|
||||
]
|
||||
|
||||
|
||||
def monkeypatch() -> None:
|
||||
def monkeypatch(extra_classes: Optional[Iterable[type]] = None) -> None:
|
||||
"""Monkey patch django as necessary to work properly with mypy."""
|
||||
|
||||
# Add the __class_getitem__ dunder.
|
||||
@@ -68,6 +68,9 @@ def monkeypatch() -> None:
|
||||
)
|
||||
for el in suited_for_this_version:
|
||||
el.cls.__class_getitem__ = classmethod(lambda cls, *args, **kwargs: cls)
|
||||
if extra_classes:
|
||||
for cls in extra_classes:
|
||||
cls.__class_getitem__ = classmethod(lambda cls, *args, **kwargs: cls) # type: ignore[attr-defined]
|
||||
|
||||
|
||||
__all__ = ["monkeypatch"]
|
||||
|
||||
Reference in New Issue
Block a user