Skip to content

Commit cc746ab

Browse files
committed
WIP: django_stubs_ext: monkeypatch reveal_{type,locals} into builtins
Fixes #590
1 parent f003968 commit cc746ab

File tree

1 file changed

+20
-2
lines changed
  • django_stubs_ext/django_stubs_ext

1 file changed

+20
-2
lines changed

django_stubs_ext/django_stubs_ext/patch.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,13 @@
1-
from typing import Any, Generic, List, Optional, Tuple, Type, TypeVar
1+
from typing import (
2+
TYPE_CHECKING,
3+
Any,
4+
Generic,
5+
List,
6+
Optional,
7+
Tuple,
8+
Type,
9+
TypeVar,
10+
)
211

312
from django import VERSION as VERSION
413
from django.contrib.admin import ModelAdmin
@@ -46,15 +55,24 @@ def __repr__(self) -> str:
4655
]
4756

4857

49-
# currently just adds the __class_getitem__ dunder. if more monkeypatching is needed, add it here
5058
def monkeypatch() -> None:
5159
"""Monkey patch django as necessary to work properly with mypy."""
60+
61+
# Add the __class_getitem__ dunder.
5262
suited_for_this_version = filter(
5363
lambda spec: spec.version is None or VERSION[:2] <= spec.version,
5464
_need_generic,
5565
)
5666
for el in suited_for_this_version:
5767
el.cls.__class_getitem__ = classmethod(lambda cls, *args, **kwargs: cls)
5868

69+
# Define reveal_type/reveal_locals, to not cause NameError during setting
70+
# up Django.
71+
if not TYPE_CHECKING:
72+
import builtins
73+
74+
builtins.reveal_type = lambda _: None
75+
builtins.reveal_locals = lambda: None
76+
5977

6078
__all__ = ["monkeypatch"]

0 commit comments

Comments
 (0)