Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Python Type Hints

Python Type Hints

제 3회 파이썬 격월 세미나에서 발표한 [Python Type Hints] 발표 슬라이드입니다. (https://www.facebook.com/groups/pythonkorea/)

Sunghyun Hwang

March 25, 2017
Tweet

More Decks by Sunghyun Hwang

Other Decks in Programming

Transcript

  1. PEP 484 - Type Hints • PEP 3107 - Function

    Annotations • 2014-09-29 • Python 3.5
  2. def get_list(a: int, b: int) -> list: return [a, b]

    from typing import List def get_list(a: int, b: int) -> List[int]: return [a, b]
  3. def get_list(a: int, b: int) -> list: return [a, b]

    from typing import Any, List def get_list(a: int, b: int) -> List[Any]: return ['a', a, 'b', b, 'a + b', a + b]
  4. class Person: def __init__(self, name: str) -> None: self.name: str

    = name self.length_of_name: int = len(name)
  5. from typing import Dict, Set def get_dict(a: int) -> Dict[int,

    str]: return { a: str(a), a + 1: str(a + 1) } def get_set(a: int) -> Set[int]: return {a, a + 1}
  6. from typing import Tuple def get_tuple(a: int, b: int) ->

    Tuple[int]: return a, b # Expected type 'Tuple[int]', got 'Tuple[int, int]' instead
  7. from typing import Tuple def get_tuple() -> Tuple[int, int, int,

    int, int, int, int, int, int, int]: return 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
  8. from typing import Callable def add_lazy(a: int, b: int) ->

    Callable[[], int]: def f() -> int: return a + b return f
  9. from abc import ABCMeta, abstractmethod from typing import Generic, TypeVar

    T = TypeVar('T') class Person: pass class Mapper(Generic[T], metaclass=ABCMeta): @classmethod @abstractmethod def from_dict(cls, request: dict) -> T: pass def convert(mapper: Mapper[Person], data: dict) -> Person: return mapper.from_dict(data)
  10. from abc import ABCMeta, abstractmethod from typing import Generic, TypeVar

    T = TypeVar('T') class Person: pass class Mapper(Generic[T], metaclass=ABCMeta): @classmethod @abstractmethod def from_dict(cls, request: dict) -> T: pass def convert(mapper: Mapper[Person], data: dict) -> Person: return mapper.from_dict(data)
  11. from abc import ABCMeta, abstractmethod from typing import Generic, TypeVar

    T = TypeVar('T') class Person: pass class Mapper(Generic[T], metaclass=ABCMeta): @classmethod @abstractmethod def from_dict(cls, request: dict) -> T: pass def convert(mapper: Mapper[Person], data: dict) -> Person: return mapper.from_dict(data)
  12. from abc import ABCMeta, abstractmethod from typing import Generic, TypeVar

    T = TypeVar('T') class Person: pass class Mapper(Generic[T], metaclass=ABCMeta): @classmethod @abstractmethod def from_dict(cls, request: dict) -> T: pass def convert(mapper: Mapper[Person], data: dict) -> Person: return mapper.from_dict(data)
  13. from abc import ABCMeta, abstractmethod from typing import Generic, TypeVar

    T = TypeVar('T') class Person: pass class Mapper(Generic[T], metaclass=ABCMeta): @classmethod @abstractmethod def from_dict(cls, request: dict) -> T: pass def convert(mapper: Mapper[Person], data: dict) -> Person: return mapper.from_dict(data)
  14. from abc import ABCMeta, abstractmethod from typing import Generic, TypeVar

    T = TypeVar('T') class Person: pass class Mapper(Generic[T], metaclass=ABCMeta): @classmethod @abstractmethod def from_dict(cls, request: dict) -> T: pass def convert(mapper: Mapper[Person], data: dict) -> Person: return mapper.from_dict(data)
  15. python2 support from typing import List def hello(): # type:

    () -> None print 'hello' class Example: def method(self, lst, opt=0, *args, **kwargs): # type: (List[str], int, *str, **bool) -> int """Docstring comes after type comment."""
  16. python/typeshed • contains external type annotations for the Python standard

    library and Python builtins • as well as third party packages.
  17. :+1: • ޙࢲച੄ ӝמ (ӝઓ੄ docstring convention ؀୓) • IDE/ী٣ఠ

    ఋੑ ୶ۿ ѐࢶ • ੿੸ Type Checkerܳ ాೠ पࣻ ߑ૑
  18. ۨ੉פझ౟ীࢲח • ఋੑ ݺदо ௏٘੄ оةࢿਸ ֫ੋ׮Ҋ ޺ਵݴ ೐۽ં౟ ҙܻ

    ࠺ਊਸ ծ୹ ࣻ ੓׮Ҋ ౸ױೞৈ بੑ೮णפ׮. • नӏ ೐۽ં౟ח CI җ੿ীࢲ mypyܳ प೯೤פ׮.
  19. ۨ੉פझ౟ীࢲח • ఋੑ ݺदо ௏٘੄ оةࢿਸ ֫ੋ׮Ҋ ޺ਵݴ ೐۽ં౟ ҙܻ

    ࠺ਊਸ ծ୹ ࣻ ੓׮Ҋ ౸ױೞৈ بੑ೮णפ׮. • नӏ ೐۽ં౟ח CI җ੿ীࢲ mypyܳ प೯೤פ׮. • ఋੑ दझమ੄ ೠ҅۽ ੋ೧ ࠛ೙ਃೠ ௏٘ܳ ੘ࢿ೧ঠ ೡ ٸ ীח @typing.no_type_check