33from __future__ import annotations
44
55import asyncio
6- import os
7- import tempfile
86from datetime import datetime
7+ import os
98from os .path import isfile
9+ import tempfile
1010from typing import Any , ForwardRef , TypedDict , TypeVar , get_type_hints
1111
1212import aiofiles
@@ -118,12 +118,15 @@ def _get_defaults(cls: type[T]) -> T:
118118 new_dict [k ] = None # type: ignore[literal-required]
119119 return new_dict # type: ignore[return-value]
120120
121- def _write_state (state_yaml : str , state : dict [str , Any ]) -> None :
121+
122+ def _write_state (state_yaml : str , state : dict [str , Any ] | StateDict ) -> None :
122123 "Write state atomically to a temp file, this prevents reading a file being written to"
123124
124125 dir_name = os .path .dirname (os .path .abspath (state_yaml ))
125126 content = yaml .dump (state )
126- with tempfile .NamedTemporaryFile (mode = "w" , dir = dir_name , delete = False , suffix = ".tmp" ) as tmp :
127+ with tempfile .NamedTemporaryFile (
128+ mode = "w" , dir = dir_name , delete = False , suffix = ".tmp"
129+ ) as tmp :
127130 tmp .write (content )
128131 tmp_path = tmp .name
129132 os .replace (tmp_path , state_yaml )
@@ -139,7 +142,9 @@ async def get_state(state_yaml: str, _already_locked: bool = False) -> StateDict
139142 :type _already_locked: ``bool``
140143 :rtype: ``StateDict``
141144 """
142- if not isfile (state_yaml ): # noqa: PTH113 - isfile is fine and simpler in this case.
145+ if not isfile (
146+ state_yaml
147+ ): # noqa: PTH113 - isfile is fine and simpler in this case.
143148 return _get_defaults (StateDict )
144149
145150 try :
@@ -180,7 +185,6 @@ async def get_state(state_yaml: str, _already_locked: bool = False) -> StateDict
180185 return defaults
181186
182187
183-
184188async def set_state (
185189 state_yaml : str ,
186190 key : str ,
@@ -206,4 +210,4 @@ async def set_state(
206210 # TODO: Use asyncio.get_running_loop() and run_in_executor to write
207211 # to the file in a non blocking manner. Currently, yaml.dump is
208212 # synchronous on the main event loop.
209- _write_state (state_yaml , new_state )
213+ _write_state (state_yaml , new_state )
0 commit comments