mirror of
https://github.com/home-assistant/core.git
synced 2025-11-08 18:39:30 +00:00
Make typing checks more strict (#14429)
## Description: Make typing checks more strict: add `--strict-optional` flag that forbids implicit None return type. This flag will become default in the next version of mypy (0.600) Add `homeassistant/util/` to checked dirs. ## Checklist: - [x] The code change is tested and works locally. - [x] Local tests pass with `tox`. **Your PR cannot be merged unless tests pass**
This commit is contained in:
@@ -8,8 +8,6 @@ from homeassistant.exceptions import HomeAssistantError
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
_UNDEFINED = object()
|
||||
|
||||
|
||||
class SerializationError(HomeAssistantError):
|
||||
"""Error serializing the data to JSON."""
|
||||
@@ -19,7 +17,7 @@ class WriteError(HomeAssistantError):
|
||||
"""Error writing the data."""
|
||||
|
||||
|
||||
def load_json(filename: str, default: Union[List, Dict] = _UNDEFINED) \
|
||||
def load_json(filename: str, default: Union[List, Dict, None] = None) \
|
||||
-> Union[List, Dict]:
|
||||
"""Load JSON data from a file and return as dict or list.
|
||||
|
||||
@@ -37,7 +35,7 @@ def load_json(filename: str, default: Union[List, Dict] = _UNDEFINED) \
|
||||
except OSError as error:
|
||||
_LOGGER.exception('JSON file reading failed: %s', filename)
|
||||
raise HomeAssistantError(error)
|
||||
return {} if default is _UNDEFINED else default
|
||||
return {} if default is None else default
|
||||
|
||||
|
||||
def save_json(filename: str, data: Union[List, Dict]):
|
||||
@@ -46,9 +44,9 @@ def save_json(filename: str, data: Union[List, Dict]):
|
||||
Returns True on success.
|
||||
"""
|
||||
try:
|
||||
data = json.dumps(data, sort_keys=True, indent=4)
|
||||
json_data = json.dumps(data, sort_keys=True, indent=4)
|
||||
with open(filename, 'w', encoding='utf-8') as fdesc:
|
||||
fdesc.write(data)
|
||||
fdesc.write(json_data)
|
||||
except TypeError as error:
|
||||
_LOGGER.exception('Failed to serialize to JSON: %s',
|
||||
filename)
|
||||
|
||||
Reference in New Issue
Block a user