Skip to content

Commit

Permalink
Conditional typing
Browse files Browse the repository at this point in the history
Signed-off-by: Cristian Le <[email protected]>
  • Loading branch information
LecrisUT committed Aug 18, 2023
1 parent 1c45048 commit 7bfef94
Showing 1 changed file with 21 additions and 17 deletions.
38 changes: 21 additions & 17 deletions fmf/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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]


# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Expand Down

0 comments on commit 7bfef94

Please sign in to comment.