11"""Manage creation of schema."""
2+ from __future__ import annotations
23import sys
34import logging
45import operator
5- from os import path
6- from schema import Schema , SchemaError , Or , Optional
7- from ruamel .yaml import YAML
8- from ruamel .yaml .comments import CommentedMap , CommentedSeq
6+ from pathlib import Path
7+ from typing import Union , List , Dict , Any
8+ from ruamel .yaml import YAML # type: ignore[import]
9+ from ruamel .yaml .comments import CommentedMap , CommentedSeq # type: ignore[import]
10+ from schema import Schema , SchemaError , Or , Optional # type: ignore[import]
911
1012from .tools import MAX_DATE_STR
1113from .schema_tags import Tags , OutputTags , BuildTags , LangTags
1214
1315log = logging .getLogger ("GHC" )
1416
15- SCHEMA_FILE = path . join ( path . dirname ( path . dirname ( __file__ )), "schema" , "schema.yml" )
17+ SCHEMA_FILE = Path ( __file__ ). parent . parent / "schema" / "schema.yml"
1618
1719
1820class SchemaManager :
1921 """Manage schema creation."""
2022
21- def __init__ (self , file_name ):
23+ GenericListType = List [Union [Any , Dict [str , Any ], List [Any ]]]
24+ GenericDictType = Dict [str , Union [Any , Dict [str , Any ], List [Any ]]]
25+
26+ def __init__ (self : SchemaManager , file_name : Path ):
2227 """Create a schema for my tests."""
28+ # TODO(igor): simplify by moving parts out of here into separate variables.
2329 self .__schema = Schema (
2430 {
2531 Tags .FOLDER_TAG : str ,
@@ -63,8 +69,9 @@ def __init__(self, file_name):
6369 }
6470 )
6571 yaml = YAML ()
66- yaml .width = 4096 # big enough value to prevent wrapping
67- yaml .explicit_start = True
72+ # big enough value to prevent wrapping
73+ yaml .width = 4096 # type: ignore[assignment]
74+ yaml .explicit_start = True # type: ignore[assignment]
6875 yaml .indent (mapping = 2 , sequence = 4 , offset = 2 )
6976 with open (file_name , "r" ) as stream :
7077 contents = SchemaManager .__to_simple_dict (yaml .load (stream ))
@@ -83,8 +90,11 @@ def __init__(self, file_name):
8390 except OSError :
8491 log .debug ("Cannot write schema file. We only use this while developing." )
8592
86- def __to_simple_list (commented_seq ):
87- simple_list = []
93+ @staticmethod
94+ def __to_simple_list (
95+ commented_seq : List [Union [Any , CommentedSeq , CommentedMap ]]
96+ ) -> SchemaManager .GenericListType :
97+ simple_list : SchemaManager .GenericListType = []
8898 for value in commented_seq :
8999 if isinstance (value , CommentedSeq ):
90100 simple_list .append (SchemaManager .__to_simple_list (value ))
@@ -94,8 +104,11 @@ def __to_simple_list(commented_seq):
94104 simple_list .append (value )
95105 return simple_list
96106
97- def __to_simple_dict (commented_map ):
98- simple_dict = {}
107+ @staticmethod
108+ def __to_simple_dict (
109+ commented_map : Dict [str , Union [Any , CommentedSeq , CommentedMap ]],
110+ ) -> SchemaManager .GenericDictType :
111+ simple_dict : SchemaManager .GenericDictType = {}
99112 for key , value in commented_map .items ():
100113 if isinstance (value , CommentedMap ):
101114 simple_dict [key ] = SchemaManager .__to_simple_dict (value )
@@ -106,7 +119,7 @@ def __to_simple_dict(commented_map):
106119 return simple_dict
107120
108121 @property
109- def validated_yaml (self ):
122+ def validated_yaml (self ) -> dict :
110123 """Return validated yaml."""
111124 return self .__validated_yaml
112125
@@ -115,9 +128,13 @@ def schema(self):
115128 """Return schema."""
116129 return self .__schema
117130
131+ # pylint: disable=R0911
132+ # This method needs this many returns.
118133 @staticmethod
119- def __sanitize_value (input_var ):
134+ def __sanitize_value (input_var : Union [ dict , Optional , Or , Any ] ):
120135 """Use the schema and create an example file from it."""
136+ # pylint: disable=W0212
137+ # This seems to be the only way to get to schema value.
121138 if isinstance (input_var , dict ):
122139 new_dict = {}
123140 for key , val in input_var .items ():
0 commit comments