Sunghyun Hwang
March 25, 2017
1.9k

# Python Type Hints

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

March 25, 2017

## Transcript

Annotations
3. ### PEP 484 - Type Hints • PEP 3107 - Function

Annotations • 2014-09-29
4. ### PEP 484 - Type Hints • PEP 3107 - Function

Annotations • 2014-09-29 • Python 3.5

b

8. ### 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]
9. ### def get_list(a: int, b: int) -> List: return ['a', a,

'b', b, 'a + b', a + b]
10. ### def get_list(a: int, b: int) -> List: return ['a', a,

'b', b, 'a + b', a + b]
11. ### 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]

str(result)
13. ### class Person: def __init__(self, name: str) -> None: self.name: str

= name self.length_of_name: int = len(name)

'a', 2, 'b']

16. ### 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}
17. ### 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
18. ### from typing import Tuple def get_tuple(a: int, b: int) ->

Tuple[int, int]: return a, b
19. ### 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
20. ### from typing import Tuple def get_tuple() -> Tuple[int, ...]: return

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

f() -> int: return a + b return f
22. ### from typing import Callable def add_lazy(a: int, b: int) ->

Callable[[], int]: def f() -> int: return a + b return f
23. ### from typing import Callable def run(f: Callable[[int, str], int], a:

int, b: str) -> int: return f(a, b)

26. ### def run( f: Callable[[Dict[str, int]], int], **kwargs: int ) ->

int: return f(kwargs)
27. ### def run( f: Callable[[Dict[str, int]], int], **kwargs: int ) ->

int: return f(kwargs)
28. ### def run( f: Callable[[Dict[str, int]], int], **kwargs: int ) ->

int: return f(kwargs)
29. ### IntReturningFunction = Callable[..., int] def run(f: IntReturningFunction) -> int: return

f(1, 2, 3, 4, 5)

pass

33. ### class Person: def get_children(self) -> List[Person]: pass NameError: name 'Person'

is not defined
34. ### class Person: def get_children(self) -> List[Person]: pass def get_children(self) ->

List['Person']: pass
35. ### 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)
36. ### 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)
37. ### 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)
38. ### 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)
39. ### 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)
40. ### 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)
41. ### 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."""

+ b

44. ### python/typeshed • contains external type annotations for the Python standard

library and Python builtins
45. ### python/typeshed • contains external type annotations for the Python standard

library and Python builtins • as well as third party packages.

ఋੑ ୶ۿ ѐࢶ
49. ### :+1: • ޙࢲച੄ ӝמ (ӝઓ੄ docstring convention ؀୓) • IDE/ী٣ఠ

ఋੑ ୶ۿ ѐࢶ • ੿੸ Type Checkerܳ ాೠ पࣻ ߑ૑

ࠗ઒ೠ ఋੑ दझమ
52. ### “Python is dynamically typed and we like it that way!”

- Guido van Rossum
53. ### ۨ੉פझ౟ীࢲח • ఋੑ ݺदо ௏٘੄ оةࢿਸ ֫ੋ׮Ҋ ޺ਵݴ ೐۽ં౟ ҙܻ

࠺ਊਸ ծ୹ ࣻ ੓׮Ҋ ౸ױೞৈ بੑ೮णפ׮.
54. ### ۨ੉פझ౟ীࢲח • ఋੑ ݺदо ௏٘੄ оةࢿਸ ֫ੋ׮Ҋ ޺ਵݴ ೐۽ં౟ ҙܻ

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

࠺ਊਸ ծ୹ ࣻ ੓׮Ҋ ౸ױೞৈ بੑ೮णפ׮. • नӏ ೐۽ં౟ח CI җ੿ীࢲ mypyܳ प೯೤פ׮. • ఋੑ दझమ੄ ೠ҅۽ ੋ೧ ࠛ೙ਃೠ ௏٘ܳ ੘ࢿ೧ঠ ೡ ٸ ীח @typing.no_type_check
56. ### ߛ௼࢟۞٘ܳ ݅٘ח ۨ੉פझ౟ীࢲ ੷൞ ৬ ೣԋೡ ࢲߡ ূ૑פয ٜ࠙ਸ ଺Ҋ

੓ णפ׮. Python, aiohttp, django