Type check various base components (#25878)
* Type check various component base classes, disabling bunch of checks for now * Type hint fixes * Help mypy out some * Add more type hints
This commit is contained in:
1 parent
cf90e49b50
commit
b738082dad
59 files changed
+233
-34
No files matched your search
@@ -27,6 +27,9 @@ from .real_ip import setup_real_ip
|
||||
from .static import CACHE_HEADERS, CachingStaticResource
|
||||
from .view import HomeAssistantView # noqa
|
||||
|
||||
|
||||
# mypy: allow-untyped-defs, no-check-untyped-defs
|
||||
|
||||
DOMAIN = "http"
|
||||
|
||||
CONF_API_PASSWORD = "api_password"
|
||||
|
||||
@@ -14,6 +14,9 @@ from homeassistant.util import dt as dt_util
|
||||
|
||||
from .const import KEY_AUTHENTICATED, KEY_HASS_USER, KEY_REAL_IP
|
||||
|
||||
|
||||
# mypy: allow-untyped-defs, no-check-untyped-defs
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
DATA_API_PASSWORD = "api_password"
|
||||
|
||||
@@ -3,6 +3,7 @@ from collections import defaultdict
|
||||
from datetime import datetime
|
||||
from ipaddress import ip_address
|
||||
import logging
|
||||
from typing import List, Optional
|
||||
|
||||
from aiohttp.web import middleware
|
||||
from aiohttp.web_exceptions import HTTPForbidden, HTTPUnauthorized
|
||||
@@ -16,6 +17,9 @@ from homeassistant.util.yaml import dump
|
||||
|
||||
from .const import KEY_REAL_IP
|
||||
|
||||
|
||||
# mypy: allow-incomplete-defs, allow-untyped-defs, no-check-untyped-defs
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
KEY_BANNED_IPS = "ha_banned_ips"
|
||||
@@ -155,7 +159,7 @@ async def process_success_login(request):
|
||||
class IpBan:
|
||||
"""Represents banned IP address."""
|
||||
|
||||
def __init__(self, ip_ban: str, banned_at: datetime = None) -> None:
|
||||
def __init__(self, ip_ban: str, banned_at: Optional[datetime] = None) -> None:
|
||||
"""Initialize IP Ban object."""
|
||||
self.ip_address = ip_address(ip_ban)
|
||||
self.banned_at = banned_at or datetime.utcnow()
|
||||
@@ -163,7 +167,7 @@ class IpBan:
|
||||
|
||||
async def async_load_ip_bans_config(hass: HomeAssistant, path: str):
|
||||
"""Load list of banned IPs from config file."""
|
||||
ip_list = []
|
||||
ip_list: List[IpBan] = []
|
||||
|
||||
try:
|
||||
list_ = await hass.async_add_executor_job(load_yaml_config_file, path)
|
||||
|
||||
@@ -5,6 +5,9 @@ from aiohttp.hdrs import ACCEPT, CONTENT_TYPE, ORIGIN, AUTHORIZATION
|
||||
from homeassistant.const import HTTP_HEADER_HA_AUTH, HTTP_HEADER_X_REQUESTED_WITH
|
||||
from homeassistant.core import callback
|
||||
|
||||
|
||||
# mypy: allow-untyped-defs, no-check-untyped-defs
|
||||
|
||||
ALLOWED_CORS_HEADERS = [
|
||||
ORIGIN,
|
||||
ACCEPT,
|
||||
|
||||
@@ -4,6 +4,9 @@ import logging
|
||||
|
||||
import voluptuous as vol
|
||||
|
||||
|
||||
# mypy: allow-untyped-defs
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
|
||||
|
||||
@@ -9,6 +9,9 @@ from homeassistant.core import callback
|
||||
from .const import KEY_REAL_IP
|
||||
|
||||
|
||||
# mypy: allow-untyped-defs
|
||||
|
||||
|
||||
@callback
|
||||
def setup_real_ip(app, use_x_forwarded_for, trusted_proxies):
|
||||
"""Create IP Ban middleware for the app."""
|
||||
|
||||
@@ -6,6 +6,9 @@ from aiohttp.web import FileResponse
|
||||
from aiohttp.web_exceptions import HTTPNotFound, HTTPForbidden
|
||||
from aiohttp.web_urldispatcher import StaticResource
|
||||
|
||||
|
||||
# mypy: allow-untyped-defs
|
||||
|
||||
CACHE_TIME = 31 * 86400 # = 1 month
|
||||
CACHE_HEADERS = {hdrs.CACHE_CONTROL: "public, max-age={}".format(CACHE_TIME)}
|
||||
|
||||
@@ -39,7 +42,8 @@ class CachingStaticResource(StaticResource):
|
||||
if filepath.is_dir():
|
||||
return await super()._handle(request)
|
||||
if filepath.is_file():
|
||||
return FileResponse(
|
||||
# type ignore: https://github.com/aio-libs/aiohttp/pull/3976
|
||||
return FileResponse( # type: ignore
|
||||
filepath, chunk_size=self._chunk_size, headers=CACHE_HEADERS
|
||||
)
|
||||
raise HTTPNotFound
|
||||
@@ -2,6 +2,7 @@
|
||||
import asyncio
|
||||
import json
|
||||
import logging
|
||||
from typing import List, Optional
|
||||
|
||||
from aiohttp import web
|
||||
from aiohttp.web_exceptions import (
|
||||
@@ -22,11 +23,14 @@ from .const import KEY_AUTHENTICATED, KEY_HASS, KEY_REAL_IP
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
|
||||
# mypy: allow-untyped-defs, no-check-untyped-defs
|
||||
|
||||
|
||||
class HomeAssistantView:
|
||||
"""Base view for all views."""
|
||||
|
||||
url = None
|
||||
extra_urls = []
|
||||
url: Optional[str] = None
|
||||
extra_urls: List[str] = []
|
||||
# Views inheriting from this class can override this
|
||||
requires_auth = True
|
||||
cors_allowed = False
|
||||
|
||||
Reference in new issue
Block a user