Repository: litestar-org/litestar

Issue:
StaticFilesConfig and virtual directories
I'm trying to write a ``FileSystemProtocol`` to load files from the package data using [importlib_resources](https://importlib-resources.readthedocs.io/en/latest/using.html#). But because ``directories`` is defined as ``DirectoryPath``, pydantic checks if the given directories exist in the local filesystem. 

This is not generally true, especially in any kind of virtual filesystem (e.g. a zipped package). I think this condition should be relaxed to support virtual filesystems.

https://github.com/starlite-api/starlite/blob/9bb6dcd57c10a591377cf8e3a537e9292566d5b9/starlite/config/static_files.py#L32


Discussion:
I agree. Do you want to submit a PR?

Resolving patch:
diff --git a/litestar/datastructures/state.py b/litestar/datastructures/state.py
--- a/litestar/datastructures/state.py
+++ b/litestar/datastructures/state.py
@@ -2,7 +2,10 @@
 
 from copy import copy, deepcopy
 from threading import RLock
-from typing import Any, Callable, Generator, Iterable, Iterator, Mapping, MutableMapping
+from typing import TYPE_CHECKING, Any, Callable, Generator, Iterable, Iterator, Mapping, MutableMapping
+
+if TYPE_CHECKING:
+    from typing_extensions import Self
 
 __all__ = ("ImmutableState", "State")
 
@@ -13,7 +16,10 @@ class ImmutableState(Mapping[str, Any]):
     It can be accessed using dot notation while exposing dict like functionalities.
     """
 
-    __slots__ = ("_state",)
+    __slots__ = (
+        "_state",
+        "_deep_copy",
+    )
 
     _state: dict[str, Any]
 
@@ -56,13 +62,13 @@ def __init__(
                 assert "first" not in state
 
         """
-
         if isinstance(state, ImmutableState):
             state = state._state
 
         if not isinstance(state, dict) and isinstance(state, Iterable):
             state = dict(state)
 
+        super().__setattr__("_deep_copy", deep_copy)
         super().__setattr__("_state", deepcopy(state) if deep_copy else state)
 
     def __bool__(self) -> bool:
@@ -116,12 +122,12 @@ def __getattr__(self, key: str) -> Any:
         except KeyError as e:
             raise AttributeError from e
 
-    def __copy__(self) -> ImmutableState:
+    def __copy__(self) -> Self:
         """Return a shallow copy of the given state object.
 
         Customizes how the builtin "copy" function will work.
         """
-        return self.__class__(deepcopy(self._state))
+        return self.__class__(self._state, deep_copy=self._deep_copy)
 
     def mutable_copy(self) -> State:
         """Return a mutable copy of the state object.
@@ -129,7 +135,7 @@ def mutable_copy(self) -> State:
         Returns:
             A ``State``
         """
-        return State(self._state)
+        return State(self._state, deep_copy=self._deep_copy)
 
     def dict(self) -> dict[str, Any]:
         """Return a shallow copy of the wrapped dict.
@@ -147,7 +153,7 @@ def __get_validators__(
         yield cls.validate
 
     @classmethod
-    def validate(cls, value: ImmutableState | dict[str, Any] | Iterable[tuple[str, Any]]) -> ImmutableState:  # type: ignore[valid-type]
+    def validate(cls, value: ImmutableState | dict[str, Any] | Iterable[tuple[str, Any]]) -> Self:  # type: ignore[valid-type]
         """Parse a value and instantiate state inside a SignatureModel. This allows us to use custom subclasses of
         state, as well as allows users to decide whether state is mutable or immutable.
 
@@ -157,7 +163,8 @@ def validate(cls, value: ImmutableState | dict[str, Any] | Iterable[tuple[str, A
         Returns:
             An ImmutableState instance
         """
-        return cls(value)
+        deep_copy = value._deep_copy if isinstance(value, ImmutableState) else False
+        return cls(value, deep_copy=deep_copy)
 
 
 class State(ImmutableState, MutableMapping[str, Any]):
@@ -289,13 +296,13 @@ def __delattr__(self, key: str) -> None:
         except KeyError as e:
             raise AttributeError from e
 
-    def copy(self) -> State:
+    def copy(self) -> Self:
         """Return a shallow copy of the state object.
 
         Returns:
             A ``State``
         """
-        return self.__class__(self.dict())
+        return self.__class__(self.dict(), deep_copy=self._deep_copy)
 
     def immutable_copy(self) -> ImmutableState:
         """Return a shallow copy of the state object, setting it to be frozen.
@@ -303,4 +310,4 @@ def immutable_copy(self) -> ImmutableState:
         Returns:
             A ``State``
         """
-        return ImmutableState(self)
+        return ImmutableState(self, deep_copy=self._deep_copy)


Test patch:
diff --git a/tests/datastructures/test_state.py b/tests/datastructures/test_state.py
--- a/tests/datastructures/test_state.py
+++ b/tests/datastructures/test_state.py
@@ -1,4 +1,7 @@
-from typing import Any, Type
+from __future__ import annotations
+
+from copy import copy
+from typing import Any
 
 import pytest
 
@@ -7,7 +10,7 @@
 
 
 @pytest.mark.parametrize("state_class", (ImmutableState, State))
-def test_state_immutable_mapping(state_class: Type[ImmutableState]) -> None:
+def test_state_immutable_mapping(state_class: type[ImmutableState]) -> None:
     state_dict = {"first": 1, "second": 2, "third": 3}
     state = state_class(state_dict, deep_copy=True)
     assert len(state) == 3
@@ -68,3 +71,17 @@ def test_state_copy() -> None:
     copy = state.copy()
     del state.key
     assert copy.key
+
+
+def test_state_copy_deep_copy_false() -> None:
+    state = State({}, deep_copy=False)
+    assert state.copy()._deep_copy is False
+
+
+def test_unpicklable_deep_copy_false() -> None:
+    # a module cannot be deep copied
+    import typing
+
+    state = ImmutableState({"module": typing}, deep_copy=False)
+    copy(state)
+    ImmutableState.validate(state)