mirror of
https://github.com/freedombox/FreedomBox.git
synced 2026-11-29 08:25:10 +00:00
*: Utilize newer 3.10 syntax for type hints
Tests: - mypy does not show any errors. - Installing ejabberd app works. Privileged actions run fine. - Unit tests work. - No additional testing was done as type annotations don't have any effect at runtime. Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org> Reviewed-by: James Valleroy <jvalleroy@mailbox.org>
This commit is contained in:
1 parent
a86a86d605
commit
38ece87c6c
29 files changed
+116
-140
No files matched your search
+8
-6
@@ -9,6 +9,7 @@ import logging
|
||||
import os
|
||||
import sys
|
||||
import traceback
|
||||
import types
|
||||
import typing
|
||||
|
||||
import plinth.log
|
||||
@@ -166,10 +167,9 @@ def _assert_valid_type(arg_name, value, annotation):
|
||||
|
||||
return
|
||||
|
||||
if not hasattr(annotation, '__origin__'):
|
||||
raise TypeError('Unsupported annotation type')
|
||||
|
||||
if annotation.__origin__ == typing.Union:
|
||||
# 'int | str' or 'typing.Union[int, str]'
|
||||
if (isinstance(annotation, types.UnionType)
|
||||
or getattr(annotation, '__origin__', None) == typing.Union):
|
||||
for arg in annotation.__args__:
|
||||
try:
|
||||
_assert_valid_type(arg_name, value, arg)
|
||||
@@ -179,7 +179,8 @@ def _assert_valid_type(arg_name, value, annotation):
|
||||
|
||||
raise TypeError(f'Expected one of unioned types for {arg_name}')
|
||||
|
||||
if annotation.__origin__ == list:
|
||||
# 'list[int]' or 'typing.List[int]'
|
||||
if getattr(annotation, '__origin__', None) == list:
|
||||
if not isinstance(value, list):
|
||||
raise TypeError(f'Expected type list for {arg_name}')
|
||||
|
||||
@@ -189,7 +190,8 @@ def _assert_valid_type(arg_name, value, annotation):
|
||||
|
||||
return
|
||||
|
||||
if annotation.__origin__ == dict:
|
||||
# 'list[dict]' or 'typing.List[dict]'
|
||||
if getattr(annotation, '__origin__', None) == dict:
|
||||
if not isinstance(value, dict):
|
||||
raise TypeError(f'Expected type dict for {arg_name}')
|
||||
|
||||
|
||||
Reference in new issue
Block a user