Skip to content

Commit

Permalink
Merge pull request mozillazg#110 from mozillazg/typing
Browse files Browse the repository at this point in the history
增加类型注解
  • Loading branch information
mozillazg authored Dec 4, 2017
2 parents ff043cb + 86a34f3 commit 05f2cda
Show file tree
Hide file tree
Showing 34 changed files with 470 additions and 4 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ install:

script:
- pre-commit run --all-files
- mypy pypinyin
- tox -e $TOX_ENV
- python setup.py install
- pypinyin hello
Expand Down
5 changes: 5 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
Changelog
---------

0.28.0 (2017-mm-dd)
+++++++++++++++++++++

* **[New]** 增加类型注解。


0.27.0 (2017-10-28)
+++++++++++++++++++++
Expand Down
1 change: 1 addition & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
include README.rst LICENSE.txt CHANGELOG.rst
recursive-include pypinyin *.pyi
5 changes: 5 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ help:
@echo "gen_data gen pinyin data"
@echo "gen_pinyin_dict gen single hanzi pinyin dict"
@echo "gen_phrases_dict gen phrase hanzi pinyin dict"
@echo "lint run lint"

.PHONY: test
test:
Expand Down Expand Up @@ -43,3 +44,7 @@ gen_pinyin_dict:
.PHONY: gen_phrases_dict
gen_phrases_dict:
python gen_phrases_dict.py phrase-pinyin-data/pinyin.txt pypinyin/phrases_dict.py

.PHONY: lint
lint:
mypy pypinyin
50 changes: 50 additions & 0 deletions pypinyin/__init__.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
from typing import Any

from . import compat
from . import constants
from . import core

__title__ = ... # type: Any
__version__ = ... # type: Any
__author__ = ... # type: Any
__license__ = ... # type: Any
__copyright__ = ... # type: Any
__all__ = ... # type: Any

PY2 = compat.PY2

Style = constants.Style
STYLE_NORMAL = constants.STYLE_NORMAL
NORMAL = constants.NORMAL
STYLE_TONE = constants.STYLE_TONE
TONE = constants.TONE
STYLE_TONE2 = constants.STYLE_TONE2
TONE2 = constants.TONE2
STYLE_TONE3 = constants.STYLE_TONE3
TONE3 = constants.TONE3
STYLE_INITIALS = constants.STYLE_INITIALS
INITIALS = constants.INITIALS
STYLE_FIRST_LETTER = constants.STYLE_FIRST_LETTER
FIRST_LETTER = constants.FIRST_LETTER
STYLE_FINALS = constants.STYLE_FINALS
FINALS = constants.FINALS
STYLE_FINALS_TONE = constants.STYLE_FINALS_TONE
FINALS_TONE = constants.FINALS_TONE
STYLE_FINALS_TONE2 = constants.STYLE_FINALS_TONE2
FINALS_TONE2 = constants.FINALS_TONE2
STYLE_FINALS_TONE3 = constants.STYLE_FINALS_TONE3
FINALS_TONE3 = constants.FINALS_TONE3
STYLE_BOPOMOFO = constants.STYLE_BOPOMOFO
BOPOMOFO = constants.BOPOMOFO
STYLE_BOPOMOFO_FIRST = constants.STYLE_BOPOMOFO_FIRST
BOPOMOFO_FIRST = constants.BOPOMOFO_FIRST
STYLE_CYRILLIC = constants.STYLE_CYRILLIC
CYRILLIC = constants.CYRILLIC
STYLE_CYRILLIC_FIRST = constants.STYLE_CYRILLIC_FIRST
CYRILLIC_FIRST = constants.CYRILLIC_FIRST

pinyin = core.pinyin
lazy_pinyin = core.lazy_pinyin
slug = core.slug
load_single_dict = core.load_single_dict
load_phrases_dict = core.load_phrases_dict
4 changes: 1 addition & 3 deletions pypinyin/__main__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys

from pypinyin.runner import main

