Skip to content

Commit 928af11

Browse files
authored
feat: add type hints (#122)
2 parents 0329415 + 86b54e6 commit 928af11

File tree

9 files changed

+715
-355
lines changed

9 files changed

+715
-355
lines changed

.github/workflows/pypi-publish.yml

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,24 @@ on:
66
- "v*"
77

88
jobs:
9-
lint:
10-
uses: prosegrinder/.github/.github/workflows/poetry-lint.yaml@main
9+
black:
10+
uses: prosegrinder/.github/.github/workflows/poetry-black.yaml@main
11+
12+
pylint:
13+
uses: prosegrinder/.github/.github/workflows/poetry-pylint.yaml@main
14+
15+
mypy:
16+
uses: prosegrinder/.github/.github/workflows/poetry-mypy.yaml@main
17+
18+
bandit:
19+
uses: prosegrinder/.github/.github/workflows/poetry-bandit.yaml@main
1120

1221
test:
13-
needs: lint
22+
needs:
23+
- pylint
24+
- black
25+
- mypy
26+
- bandit
1427
uses: prosegrinder/.github/.github/workflows/poetry-test.yaml@main
1528

1629
publish:

.github/workflows/python-ci.yml

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
name: Python Poetry CI
22

3+
permissions:
4+
contents: read
5+
36
on:
47
pull_request:
58

@@ -8,11 +11,24 @@ concurrency:
811
cancel-in-progress: true
912

1013
jobs:
11-
lint:
12-
uses: prosegrinder/.github/.github/workflows/poetry-lint.yaml@main
14+
black:
15+
uses: prosegrinder/.github/.github/workflows/poetry-black.yaml@main
16+
17+
pylint:
18+
uses: prosegrinder/.github/.github/workflows/poetry-pylint.yaml@main
19+
20+
mypy:
21+
uses: prosegrinder/.github/.github/workflows/poetry-mypy.yaml@main
22+
23+
bandit:
24+
uses: prosegrinder/.github/.github/workflows/poetry-bandit.yaml@main
1325

1426
test:
15-
needs: lint
27+
needs:
28+
- pylint
29+
- black
30+
- mypy
31+
- bandit
1632
uses: prosegrinder/.github/.github/workflows/poetry-test.yaml@main
1733

1834
cz-dry-run:

.github/workflows/python-release.yml

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
name: Bump Version and Create Release
22

3+
permissions:
4+
contents: write
5+
36
on:
47
push:
58
branches:
@@ -10,9 +13,32 @@ concurrency:
1013
cancel-in-progress: true
1114

1215
jobs:
16+
black:
17+
uses: prosegrinder/.github/.github/workflows/poetry-black.yaml@main
18+
19+
pylint:
20+
uses: prosegrinder/.github/.github/workflows/poetry-pylint.yaml@main
21+
22+
mypy:
23+
uses: prosegrinder/.github/.github/workflows/poetry-mypy.yaml@main
24+
25+
bandit:
26+
uses: prosegrinder/.github/.github/workflows/poetry-bandit.yaml@main
27+
28+
test:
29+
needs:
30+
- pylint
31+
- black
32+
- mypy
33+
- bandit
34+
uses: prosegrinder/.github/.github/workflows/poetry-test.yaml@main
35+
1336
release:
14-
if: "!startsWith(github.event.head_commit.message, 'bump:')"
37+
needs: test
38+
if: ${{ !startsWith(github.event.head_commit.message, 'bump:') }}
1539
# Don't run 'bump:'
40+
permissions:
41+
contents: write
1642
uses: prosegrinder/.github/.github/workflows/poetry-release.yaml@main
1743
secrets:
1844
VERSION_BUMP_TAG_TOKEN: "${{ secrets.VERSION_BUMP_TAG_TOKEN }}"

poetry.lock

Lines changed: 519 additions & 207 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,37 @@
1-
[tool.commitizen]
2-
version = "1.0.33"
3-
tag_format = "v$version"
4-
update_changelog_on_bump = true
5-
changelog_incremental = true
6-
bump_message = "bump: $current_version → $new_version"
7-
version_files = [
8-
"pyproject.toml:version",
1+
[project]
2+
name = "cmudict"
3+
dynamic = [ "version", "classifiers" ]
4+
description = "A versioned python wrapper package for The CMU Pronouncing Dictionary data files."
5+
license = { text = "GPL-3.0-or-later" }
6+
readme = "README.md"
7+
requires-python = ">=3.9,<4.0"
8+
authors = [
9+
{ name = "David L. Day", email = "[email protected]" }
10+
]
11+
keywords = ["cmudict"]
12+
dependencies = [
13+
"importlib-metadata >=5",
14+
"importlib-resources >=5",
915
]
1016

17+
[project.urls]
18+
homepage = "https://github.com/prosegrinder/python-cmudict"
19+
repository = "https://github.com/prosegrinder/python-cmudict"
20+
"Bug Tracker" = "https://github.com/prosegrinder/python-cmudict/issues"
21+
1122
[build-system]
1223
requires = ["poetry-core"]
1324
build-backend = "poetry.core.masonry.api"
1425

26+
[tool.commitizen]
27+
tag_format = "v$version"
28+
update_changelog_on_bump = true
29+
changelog_incremental = true
30+
bump_message = "bump: $current_version → $new_version"
31+
version_provider = "poetry"
32+
1533
[tool.poetry]
16-
name = "cmudict"
1734
version = "1.0.33"
18-
description = "A versioned python wrapper package for The CMU Pronouncing Dictionary data files."
19-
authors = ["David L. Day <[email protected]>"]
20-
license = "GPL-3.0-or-later"
21-
homepage = "https://github.com/prosegrinder/python-cmudict"
22-
repository = "https://github.com/prosegrinder/python-cmudict"
23-
readme = "README.md"
24-
keywords = ["cmudict"]
2535
classifiers = [
2636
"Development Status :: 5 - Production/Stable",
2737
"Intended Audience :: Developers",
@@ -35,17 +45,14 @@ exclude = [
3545
"src/cmudict/data/.git"
3646
]
3747

38-
[tool.poetry.dependencies]
39-
python = "^3.8"
40-
importlib-metadata = ">=5"
41-
importlib-resources = ">=5"
42-
4348
[tool.poetry.group.dev.dependencies]
4449
pytest = ">=6.2.2"
4550
coverage = {extras = ["toml"], version = ">=6.5"}
4651
pytest-cov = ">=4.0"
4752
pylint = ">=3"
4853
black = ">=22.10"
54+
mypy = "^1.17.0"
55+
bandit = "^1.8.6"
4956

5057
[tool.pytest.ini_options]
5158
filterwarnings = [

src/cmudict/__init__.py

Lines changed: 38 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,11 @@
44
files. Compatible with NLTK's CMUDictCorpusReader.
55
"""
66

7+
import atexit
78
import re
8-
import sys
9-
from collections import defaultdict
109
from contextlib import ExitStack
11-
import atexit
12-
13-
if sys.version_info >= (3, 9):
14-
from importlib import metadata, resources
15-
else:
16-
import importlib_metadata as metadata
17-
import importlib_resources as resources
10+
from importlib import metadata, resources
11+
from typing import IO, Dict, List, Optional, Tuple
1812

1913
__version__ = metadata.version(__name__)
2014

@@ -28,17 +22,19 @@
2822
atexit.register(file_manager.close)
2923

3024

31-
def _stream(resource_name):
32-
stream = resources.files(__name__).joinpath(resource_name).open("rb")
25+
def _stream(resource_name: str) -> IO[bytes]:
26+
stream: IO[bytes] = resources.files(__name__).joinpath(resource_name).open("rb")
3327
return stream
3428

3529

36-
def _string(resource_name):
30+
def _string(resource_name: str) -> str:
3731
with resources.files(__name__).joinpath(resource_name).open() as file:
3832
return file.read()
3933

4034

41-
def _entries(stream, comment_string=None):
35+
def _entries(
36+
stream: IO[bytes], comment_string: Optional[str] = None
37+
) -> List[Tuple[str, List[str]]]:
4238
cmudict_entries = []
4339
for line in stream:
4440
parts = []
@@ -52,113 +48,117 @@ def _entries(stream, comment_string=None):
5248

5349

5450
# pylint: disable-next=redefined-builtin
55-
def dict():
51+
def dict() -> Dict[str, List[List[str]]]:
5652
"""
5753
Compatibility with NLTK.
5854
Returns the cmudict lexicon as a dictionary, whose keys are
5955
lowercase words and whose values are lists of pronunciations.
6056
"""
61-
default = defaultdict(list)
57+
default: Dict[str, List[List[str]]] = {}
6258
for key, value in entries():
59+
if key not in default:
60+
default[key] = []
6361
default[key].append(value)
6462
return default
6563

6664

67-
def dict_stream():
65+
def dict_stream() -> IO[bytes]:
6866
"""Return a readable file-like object of the cmudict.dict file."""
69-
stream = _stream(CMUDICT_DICT)
67+
stream: IO[bytes] = _stream(CMUDICT_DICT)
7068
return stream
7169

7270

73-
def dict_string():
71+
def dict_string() -> str:
7472
"""Return the contents of cmudict.dict as a string."""
7573
string = _string(CMUDICT_DICT)
7674
return string
7775

7876

79-
def license_string():
77+
def license_string() -> str:
8078
"""Return the contents of LICENSE as a string."""
8179
string = _string(CMUDICT_LICENSE)
8280
return string
8381

8482

85-
def phones():
83+
def phones() -> List[Tuple[str, List[str]]]:
8684
"""Return a list of phones used in the main dict."""
87-
cmu_phones = []
85+
cmu_phones: List[Tuple[str, List[str]]] = []
8886
for line in phones_stream():
8987
parts = line.decode("utf-8").strip().split()
9088
cmu_phones.append((parts[0], parts[1:]))
9189
return cmu_phones
9290

9391

94-
def phones_stream():
92+
def phones_stream() -> IO[bytes]:
9593
"""Return a readable file-like object of the cmudict.phones file."""
96-
p_stream = _stream(CMUDICT_PHONES)
94+
p_stream: IO[bytes] = _stream(CMUDICT_PHONES)
9795
return p_stream
9896

9997

100-
def phones_string():
98+
def phones_string() -> str:
10199
"""Return the contents of cmudict.phones as a string."""
102100
string = _string(CMUDICT_PHONES)
103101
return string
104102

105103

106-
def symbols():
104+
def symbols() -> List[str]:
107105
"""Return a list of symbols."""
108-
cmu_symbols = []
106+
cmu_symbols: List[str] = []
109107
for line in symbols_stream():
110108
cmu_symbols.append(line.decode("utf-8").strip())
111109
return cmu_symbols
112110

113111

114-
def symbols_stream():
112+
def symbols_stream() -> IO[bytes]:
115113
"""Return a readable file-like object of the cmudict.symbols file."""
116-
stream = _stream(CMUDICT_SYMBOLS)
114+
stream: IO[bytes] = _stream(CMUDICT_SYMBOLS)
117115
return stream
118116

119117

120-
def symbols_string():
118+
def symbols_string() -> str:
121119
"""Return the contents of cmudict.symbols as a string."""
122120
string = _string(CMUDICT_SYMBOLS)
123121
return string
124122

125123

126124
# pylint: disable-next=invalid-name
127-
def vp():
125+
def vp() -> Dict[str, List[List[str]]]:
128126
"""Return a list of punctuation pronounciations."""
129-
cmu_vp = defaultdict(list)
127+
cmu_vp: Dict[str, List[List[str]]] = {}
130128
with vp_stream() as stream:
131129
for key, value in _entries(stream):
130+
if not key in cmu_vp:
131+
cmu_vp[key] = []
132132
cmu_vp[key].append(value)
133133
return cmu_vp
134134

135135

136-
def vp_stream():
136+
def vp_stream() -> IO[bytes]:
137137
"""Return a readable file-like object of the cmudict.vp file."""
138-
stream = _stream(CMUDICT_VP)
138+
stream: IO[bytes] = _stream(CMUDICT_VP)
139139
return stream
140140

141141

142-
def vp_string():
142+
def vp_string() -> str:
143143
"""Return the contents of cmudict.vp as a string."""
144144
string = _string(CMUDICT_VP)
145145
return string
146146

147147

148148
# The .entries(), .raw(), and .words() functions
149149
# maintain compatability with NTLK.
150-
def entries():
150+
def entries() -> List[Tuple[str, List[str]]]:
151151
"""
152152
Compatibility with NLTK.
153153
Returns the cmudict lexicon as a list of entries
154154
containing (word, transcriptions) tuples.
155155
"""
156156
with dict_stream() as stream:
157-
cmu_entries = _entries(stream, "#")
157+
cmu_entries: List[Tuple[str, List[str]]] = _entries(stream, "#")
158158
return cmu_entries
159159

160160

161-
def raw():
161+
def raw() -> str:
162162
"""
163163
Compatibility with NLTK.
164164
Returns the cmudict lexicon as a raw string.
@@ -167,7 +167,7 @@ def raw():
167167
return string
168168

169169

170-
def words():
170+
def words() -> List[str]:
171171
"""
172172
Compatibility with NLTK.
173173
Returns a list of all words defined in the cmudict lexicon.

src/cmudict/py.typed

Whitespace-only changes.

0 commit comments

Comments
 (0)