Skip to content

Commit

Permalink
Added type hints.
Browse files Browse the repository at this point in the history
Protocal only available in Py3.8+

Added ref to `render_context` fix to django-stubs.
  • Loading branch information
smithdc1 committed Jul 9, 2022
1 parent 3226c5a commit 4f54e25
Show file tree
Hide file tree
Showing 17 changed files with 692 additions and 284 deletions.
1 change: 1 addition & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ repos:
- id: flake8
additional_dependencies:
- flake8-comprehensions
- flake8-typing-imports
1 change: 1 addition & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ include README.rst
recursive-include crispy_forms/static *
recursive-include crispy_forms/templates *
recursive-include crispy_forms/tests *.py *.html
include crispy_forms/py.typed
19 changes: 16 additions & 3 deletions crispy_forms/base.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
from __future__ import annotations

from types import TracebackType
from typing import Collection, Union

from django.template import Context


class KeepContext:
"""
Context manager that receives a `django.template.Context` instance and a list of keys
Expand All @@ -9,14 +17,19 @@ class KeepContext:
touch context object themselves, that could introduce side effects.
"""

def __init__(self, context, keys):
def __init__(self, context: Context, keys: Collection[str]) -> None:
self.context = context
self.keys = keys

def __enter__(self):
def __enter__(self) -> None:
pass

def __exit__(self, type, value, traceback):
def __exit__(
self,
type: Union[type[BaseException], None],
value: Union[BaseException, None],
traceback: Union[TracebackType, None],
) -> None:
for key in list(self.keys):
if key in self.context:
del self.context[key]

0 comments on commit 4f54e25

Please sign in to comment.