mirror of
https://github.com/davidhalter/django-stubs.git
synced 2025-12-19 02:15:59 +08:00
WIP: django_stubs_ext: monkeypatch reveal_{type,locals} into builtins (#591)
* WIP: django_stubs_ext: monkeypatch `reveal_{type,locals}` into builtins
Fixes https://github.com/typeddjango/django-stubs/issues/590
* fixup! WIP: django_stubs_ext: monkeypatch `reveal_{type,locals}` into builtins
* fixup! fixup! WIP: django_stubs_ext: monkeypatch `reveal_{type,locals}` into builtins
* Update patch.py
Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
import builtins
|
||||
from typing import Any, Generic, List, Optional, Tuple, Type, TypeVar
|
||||
|
||||
from django import VERSION as VERSION
|
||||
@@ -46,9 +47,10 @@ _need_generic: List[MPGeneric[Any]] = [
|
||||
]
|
||||
|
||||
|
||||
# currently just adds the __class_getitem__ dunder. if more monkeypatching is needed, add it here
|
||||
def monkeypatch() -> None:
|
||||
"""Monkey patch django as necessary to work properly with mypy."""
|
||||
|
||||
# Add the __class_getitem__ dunder.
|
||||
suited_for_this_version = filter(
|
||||
lambda spec: spec.version is None or VERSION[:2] <= spec.version,
|
||||
_need_generic,
|
||||
@@ -56,5 +58,9 @@ def monkeypatch() -> None:
|
||||
for el in suited_for_this_version:
|
||||
el.cls.__class_getitem__ = classmethod(lambda cls, *args, **kwargs: cls)
|
||||
|
||||
# Define mypy builtins, to not cause NameError during setting up Django.
|
||||
builtins.reveal_type = lambda _: None
|
||||
builtins.reveal_locals = lambda: None
|
||||
|
||||
|
||||
__all__ = ["monkeypatch"]
|
||||
|
||||
Reference in New Issue
Block a user