22
33import os
44from pathlib import Path
5+ from typing import TYPE_CHECKING , Any
56
67from tomlkit import exceptions , parse , table
78
89from commitizen .exceptions import InvalidConfigurationError
910
1011from .base_config import BaseConfig
1112
13+ if TYPE_CHECKING :
14+ import sys
15+
16+ # Self is Python 3.11+ but backported in typing-extensions
17+ if sys .version_info < (3 , 11 ):
18+ from typing_extensions import Self
19+ else :
20+ from typing import Self
21+
1222
1323class TomlConfig (BaseConfig ):
1424 def __init__ (self , * , data : bytes | str , path : Path | str ):
1525 super ().__init__ ()
1626 self .is_empty_config = False
17- self .path = path # type: ignore
27+ self .path : Path = path # type: ignore
1828 self ._parse_setting (data )
1929
20- def init_empty_config_content (self ):
30+ def init_empty_config_content (self ) -> None :
2131 if os .path .isfile (self .path ):
2232 with open (self .path , "rb" ) as input_toml_file :
2333 parser = parse (input_toml_file .read ())
@@ -27,10 +37,10 @@ def init_empty_config_content(self):
2737 with open (self .path , "wb" ) as output_toml_file :
2838 if parser .get ("tool" ) is None :
2939 parser ["tool" ] = table ()
30- parser ["tool" ]["commitizen" ] = table ()
40+ parser ["tool" ]["commitizen" ] = table () # type: ignore
3141 output_toml_file .write (parser .as_string ().encode (self .encoding ))
3242
33- def set_key (self , key , value ) :
43+ def set_key (self , key : str , value : Any ) -> Self :
3444 """Set or update a key in the conf.
3545
3646 For now only strings are supported.
@@ -39,7 +49,7 @@ def set_key(self, key, value):
3949 with open (self .path , "rb" ) as f :
4050 parser = parse (f .read ())
4151
42- parser ["tool" ]["commitizen" ][key ] = value
52+ parser ["tool" ]["commitizen" ][key ] = value # type: ignore
4353 with open (self .path , "wb" ) as f :
4454 f .write (parser .as_string ().encode (self .encoding ))
4555 return self
0 commit comments