if __name__ == '__main__':
sys.exit(main())
main()
72 changes: 72 additions & 0 deletions pypinyin/constants.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
from enum import IntEnum, unique
from typing import Dict, List, Any, Text

PHRASES_DICT = ... # type: Dict[Text, List[List[Text]]]

PINYIN_DICT = ... # type: Dict[int, Text]

RE_TONE2 = ... # type: Any

RE_HANS = ... # type: Any


@unique
class Style(IntEnum):

NORMAL = ...

TONE = ...

TONE2 = ...

TONE3 = ...

INITIALS = ...

FIRST_LETTER = ...

FINALS = ...

FINALS_TONE = ...

FINALS_TONE2 = ...

FINALS_TONE3 = ...

BOPOMOFO = ...

BOPOMOFO_FIRST = ...

CYRILLIC = ...

CYRILLIC_FIRST = ...


NORMAL = ... # type: Style
STYLE_NORMAL = ... # type: Style
TONE = ... # type: Style
STYLE_TONE = ... # type: Style
TONE2 = ... # type: Style
STYLE_TONE2 = ... # type: Style
TONE3 = ... # type: Style
STYLE_TONE3 = ... # type: Style
INITIALS = ... # type: Style
STYLE_INITIALS = ... # type: Style
FIRST_LETTER = ... # type: Style
STYLE_FIRST_LETTER = ... # type: Style
FINALS = ... # type: Style
STYLE_FINALS = ... # type: Style
FINALS_TONE = ... # type: Style
STYLE_FINALS_TONE = ... # type: Style
FINALS_TONE2 = ... # type: Style
STYLE_FINALS_TONE2 = ... # type: Style
FINALS_TONE3 = ... # type: Style
STYLE_FINALS_TONE3 = ... # type: Style
BOPOMOFO = ... # type: Style
STYLE_BOPOMOFO = ... # type: Style
BOPOMOFO_FIRST = ... # type: Style
STYLE_BOPOMOFO_FIRST = ... # type: Style
CYRILLIC = ... # type: Style
STYLE_CYRILLIC = ... # type: Style
CYRILLIC_FIRST = ... # type: Style
STYLE_CYRILLIC_FIRST = ... # type: Style
29 changes: 29 additions & 0 deletions pypinyin/contrib/mmseg.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
from typing import Iterator
from typing import Text


class Seg(object):
"""最大正向匹配分词
:type prefix_set: PrefixSet
"""
def __init__(self, prefix_set: PrefixSet) -> None: ...

def cut(self, text: Text) -> Iterator[Text]: ...

def train(self, words: Iterator[Text]) -> None: ...


class PrefixSet(object):
def __init__(self): ...

def train(self, word_s: Iterator[Text]) -> None: ...

def __contains__(self, key: Text) -> bool: ...


p_set = ... # type: PrefixSet
seg = ... # type: Seg


def retrain(seg_instance: Seg) -> None: ...
83 changes: 83 additions & 0 deletions pypinyin/core.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
from typing import List
from typing import Dict
from typing import Union
from typing import Callable
from typing import Optional
from typing import Text

from pypinyin.constants import Style


TStyle = Style
TErrors = Union[Callable[[Text], Text], Text]
TPinyinResult = List[List[Text]]


def seg(hans: Text) -> List[Text]: ...


def load_single_dict(pinyin_dict: Dict[int, Text],
style: str = ...) -> None: ...


def load_phrases_dict(phrases_dict: Dict[Text, List[List[Text]]],
style: str = ...
) -> None: ...


def to_fixed(pinyin: Text, style: TStyle,
strict: bool = ...) -> Text: ...


def _handle_nopinyin_char(chars: Text, errors: TErrors = ...
) -> Optional[Text]: ...


def handle_nopinyin(chars: Text, errors: TErrors = ...
) -> List[Text]: ...


def single_pinyin(han: Text, style: TStyle, heteronym: bool,
errors: TErrors = ...,
strict: bool = ...
) -> List[Text]: ...


