Skip to content

Commit 37c911d

Browse files
committed
basic typing
1 parent 42f43e2 commit 37c911d

2 files changed

Lines changed: 27 additions & 5 deletions

File tree

titlecase/__init__.py

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
import argparse
1111
import logging
12+
from typing import TYPE_CHECKING
13+
1214
logger = logging.getLogger(__name__)
1315
import os
1416
import string
@@ -22,10 +24,21 @@
2224
else:
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\.?'
2942
PUNCT = r"""!"“#$%&'‘()*+,\-–‒—―./:;?@[\\\]_`{|}~"""
3043

3144
SMALL_WORDS = regex.compile(r'^(%s)$' % SMALL, regex.I)
@@ -49,13 +62,14 @@
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-
5365
class Immutable(object):
5466
pass
5567

68+
5669
class ImmutableString(str, Immutable):
5770
pass
5871

72+
5973
class 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

titlecase/py.typed

Whitespace-only changes.

0 commit comments

Comments
 (0)