Skip to content

Commit

Permalink
chore: Use typing library, typing as t (#276)
Browse files Browse the repository at this point in the history
  • Loading branch information
tony committed Jun 11, 2023
2 parents f84ed88 + e02edc7 commit c728d2c
Show file tree
Hide file tree
Showing 7 changed files with 135 additions and 117 deletions.
16 changes: 14 additions & 2 deletions CHANGES
Expand Up @@ -19,13 +19,23 @@ $ pipx install --suffix=@next unihan-etl --pip-args '\--pre' --force

<!-- Maintainers, insert changes / features for the next release here -->

### Breaking changes

- **Python 3.7 Dropped**

Python 3.7 support has been dropped (#272)

Its end-of-life is June 27th, 2023 and Python 3.8 will add support for
`typing.TypedDict` and `typing.Protocol` out of the box without needing
`typing_extensions`.
{mod}`typing`'s {class}`typing.TypedDict` and {class}`typing.Protocol` out of the box without needing
{mod}`typing_extensions`'s.

### Internal improvement

- Typings:

- Import {mod}`typing` as a namespace, e.g. `import typing as t` (#276)
- Use `typing` for {class}`typing.TypedDict` and {class}`typing.Literal` (#276)
- Use typing_extensions' {py:data}`TypeAlias` for repeated types, such in test_expansions (#276)

## unihan-etl 0.19.1 (2023-05-28)

Expand All @@ -49,9 +59,11 @@ _Maintenance only, no bug fixes or features_
formatting can be done almost instantly.

This change replaces black, isort, flake8 and flake8 plugins.

- poetry: 1.4.0 -> 1.5.0

See also: https://github.com/python-poetry/poetry/releases/tag/1.5.0

- pytest: Fix invalid escape sequence warning from `zhon`

[ruff]: https://ruff.rs
Expand Down
4 changes: 3 additions & 1 deletion docs/conf.py
Expand Up @@ -166,7 +166,9 @@
]

intersphinx_mapping = {
"python": ("https://docs.python.org/", None),
"python": ("https://docs.python.org/3", None),
"typing_extensions": ("https://typing-extensions.readthedocs.io/en/latest/", None),
"mypy": ("https://mypy.readthedocs.io/en/stable/", None),
"sqlalchemy": ("https://sqlalchemy.readthedocs.org/en/latest/", None),
}

Expand Down
78 changes: 38 additions & 40 deletions src/unihan_etl/expansion.py
Expand Up @@ -12,11 +12,9 @@
"""
import re
import typing as t
from typing import Sequence

import zhon.hanzi
import zhon.pinyin
from typing_extensions import TypedDict

from unihan_etl.constants import SPACE_DELIMITED_FIELDS

Expand All @@ -28,7 +26,7 @@ def expand_kDefinition(value: str) -> t.List[str]:
return [c.strip() for c in value.split(";")]


kMandarinDict = TypedDict(
kMandarinDict = t.TypedDict(
"kMandarinDict",
{"zh-Hans": str, "zh-Hant": str},
)
Expand All @@ -43,7 +41,7 @@ def expand_kMandarin(value: t.List[str]) -> kMandarinDict:
return kMandarinDict({"zh-Hans": cn, "zh-Hant": tw})


kTotalStrokesDict = TypedDict(
kTotalStrokesDict = t.TypedDict(
"kTotalStrokesDict",
{"zh-Hans": int, "zh-Hant": int},
)
Expand All @@ -58,7 +56,7 @@ def expand_kTotalStrokes(value: t.List[str]) -> kTotalStrokesDict:
return kTotalStrokesDict({"zh-Hans": int(cn), "zh-Hant": int(tw)})


class kLocationDict(TypedDict):
class kLocationDict(t.TypedDict):
volume: int
page: int
character: int
Expand All @@ -75,7 +73,7 @@ def expand_kHanYu(value: t.List[str]) -> t.List[kLocationDict]:
""",
re.X,
)
expanded: Sequence[t.Union[str, kLocationDict]] = value.copy()
expanded: t.Sequence[t.Union[str, kLocationDict]] = value.copy()
assert isinstance(expanded, list)

for i, v in enumerate(value):
Expand Down Expand Up @@ -105,7 +103,7 @@ def expand_kIRGHanyuDaZidian(value: t.List[str]) -> t.List[kLocationDict]:
re.X,
)

expanded: Sequence[t.Union[str, kLocationDict]] = value.copy()
expanded: t.Sequence[t.Union[str, kLocationDict]] = value.copy()
assert isinstance(expanded, list)

for i, v in enumerate(value):
Expand All @@ -124,12 +122,12 @@ def expand_kIRGHanyuDaZidian(value: t.List[str]) -> t.List[kLocationDict]:
return expanded


class kHanyuPinyinPreDict(TypedDict):
locations: Sequence[t.Union[str, kLocationDict]]
class kHanyuPinyinPreDict(t.TypedDict):
locations: t.Sequence[t.Union[str, kLocationDict]]
readings: t.List[str]


class kHanyuPinyinDict(TypedDict):
class kHanyuPinyinDict(t.TypedDict):
locations: kLocationDict
readings: t.List[str]

Expand All @@ -147,7 +145,7 @@ def expand_kHanyuPinyin(
re.X,
)

expanded: Sequence[t.Union[str, kHanyuPinyinDict]] = value.copy()
expanded: t.Sequence[t.Union[str, kHanyuPinyinDict]] = value.copy()
assert isinstance(expanded, list)

for i, val in enumerate(value):
Expand All @@ -172,19 +170,19 @@ def expand_kHanyuPinyin(
return expanded


class kXHC1983LocationDict(TypedDict):
class kXHC1983LocationDict(t.TypedDict):
page: int
character: int
entry: t.Optional[int]
substituted: bool


class kXHC1983Dict(TypedDict):
class kXHC1983Dict(t.TypedDict):
locations: kXHC1983LocationDict
reading: str


class kXHC1983PreDict(TypedDict):
class kXHC1983PreDict(t.TypedDict):
locations: t.Union[t.List[str], kXHC1983LocationDict]
reading: str

Expand All @@ -202,7 +200,7 @@ def expand_kXHC1983(
re.X,
)

expanded: Sequence[t.Union[str, kXHC1983Dict]] = value.copy()
expanded: t.Sequence[t.Union[str, kXHC1983Dict]] = value.copy()
assert isinstance(expanded, list)

for i, v in enumerate(value):
Expand All @@ -228,7 +226,7 @@ def expand_kXHC1983(
return expanded


class kCheungBauerDict(TypedDict):
class kCheungBauerDict(t.TypedDict):
radical: int
strokes: int
cangjie: t.Optional[str]
Expand All @@ -247,7 +245,7 @@ def expand_kCheungBauer(
re.X,
)

expanded: Sequence[t.Union[str, kCheungBauerDict]] = value.copy()
expanded: t.Sequence[t.Union[str, kCheungBauerDict]] = value.copy()
assert isinstance(expanded, list)

for i, v in enumerate(value):
Expand All @@ -266,7 +264,7 @@ def expand_kCheungBauer(
return expanded


kRSAdobe_Japan1_6Dict = TypedDict(
kRSAdobe_Japan1_6Dict = t.TypedDict(
"kRSAdobe_Japan1_6Dict",
{"type": str, "cid": int, "radical": int, "strokes": int, "strokes-residue": int},
)
Expand All @@ -283,7 +281,7 @@ def expand_kRSAdobe_Japan1_6(value: t.List[str]) -> t.List[kRSAdobe_Japan1_6Dict
""",
re.X,
)
expanded: Sequence[t.Union[str, kRSAdobe_Japan1_6Dict]] = value.copy()
expanded: t.Sequence[t.Union[str, kRSAdobe_Japan1_6Dict]] = value.copy()
assert isinstance(expanded, list)

for i, v in enumerate(value):
Expand All @@ -305,7 +303,7 @@ def expand_kRSAdobe_Japan1_6(value: t.List[str]) -> t.List[kRSAdobe_Japan1_6Dict
return expanded


class kCihaiTDict(TypedDict):
class kCihaiTDict(t.TypedDict):
page: int
row: int
character: int
Expand All @@ -320,7 +318,7 @@ def expand_kCihaiT(value: t.List[str]) -> t.List[kCihaiTDict]:
""",
re.X,
)
expanded: Sequence[t.Union[str, kCihaiTDict]] = value.copy()
expanded: t.Sequence[t.Union[str, kCihaiTDict]] = value.copy()
assert isinstance(expanded, list)

for i, v in enumerate(value):
Expand All @@ -340,23 +338,23 @@ def expand_kCihaiT(value: t.List[str]) -> t.List[kCihaiTDict]:
return expanded


class kIICoreDict(TypedDict):
class kIICoreDict(t.TypedDict):
priority: str
sources: t.List[str]


def expand_kIICore(
value: t.List[str],
) -> t.List[kIICoreDict]:
expanded: Sequence[t.Union[str, kIICoreDict]] = value.copy()
expanded: t.Sequence[t.Union[str, kIICoreDict]] = value.copy()
assert isinstance(expanded, list)

for i, v in enumerate(value):
expanded[i] = kIICoreDict(priority=v[0], sources=list(v[1:]))
return expanded


class kDaeJaweonDict(TypedDict):
class kDaeJaweonDict(t.TypedDict):
page: int
character: int
virtual: int
Expand Down Expand Up @@ -385,7 +383,7 @@ def expand_kDaeJaweon(value: str) -> kDaeJaweonDict:


def expand_kIRGKangXi(value: t.List[str]) -> t.List[kDaeJaweonDict]:
expanded: Sequence[t.Union[str, kDaeJaweonDict]] = value.copy()
expanded: t.Sequence[t.Union[str, kDaeJaweonDict]] = value.copy()
assert isinstance(expanded, list)

for i, v in enumerate(value):
Expand All @@ -394,15 +392,15 @@ def expand_kIRGKangXi(value: t.List[str]) -> t.List[kDaeJaweonDict]:


def expand_kIRGDaeJaweon(value: t.List[str]) -> t.List[kDaeJaweonDict]:
expanded: Sequence[t.Union[str, kDaeJaweonDict]] = value.copy()
expanded: t.Sequence[t.Union[str, kDaeJaweonDict]] = value.copy()
assert isinstance(expanded, list)

for i, v in enumerate(value):
expanded[i] = expand_kDaeJaweon(v)
return expanded


class kFennDict(TypedDict):
class kFennDict(t.TypedDict):
phonetic: str
frequency: str

Expand All @@ -415,7 +413,7 @@ def expand_kFenn(value: t.List[str]) -> t.List[kFennDict]:
""",
re.X,
)
expanded: Sequence[t.Union[str, kFennDict]] = value.copy()
expanded: t.Sequence[t.Union[str, kFennDict]] = value.copy()
assert isinstance(expanded, list)

for i, v in enumerate(value):
Expand All @@ -430,7 +428,7 @@ def expand_kFenn(value: t.List[str]) -> t.List[kFennDict]:
return expanded


class kHanyuPinluDict(TypedDict):
class kHanyuPinluDict(t.TypedDict):
phonetic: str
frequency: int

Expand All @@ -445,7 +443,7 @@ def expand_kHanyuPinlu(value: t.List[str]) -> t.List[kHanyuPinluDict]:
),
re.X,
)
expanded: Sequence[t.Union[str, kHanyuPinluDict]] = value.copy()
expanded: t.Sequence[t.Union[str, kHanyuPinluDict]] = value.copy()
assert isinstance(expanded, list)

for i, v in enumerate(value):
Expand All @@ -460,14 +458,14 @@ def expand_kHanyuPinlu(value: t.List[str]) -> t.List[kHanyuPinluDict]:
return expanded


class LocationDict(TypedDict):
class LocationDict(t.TypedDict):
volume: int
page: int
character: int
virtual: int


class kHDZRadBreakDict(TypedDict):
class kHDZRadBreakDict(t.TypedDict):
radical: str
ucn: str
location: LocationDict
Expand Down Expand Up @@ -515,13 +513,13 @@ def expand_kHDZRadBreak(value: str) -> kHDZRadBreakDict:
return kHDZRadBreakDict(radical=g["radical"], ucn=g["ucn"], location=location)


class kSBGYDict(TypedDict):
class kSBGYDict(t.TypedDict):
page: int
character: int


def expand_kSBGY(value: t.List[str]) -> t.List[kSBGYDict]:
expanded: Sequence[t.Union[str, kSBGYDict]] = value.copy()
expanded: t.Sequence[t.Union[str, kSBGYDict]] = value.copy()
assert isinstance(expanded, list)

for i, v in enumerate(value):
Expand All @@ -530,7 +528,7 @@ def expand_kSBGY(value: t.List[str]) -> t.List[kSBGYDict]:
return expanded


class kRSGenericDict(TypedDict):
class kRSGenericDict(t.TypedDict):
radical: int
strokes: int
simplified: bool
Expand Down Expand Up @@ -568,7 +566,7 @@ def _expand_kRSGeneric(value: t.List[str]) -> t.List[kRSGenericDict]:
expand_kRSKorean = _expand_kRSGeneric


class SourceLocationDict(TypedDict):
class SourceLocationDict(t.TypedDict):
source: str
location: t.Optional[str]

Expand All @@ -589,7 +587,7 @@ def _expand_kIRG_GenericSource(value: str) -> SourceLocationDict:
expand_kIRG_VSource = _expand_kIRG_GenericSource


class kGSRDict(TypedDict):
class kGSRDict(t.TypedDict):
set: int
letter: str
apostrophe: bool
Expand All @@ -605,7 +603,7 @@ def expand_kGSR(value: t.List[str]) -> t.List[kGSRDict]:
re.X,
)

expanded: Sequence[t.Union[str, kGSRDict]] = value.copy()
expanded: t.Sequence[t.Union[str, kGSRDict]] = value.copy()
assert isinstance(expanded, list)

for i, v in enumerate(value):
Expand All @@ -624,15 +622,15 @@ def expand_kGSR(value: t.List[str]) -> t.List[kGSRDict]:
return expanded


class kCheungBauerIndexDict(TypedDict):
class kCheungBauerIndexDict(t.TypedDict):
page: int
character: int


def expand_kCheungBauerIndex(
value: t.List[str],
) -> t.List[t.Union[str, kCheungBauerIndexDict]]:
expanded: Sequence[t.Union[str, kCheungBauerIndexDict]] = value.copy()
expanded: t.Sequence[t.Union[str, kCheungBauerIndexDict]] = value.copy()
assert isinstance(expanded, list)

for i, v in enumerate(value):
Expand Down

0 comments on commit c728d2c

Please sign in to comment.