75
75
Sequence ,
76
76
Set ,
77
77
Tuple ,
78
+ Type ,
78
79
TypeVar ,
79
80
Union ,
80
81
cast
@@ -143,7 +144,7 @@ class Block(ABC):
143
144
a reference to a container wherein the object resides.
144
145
"""
145
146
146
- def __init__ (self , container : Container , ** kwargs ):
147
+ def __init__ (self , container : Union [ Container , List [ "Block" ], None ] , ** kwargs ):
147
148
self ._container = container
148
149
self .lines : List [str ] = []
149
150
self ._updated : bool = False
@@ -155,13 +156,13 @@ def __str__(self) -> str:
155
156
def __len__ (self ) -> int :
156
157
return len (self .lines )
157
158
158
- def __eq__ (self , other ) -> bool :
159
+ def __eq__ (self , other ) -> bool : # noqa: MAN001
159
160
if isinstance (other , self .__class__ ):
160
161
return self .lines == other .lines
161
162
else :
162
163
return False
163
164
164
- def add_line (self , line : str ):
165
+ def add_line (self , line : str ) -> "Block" :
165
166
"""
166
167
Add a line to the current block.
167
168
@@ -172,7 +173,7 @@ def add_line(self, line: str):
172
173
return self
173
174
174
175
@property
175
- def container (self ) -> Container :
176
+ def container (self ) -> Union [ Container , List [ "Block" ], None ] :
176
177
return self ._container
177
178
178
179
# @property
@@ -199,7 +200,7 @@ class BlockBuilder:
199
200
Builder that injects blocks at a given index position.
200
201
"""
201
202
202
- def __init__ (self , container : Container , idx ):
203
+ def __init__ (self , container : Container , idx : int ):
203
204
self ._container : Container = container
204
205
self ._idx = idx
205
206
@@ -292,7 +293,7 @@ class Comment(Block):
292
293
Comment block.
293
294
"""
294
295
295
- def __init__ (self , container = None ):
296
+ def __init__ (self , container : Union [ Container , List [ Block ], None ] = None ):
296
297
super ().__init__ (container = container )
297
298
298
299
def __repr__ (self ) -> str :
@@ -307,7 +308,7 @@ class Space(Block):
307
308
def __init__ (self , container = None ):
308
309
super ().__init__ (container = container )
309
310
310
- def __repr__ (self ):
311
+ def __repr__ (self ) -> str :
311
312
return "<Space>"
312
313
313
314
@@ -326,7 +327,7 @@ def __init__(self, name: str, container, **kwargs):
326
327
self ._updated = False
327
328
super ().__init__ (container = container , ** kwargs )
328
329
329
- def add_option (self , entry : "Option" ):
330
+ def add_option (self , entry : "Option" ) -> "Section" :
330
331
"""
331
332
Add an Option object to the section.
332
333
@@ -338,7 +339,7 @@ def add_option(self, entry: "Option"):
338
339
self ._structure .append (entry )
339
340
return self
340
341
341
- def add_comment (self , line : str ):
342
+ def add_comment (self , line : str ) -> "Section" :
342
343
"""
343
344
Add a Comment object to the section.
344
345
@@ -353,7 +354,7 @@ def add_comment(self, line: str):
353
354
self .last_item .add_line (line )
354
355
return self
355
356
356
- def add_space (self , line : str ):
357
+ def add_space (self , line : str ) -> "Section" :
357
358
"""
358
359
Add a Space object to the section.
359
360
@@ -368,7 +369,7 @@ def add_space(self, line: str):
368
369
self .last_item .add_line (line )
369
370
return self
370
371
371
- def _get_option_idx (self , key ) :
372
+ def _get_option_idx (self , key : str ) -> int :
372
373
idx = [i for i , entry in enumerate (self ._structure ) if isinstance (entry , Option ) and entry .key == key ]
373
374
if idx :
374
375
return idx [0 ]
@@ -391,12 +392,12 @@ def __str__(self) -> str:
391
392
def __repr__ (self ) -> str :
392
393
return f"<Section: { self .name } >"
393
394
394
- def __getitem__ (self , key ) -> "Option" :
395
+ def __getitem__ (self , key : str ): # noqa: MAN002
395
396
if key not in self .options ():
396
397
raise KeyError (key )
397
398
return self ._structure [self ._get_option_idx (key = key )]
398
399
399
- def __setitem__ (self , key , value ) :
400
+ def __setitem__ (self , key : str , value : Any ) -> None :
400
401
str_value = convert_to_string (value , key )
401
402
402
403
if key in self :
@@ -407,26 +408,26 @@ def __setitem__(self, key, value):
407
408
option .value = str_value
408
409
self ._structure .append (option )
409
410
410
- def __delitem__ (self , key ) :
411
+ def __delitem__ (self , key : str ) -> None :
411
412
if key not in self .options ():
412
413
raise KeyError (key )
413
414
idx = self ._get_option_idx (key = key )
414
415
del self ._structure [idx ]
415
416
416
- def __contains__ (self , key ):
417
+ def __contains__ (self , key ) -> bool : # noqa: MAN001
417
418
return key in self .options ()
418
419
419
420
def __len__ (self ) -> int :
420
421
return len (self ._structure )
421
422
422
- def __iter__ (self ):
423
+ def __iter__ (self ) -> Iterator [ Block ] :
423
424
"""
424
425
Return all entries, not just options.
425
426
"""
426
427
427
428
return self ._structure .__iter__ ()
428
429
429
- def __eq__ (self , other ) -> bool :
430
+ def __eq__ (self , other ) -> bool : # noqa: MAN001
430
431
if isinstance (other , self .__class__ ):
431
432
return self .name == other .name and self ._structure == other ._structure
432
433
else :
@@ -471,7 +472,7 @@ def name(self) -> str:
471
472
return self ._name
472
473
473
474
@name .setter
474
- def name (self , value ) :
475
+ def name (self , value : str ) -> None :
475
476
self ._name = str (value )
476
477
self ._updated = True
477
478
@@ -513,10 +514,10 @@ def __init__(
513
514
self ,
514
515
key : str ,
515
516
value : Optional [str ],
516
- container ,
517
+ container : Container ,
517
518
delimiter : str = '=' ,
518
519
space_around_delimiters : bool = True ,
519
- line = None ,
520
+ line : Optional [ str ] = None ,
520
521
):
521
522
super ().__init__ (container = container )
522
523
self ._key : str = key
@@ -530,9 +531,10 @@ def __init__(
530
531
if line :
531
532
self .lines .append (line )
532
533
533
- def add_line (self , line ) -> None :
534
+ def add_line (self , line : str ) -> "Option" :
534
535
super ().add_line (line )
535
536
self ._values .append (line .strip ())
537
+ return self
536
538
537
539
def _join_multiline_value (self ) -> None :
538
540
if not self ._multiline_value_joined and not self ._value_is_none :
@@ -570,7 +572,7 @@ def key(self) -> str:
570
572
return self ._key
571
573
572
574
@key .setter
573
- def key (self , value : str ):
575
+ def key (self , value : str ) -> None :
574
576
self ._join_multiline_value ()
575
577
self ._key = value
576
578
self ._updated = True
@@ -581,7 +583,7 @@ def value(self) -> str:
581
583
return cast (str , self ._value )
582
584
583
585
@value .setter
584
- def value (self , value : str ):
586
+ def value (self , value : str ) -> None :
585
587
self ._updated = True
586
588
self ._multiline_value_joined = True
587
589
self ._value = value
@@ -695,14 +697,14 @@ def __init__(
695
697
self ._empty_lines_in_values = False
696
698
super ().__init__ ()
697
699
698
- def _get_section_idx (self , name ) :
700
+ def _get_section_idx (self , name : str ) -> int :
699
701
idx = [i for i , entry in enumerate (self ._structure ) if isinstance (entry , Section ) and entry .name == name ]
700
702
if idx :
701
703
return idx [0 ]
702
704
else :
703
705
raise ValueError
704
706
705
- def read (self , filename : PathLike , encoding : Optional [str ] = "UTF-8" ):
707
+ def read (self , filename : PathLike , encoding : Optional [str ] = "UTF-8" ) -> None :
706
708
"""
707
709
Read and parse a filename.
708
710
@@ -711,11 +713,11 @@ def read(self, filename: PathLike, encoding: Optional[str] = "UTF-8"):
711
713
"""
712
714
713
715
with open (filename , encoding = encoding ) as fp :
714
- self ._read (fp , filename )
716
+ self ._read (fp , os . fspath ( filename ) )
715
717
716
718
self ._filename = os .path .abspath (filename )
717
719
718
- def read_file (self , f : IO , source : Optional [str ] = None ):
720
+ def read_file (self , f : IO , source : Optional [str ] = None ) -> None :
719
721
"""
720
722
Like read() but the argument must be a file-like object.
721
723
@@ -736,7 +738,7 @@ def read_file(self, f: IO, source: Optional[str] = None):
736
738
source = "<???>"
737
739
self ._read (f , source )
738
740
739
- def read_string (self , string : str , source : str = "<string>" ):
741
+ def read_string (self , string : str , source : str = "<string>" ) -> None :
740
742
"""
741
743
Read configuration from a given string.
742
744
@@ -756,12 +758,12 @@ def optionxform(self, optionstr: str) -> str:
756
758
"""
757
759
return optionstr .lower ()
758
760
759
- def _update_curr_block (self , block_type ) :
761
+ def _update_curr_block (self , block_type : Type [ Block ]) -> None :
760
762
if not isinstance (self .last_item , block_type ):
761
763
new_block = block_type (container = self )
762
764
self ._structure .append (new_block )
763
765
764
- def _add_comment (self , line ) :
766
+ def _add_comment (self , line : str ) -> None :
765
767
if isinstance (self .last_item , Section ):
766
768
self .last_item .add_comment (line )
767
769
elif self .last_item is not None :
@@ -770,12 +772,12 @@ def _add_comment(self, line):
770
772
# else:
771
773
# raise ValueError("Cannot add a comment without somewhere to add it to.")
772
774
773
- def _add_section (self , sectname , line ) :
775
+ def _add_section (self , sectname : str , line : str ) -> None :
774
776
new_section = Section (sectname , container = self )
775
777
new_section .add_line (line )
776
778
self ._structure .append (new_section )
777
779
778
- def _add_option (self , key , vi , value , line ) :
780
+ def _add_option (self , key : str , vi : str , value : Optional [ str ] , line : str ) -> None :
779
781
entry = Option (
780
782
key ,
781
783
value ,
@@ -786,14 +788,14 @@ def _add_option(self, key, vi, value, line):
786
788
)
787
789
self .last_item .add_option (entry )
788
790
789
- def _add_space (self , line ) :
791
+ def _add_space (self , line : str ) -> None :
790
792
if isinstance (self .last_item , Section ):
791
793
self .last_item .add_space (line )
792
794
else :
793
795
self ._update_curr_block (Space )
794
796
self .last_item .add_line (line )
795
797
796
- def _read (self , fp , fpname ) :
798
+ def _read (self , fp : IO , fpname : str ) -> None :
797
799
"""
798
800
Parse a sectioned configuration file.
799
801
@@ -928,7 +930,7 @@ def _read(self, fp, fpname):
928
930
if e :
929
931
raise e
930
932
931
- def _handle_error (self , exc , fpname , lineno , line ) :
933
+ def _handle_error (self , exc : Optional [ ParsingError ] , fpname : str , lineno : int , line : str ) -> ParsingError :
932
934
if not exc :
933
935
exc = ParsingError (fpname )
934
936
exc .append (lineno , repr (line ))
@@ -1013,7 +1015,7 @@ def __delitem__(self, section: str) -> None:
1013
1015
raise KeyError (section )
1014
1016
self .remove_section (section )
1015
1017
1016
- def __contains__ (self , section ) -> bool :
1018
+ def __contains__ (self , section ) -> bool : # noqa: MAN001
1017
1019
"""
1018
1020
Returns whether the given section exists.
1019
1021
"""
@@ -1034,13 +1036,13 @@ def __iter__(self) -> Iterator[Block]:
1034
1036
1035
1037
return self ._structure .__iter__ ()
1036
1038
1037
- def __eq__ (self , other ) -> bool :
1039
+ def __eq__ (self , other ) -> bool : # noqa: MAN001
1038
1040
if isinstance (other , self .__class__ ):
1039
1041
return self ._structure == other ._structure
1040
1042
else :
1041
1043
return False
1042
1044
1043
- def add_section (self , section : Union [str , Section ]):
1045
+ def add_section (self , section : Union [str , Section ]) -> None :
1044
1046
"""Create a new section in the configuration.
1045
1047
1046
1048
:raises: :exc:`~.DuplicateSectionError` if a section by the specified name already exists.
@@ -1070,7 +1072,7 @@ def options(self, section: str) -> List[str]:
1070
1072
raise NoSectionError (section ) from None
1071
1073
return self [section ].options ()
1072
1074
1073
- def get (self , section : str , option : str ): # type: ignore[override]
1075
+ def get (self , section : str , option : str ) -> "Option" : # type: ignore[override]
1074
1076
"""Gets an option value for a given section.
1075
1077
1076
1078
:param section: section name
@@ -1198,7 +1200,7 @@ def to_dict(self) -> Dict:
1198
1200
return {sect : self [sect ].to_dict () for sect in self .sections ()}
1199
1201
1200
1202
1201
- def convert_to_string (value , key ) :
1203
+ def convert_to_string (value : Any , key : str ) -> str :
1202
1204
if isinstance (value , str ):
1203
1205
1204
1206
split_lines = value .split ('\n ' )
0 commit comments