99
1010import argparse
1111import logging
12+ from typing import TYPE_CHECKING
13+
1214logger = logging .getLogger (__name__ )
1315import os
1416import string
2224else :
2325 REGEX_AVAILABLE = True
2426
27+ if TYPE_CHECKING :
28+ from typing import Final , Protocol , Union
29+
30+ from typing_extensions import LiteralString
31+
32+ class CallbackProtocol (Protocol ):
33+ def __call__ (
34+ self , word : str , * , all_caps : bool , ** kwargs
35+ ) -> Union [str , None ]: ...
36+
37+
2538__all__ = ['titlecase' ]
2639__version__ = '2.4.1'
2740
28- SMALL = r'a|an|and|as|at|but|by|en|for|if|in|of|on|or|the|to|v\.?|via|vs\.?'
41+ SMALL : 'Final' = r'a|an|and|as|at|but|by|en|for|if|in|of|on|or|the|to|v\.?|via|vs\.?'
2942PUNCT = r"""!"“#$%&'‘()*+,\-–‒—―./:;?@[\\\]_`{|}~"""
3043
3144SMALL_WORDS = regex .compile (r'^(%s)$' % SMALL , regex .I )
4962 APOS_SECOND = regex .compile (r"^[dol]['‘][\w]+(?:['s]{2})?$" , regex .I )
5063 UC_INITIALS = regex .compile (r"^(?:[A-Z]\.|[A-Z]\.[A-Z])+$" )
5164
52-
5365class Immutable (object ):
5466 pass
5567
68+
5669class ImmutableString (str , Immutable ):
5770 pass
5871
72+
5973class ImmutableBytes (bytes , Immutable ):
6074 pass
6175
@@ -66,7 +80,7 @@ def _mark_immutable(text):
6680 return ImmutableString (text )
6781
6882
69- def set_small_word_list (small = SMALL ):
83+ def set_small_word_list (small : 'LiteralString' = SMALL ) -> None :
7084 global SMALL_WORDS
7185 global SMALL_FIRST
7286 global SMALL_LAST
@@ -77,7 +91,13 @@ def set_small_word_list(small=SMALL):
7791 SUBPHRASE = regex .compile (r'([:.;?!][ ])(%s)' % small )
7892
7993
80- def titlecase (text , callback = None , small_first_last = True , preserve_blank_lines = False , normalise_space_characters = False ):
94+ def titlecase (
95+ text : str ,
96+ callback : 'Union[CallbackProtocol, None]' = None ,
97+ small_first_last : bool = True ,
98+ preserve_blank_lines : bool = False ,
99+ normalise_space_characters : bool = False ,
100+ ) -> 'LiteralString' :
81101 """
82102 :param text: Titlecases input text
83103 :param callback: Callback function that returns the titlecase version of a specific word
@@ -214,7 +234,9 @@ def titlecase(text, callback=None, small_first_last=True, preserve_blank_lines=F
214234 return result
215235
216236
217- def create_wordlist_filter_from_file (file_path ):
237+ def create_wordlist_filter_from_file (
238+ file_path : 'Union[str, None]' ,
239+ ) -> 'CallbackProtocol' :
218240 '''
219241 Load a list of abbreviations from the file with the provided path,
220242 reading one abbreviation from each line, and return a callback to
0 commit comments