Skip to content
This repository was archived by the owner on Nov 13, 2024. It is now read-only.

Commit 07fcfc3

Browse files
committed
add mypy type annotations
1 parent 6eb5edd commit 07fcfc3

File tree

7 files changed

+28
-13
lines changed

7 files changed

+28
-13
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
*.egg-info
22
__pycache__
33
.pytest_cache
4+
.mypy_cache
45
.vscode
56
build
67
dist

MANIFEST.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
include README.md
22
include LICENSE
3+
include languagecodes/py.typed

languagecodes/__init__.py

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,37 @@
1+
from typing import Iterable, Optional, Set
12
from banal import ensure_list
23

34
from languagecodes.iso639 import ISO3_ALL, ISO2_MAP, ISO3_MAP
45
from languagecodes.synonyms import expand_synonyms
56
from languagecodes.util import normalize_code
67

78

8-
def iso_639_alpha3(code):
9+
def iso_639_alpha3(code: str) -> Optional[str]:
910
"""Convert a given language identifier into an ISO 639 Part 2 code, such
1011
as "eng" or "deu". This will accept language codes in the two- or three-
1112
letter format, and some language names. If the given string cannot be
1213
converted, ``None`` will be returned.
1314
"""
14-
code = normalize_code(code)
15-
code = ISO3_MAP.get(code, code)
16-
if code in ISO3_ALL:
17-
return code
15+
norm = normalize_code(code)
16+
if norm is not None:
17+
norm = ISO3_MAP.get(norm, norm)
18+
if norm not in ISO3_ALL:
19+
return None
20+
return norm
1821

1922

20-
def iso_639_alpha2(code):
23+
def iso_639_alpha2(code: str) -> Optional[str]:
2124
"""Convert a language identifier to an ISO 639 Part 1 code, such as "en"
2225
or "de". For languages which do not have a two-letter identifier, or
2326
invalid language codes, ``None`` will be returned.
2427
"""
25-
code = iso_639_alpha3(code)
26-
return ISO2_MAP.get(code)
28+
alpha3 = iso_639_alpha3(code)
29+
if alpha3 is None:
30+
return None
31+
return ISO2_MAP.get(alpha3)
2732

2833

29-
def list_to_alpha3(languages, synonyms=True):
34+
def list_to_alpha3(languages: Iterable[str], synonyms: bool = True) -> Set[str]:
3035
"""Parse all the language codes in a given list into ISO 639 Part 2 codes
3136
and optionally expand them with synonyms (i.e. other names for the same
3237
language)."""

languagecodes/py.typed

Whitespace-only changes.

languagecodes/synonyms.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
# This is a set of synonyms for pragmatic usage in NLP. It is based on
22
# working with Tesseract 3.04, but should be applicable elsewhere.
33

4+
from typing import Iterable
5+
6+
47
LANG_SYNONYMS = [
58
("srp", "hbs", "hrv", "bos"),
69
("sli", "alb"),
@@ -30,7 +33,7 @@
3033
]
3134

3235

33-
def expand_synonyms(language):
36+
def expand_synonyms(language: str) -> Iterable[str]:
3437
"""Expand a language code into a set of codes."""
3538
for synonyms in LANG_SYNONYMS:
3639
if language in synonyms:

languagecodes/util.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
def normalize_code(code):
1+
from typing import Optional
2+
3+
4+
def normalize_code(code: Optional[str]) -> Optional[str]:
25
if code is None:
36
return None
47
code = str(code)

setup.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from setuptools import setup, find_packages
1+
from setuptools import setup
22

33
setup(
44
name="languagecodes",
@@ -19,16 +19,18 @@
1919
author_email="[email protected]",
2020
url="http://github.com/alephdata/languagecodes",
2121
license="MIT",
22-
packages=find_packages(exclude=["ez_setup", "examples", "test"]),
22+
packages=["languagecodes"],
2323
namespace_packages=[],
2424
include_package_data=True,
25+
package_data={"languagecodes": ["py.typed"]},
2526
zip_safe=False,
2627
test_suite="nose.collector",
2728
install_requires=[],
2829
extras_require={
2930
"dev": [
3031
"wheel>=0.29.0",
3132
"twine",
33+
"mypy",
3234
"flake8>=2.6.0",
3335
"nose",
3436
"banal",

0 commit comments

Comments
 (0)