From 7bfef94e30b0099fe7162ec70209b62f9a20117e Mon Sep 17 00:00:00 2001 From: Cristian Le Date: Tue, 15 Aug 2023 14:21:20 +0200 Subject: [PATCH] Conditional typing Signed-off-by: Cristian Le --- fmf/base.py | 38 +++++++++++++++++++++----------------- 1 file changed, 21 insertions(+), 17 deletions(-) diff --git a/fmf/base.py b/fmf/base.py index 996e07ab..d6216b16 100644 --- a/fmf/base.py +++ b/fmf/base.py @@ -11,15 +11,18 @@ ValuesView) from io import open from pprint import pformat as pretty -# TODO: py3.10: typing.Optional, typing.Union -> '|' operator -from typing import Any, Optional, Union +from typing import TYPE_CHECKING -from _typeshed import StrPath +if TYPE_CHECKING: + # TODO: py3.10: typing.Optional, typing.Union -> '|' operator + from typing import Any, Optional, Union -if sys.version_info >= (3, 10): - from typing import Self, TypeAlias -else: - from typing_extensions import TypeAlias, Self + from _typeshed import StrPath + + if sys.version_info >= (3, 10): + from typing import Self, TypeAlias + else: + from typing_extensions import TypeAlias, Self import jsonschema from ruamel.yaml import YAML @@ -38,16 +41,17 @@ MAIN = "main" + SUFFIX IGNORED_DIRECTORIES = ['/dev', '/proc', '/sys'] -# TypeHints -RawDataType: TypeAlias = Union[None, int, float, str, bool] -ListDataType: TypeAlias = list[Union[RawDataType, 'ListDataType', 'DictDataType']] -DictDataType: TypeAlias = dict[str, Union[RawDataType, ListDataType, 'DictDataType']] -# Equivalent to: -# JSON: TypeAlias = dict[str, "JSON"] | list["JSON"] | str | int | float | bool | None -DataType: TypeAlias = Union[RawDataType, ListDataType, DictDataType] -TreeData: TypeAlias = dict[str, DataType] -TreeDataPath: TypeAlias = Union[TreeData, str] # Either TreeData or path -JsonSchema: TypeAlias = Mapping[str, Any] +if TYPE_CHECKING: + # TypeHints + RawDataType: TypeAlias = Union[None, int, float, str, bool] + ListDataType: TypeAlias = list[Union[RawDataType, 'ListDataType', 'DictDataType']] + DictDataType: TypeAlias = dict[str, Union[RawDataType, ListDataType, 'DictDataType']] + # Equivalent to: + # JSON: TypeAlias = dict[str, "JSON"] | list["JSON"] | str | int | float | bool | None + DataType: TypeAlias = Union[RawDataType, ListDataType, DictDataType] + TreeData: TypeAlias = dict[str, DataType] + TreeDataPath: TypeAlias = Union[TreeData, str] # Either TreeData or path + JsonSchema: TypeAlias = Mapping[str, Any] # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~