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

Avoid mutable global state in SettingsWrapper #1097

Merged
merged 1 commit into from
Nov 10, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions pytest_django/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
TYPE_CHECKING,
Any,
Callable,
ClassVar,
ContextManager,
Generator,
Iterable,
Expand Down Expand Up @@ -498,7 +497,9 @@ def async_rf() -> django.test.AsyncRequestFactory:


class SettingsWrapper:
_to_restore: ClassVar[list[Any]] = []
def __init__(self) -> None:
self._to_restore: list[django.test.override_settings]
Copy link
Member

Choose a reason for hiding this comment

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

The type declaration can remain at the class level, like in a stub. IMO a little more readable.

Copy link
Member Author

Choose a reason for hiding this comment

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

My idea was to make it look like self._to_restore: list[django.test.override_settings] = []

Copy link
Member

Choose a reason for hiding this comment

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

Yeah fair, I guess type checkers support it.

object.__setattr__(self, "_to_restore", [])

def __delattr__(self, attr: str) -> None:
from django.test import override_settings
Expand Down