11from __future__ import annotations
22
33import os
4+ import sys
45from pathlib import Path
6+ from typing import TYPE_CHECKING , Any
57
68from tomlkit import exceptions , parse , table
79
810from commitizen .exceptions import InvalidConfigurationError
911
1012from .base_config import BaseConfig
1113
14+ if TYPE_CHECKING :
15+ import sys
16+
17+ # Self is Python 3.11+ but backported in typing-extensions
18+ if sys .version_info < (3 , 11 ):
19+ from typing_extensions import Self
20+ else :
21+ from typing import Self
22+
1223
1324class TomlConfig (BaseConfig ):
1425 def __init__ (self , * , data : bytes | str , path : Path | str ):
1526 super ().__init__ ()
1627 self .is_empty_config = False
17- self .path = path # type: ignore
28+ self .path : Path = path # type: ignore
1829 self ._parse_setting (data )
1930
20- def init_empty_config_content (self ):
31+ def init_empty_config_content (self ) -> None :
2132 if os .path .isfile (self .path ):
2233 with open (self .path , "rb" ) as input_toml_file :
2334 parser = parse (input_toml_file .read ())
@@ -27,10 +38,10 @@ def init_empty_config_content(self):
2738 with open (self .path , "wb" ) as output_toml_file :
2839 if parser .get ("tool" ) is None :
2940 parser ["tool" ] = table ()
30- parser ["tool" ]["commitizen" ] = table ()
41+ parser ["tool" ]["commitizen" ] = table () # type: ignore
3142 output_toml_file .write (parser .as_string ().encode (self .encoding ))
3243
33- def set_key (self , key , value ) :
44+ def set_key (self , key : str , value : Any ) -> Self :
3445 """Set or update a key in the conf.
3546
3647 For now only strings are supported.
@@ -39,7 +50,7 @@ def set_key(self, key, value):
3950 with open (self .path , "rb" ) as f :
4051 parser = parse (f .read ())
4152
42- parser ["tool" ]["commitizen" ][key ] = value
53+ parser ["tool" ]["commitizen" ][key ] = value # type: ignore
4354 with open (self .path , "wb" ) as f :
4455 f .write (parser .as_string ().encode (self .encoding ))
4556 return self
0 commit comments