Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Type Hints #1282

Draft
wants to merge 23 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@ repos:
- id: flake8
additional_dependencies:
- flake8-comprehensions
- flake8-typing-imports
- flake8-type-checking
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
21 changes: 18 additions & 3 deletions crispy_forms/base.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
from __future__ import annotations

from typing import TYPE_CHECKING, Collection

if TYPE_CHECKING:
from types import TracebackType

from django.template import Context, Node


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

def __init__(self, context, keys):
def __init__(self, context: Context, keys: Collection[int | str | Node]) -> None:
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

self.context = context
self.keys = keys

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

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