def phrase_pinyin(phrase: Text,
style: TStyle,
heteronym: bool,
errors: TErrors = ...,
strict: bool = ...
) -> List[List[Text]]: ...


def _pinyin(words: Text,
style: TStyle,
heteronym: bool,
errors: TErrors,
strict: bool = ...
) -> List[List[Text]]:...


def pinyin(hans: Union[List[Text], Text],
style: TStyle = ...,
heteronym: bool = ...,
errors: TErrors = ...,
strict: bool = ...
) -> List[List[Text]]: ...


def slug(hans: Union[List[Text], Text],
style: TStyle = ...,
heteronym: bool = ...,
separator: Text = ...,
errors: TErrors = ...,
strict: bool = ...
) -> Text: ...


def lazy_pinyin(hans: Union[List[Text], Text],
style: TStyle = ...,
errors: TErrors = ...,
strict: bool = ...
) -> List[Text]: ...
5 changes: 5 additions & 0 deletions pypinyin/phonetic_symbol.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from typing import Dict, Text


phonetic_symbol = ... # type: Dict[Text, Text]
phonetic_symbol_reverse = ... # type: Dict[Text, Text]
3 changes: 3 additions & 0 deletions pypinyin/phrases_dict.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from typing import Dict, List, Text

phrases_dict = ... # type: Dict[Text, List[List[Text]]]
3 changes: 3 additions & 0 deletions pypinyin/pinyin_dict.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from typing import Dict, Text

pinyin_dict = ... # type: Dict[int, Text]
2 changes: 1 addition & 1 deletion pypinyin/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from __future__ import unicode_literals
from argparse import ArgumentParser
import logging
import sys

import pypinyin
Expand Down Expand Up @@ -81,7 +82,6 @@ def get_parser():


def main():
import logging
# 禁用除 CRITICAL 外的日志消息
logging.disable(logging.CRITICAL)

Expand Down
12 changes: 12 additions & 0 deletions pypinyin/runner.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from argparse import ArgumentParser
from typing import Union, Text, ByteString


class NullWriter(object):
def write(self, string: Union[Text, ByteString]) -> None: ...


def get_parser() -> ArgumentParser: ...


def main() -> None: ...
18 changes: 18 additions & 0 deletions pypinyin/standard.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from typing import Text

def convert_zero_consonant(pinyin: Text) -> Text: ...


def convert_uv(pinyin: Text) -> Text: ...


def convert_iou(pinyin: Text) -> Text: ...


def convert_uei(pinyin: Text) -> Text: ...


def convert_uen(pinyin: Text) -> Text: ...


def convert_finals(pinyin: Text) -> Text: ...
20 changes: 20 additions & 0 deletions pypinyin/style/__init__.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# -*- coding: utf-8 -*-
from typing import Any, Optional, Callable, Dict, Text, Union

from pypinyin.constants import Style

TStyle = Style
TRegisterFunc = Optional[Callable[[Text, Dict[Any, Any]], Optional[Text]]]
TWrapperFunc = Optional[Callable[[Text, Dict[Any, Any]], Optional[Text]]]

_registry = {} # type: Dict[Union[TStyle, int, str, Any], TRegisterFunc]


def convert(pinyin: Text, style: TStyle, strict: bool,
default: Optional[Text] = ..., **kwargs: Any) -> Any: ...


def register(style: Union[TStyle, int, str, Any],
func: TRegisterFunc = ...) -> TWrapperFunc: ...

def auto_discover() -> None: ...
19 changes: 19 additions & 0 deletions pypinyin/style/_constants.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from typing import Any, List, Dict, Text


_INITIALS = ... # type: List[Text]

_INITIALS_NOT_STRICT = ... # type: List[Text]


PHONETIC_SYMBOL_DICT = ... # type: Dict[Text, Text]

RE_PHONETIC_SYMBOL = ... # type : Any


RE_TONE2 = ... # type : Any

RE_TONE3 = ... # type : Any


RE_NUMBER = ... # type: Any
Loading

0 comments on commit 05f2cda

Please sign in to comment.