Skip to content

Commit

Permalink
WIP: django_stubs_ext: monkeypatch reveal_{type,locals} into builtins
Browse files Browse the repository at this point in the history
  • Loading branch information
blueyed committed Apr 13, 2021
1 parent f003968 commit cc746ab
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions django_stubs_ext/django_stubs_ext/patch.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -46,15 +55,24 @@ 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,
)
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"]

0 comments on commit cc746ab

Please sign in to comment.