From cc746ab6e5fce1d2f12c7f3206bcb720efcf9adc Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Tue, 13 Apr 2021 22:53:54 +0200 Subject: [PATCH] WIP: django_stubs_ext: monkeypatch `reveal_{type,locals}` into builtins Fixes https://github.com/typeddjango/django-stubs/issues/590 --- django_stubs_ext/django_stubs_ext/patch.py | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/django_stubs_ext/django_stubs_ext/patch.py b/django_stubs_ext/django_stubs_ext/patch.py index 522737f40..2906a0df1 100644 --- a/django_stubs_ext/django_stubs_ext/patch.py +++ b/django_stubs_ext/django_stubs_ext/patch.py @@ -1,4 +1,13 @@ -from typing import Any, Generic, List, Optional, Tuple, Type, TypeVar +from typing import ( + TYPE_CHECKING, + Any, + Generic, + List, + Optional, + Tuple, + Type, + TypeVar, +) from django import VERSION as VERSION from django.contrib.admin import ModelAdmin @@ -46,9 +55,10 @@ def __repr__(self) -> str: ] -# 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 +66,13 @@ def monkeypatch() -> None: for el in suited_for_this_version: el.cls.__class_getitem__ = classmethod(lambda cls, *args, **kwargs: cls) + # Define reveal_type/reveal_locals, to not cause NameError during setting + # up Django. + if not TYPE_CHECKING: + import builtins + + builtins.reveal_type = lambda _: None + builtins.reveal_locals = lambda: None + __all__ = ["monkeypatch"]