Slide 1

Slide 1 text

Python Type Hints presenter: str = "sunghyunzz"

Slide 2

Slide 2 text

PEP 484 - Type Hints • PEP 3107 - Function Annotations

Slide 3

Slide 3 text

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

Slide 4

Slide 4 text

PEP 484 - Type Hints • PEP 3107 - Function Annotations • 2014-09-29 • Python 3.5

Slide 5

Slide 5 text

def add(a: int, b: int) -> int: return a + b

Slide 6

Slide 6 text

def print_sum(a: int, b: int) -> None: print(a + b)

Slide 7

Slide 7 text

def get_list(a: int, b: int) -> list: return [a, b]

Slide 8

Slide 8 text

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]

Slide 9

Slide 9 text

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

Slide 10

Slide 10 text

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

Slide 11

Slide 11 text

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]

Slide 12

Slide 12 text

def func(text: str) -> str: result: int = some_complex_func(text) return str(result)

Slide 13

Slide 13 text

class Person: def __init__(self, name: str) -> None: self.name: str = name self.length_of_name: int = len(name)

Slide 14

Slide 14 text

from typing import List, Union result: List[Union[int, str]] = [1, 'a', 2, 'b']

Slide 15

Slide 15 text

from typing import Optional, Union assert Optional[int] == Union[int, None]

Slide 16

Slide 16 text

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}

Slide 17

Slide 17 text

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

Slide 18

Slide 18 text

from typing import Tuple def get_tuple(a: int, b: int) -> Tuple[int, int]: return a, b

Slide 19

Slide 19 text

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

Slide 20

Slide 20 text

from typing import Tuple def get_tuple() -> Tuple[int, ...]: return 1, 2, 3, 4, 5, 6, 7, 8, 9, 10

Slide 21

Slide 21 text

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

Slide 22

Slide 22 text

from typing import Callable def add_lazy(a: int, b: int) -> Callable[[], int]: def f() -> int: return a + b return f

Slide 23

Slide 23 text

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

Slide 24

Slide 24 text

def run(f: Callable[..., int], *args) -> int: return f(args)

Slide 25

Slide 25 text

def run(f: Callable[..., int], *args: int) -> int: return f(args)

Slide 26

Slide 26 text

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

Slide 27

Slide 27 text

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

Slide 28

Slide 28 text

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

Slide 29

Slide 29 text

IntReturningFunction = Callable[..., int] def run(f: IntReturningFunction) -> int: return f(1, 2, 3, 4, 5)

Slide 30

Slide 30 text

def fetch(_id: int, category: int) -> Transaction: pass

Slide 31

Slide 31 text

EntityID = int def fetch(_id: EntityID, category: int) -> Transaction: pass

Slide 32

Slide 32 text

class Person: def get_children(self) -> List[Person]: pass

Slide 33

Slide 33 text

class Person: def get_children(self) -> List[Person]: pass NameError: name 'Person' is not defined

Slide 34

Slide 34 text

class Person: def get_children(self) -> List[Person]: pass def get_children(self) -> List['Person']: pass

Slide 35

Slide 35 text

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)

Slide 36

Slide 36 text

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)

Slide 37

Slide 37 text

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)

Slide 38

Slide 38 text

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)

Slide 39

Slide 39 text

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)

Slide 40

Slide 40 text

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)

Slide 41

Slide 41 text

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."""

Slide 42

Slide 42 text

# some other package (math.py) def add(a, b): return a + b

Slide 43

Slide 43 text

# stub (math.pyi) def add(a: float, b: float) -> float: …

Slide 44

Slide 44 text

No content

Slide 45

Slide 45 text

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

Slide 46

Slide 46 text

python/typeshed • contains external type annotations for the Python standard library and Python builtins • as well as third party packages.

Slide 47

Slide 47 text

No content

Slide 48

Slide 48 text

No content

Slide 49

Slide 49 text

No content

Slide 50

Slide 50 text

No content

Slide 51

Slide 51 text

No content

Slide 52

Slide 52 text

No content

Slide 53

Slide 53 text

[mypy] disallow_untyped_defs = True strict_optional = True warn_redundant_casts = True

Slide 54

Slide 54 text

:+1: • ޙࢲച੄ ӝמ (ӝઓ੄ docstring convention ؀୓)

Slide 55

Slide 55 text

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

Slide 56

Slide 56 text

:+1: • ޙࢲച੄ ӝמ (ӝઓ੄ docstring convention ؀୓) • IDE/ী٣ఠ ఋੑ ୶ۿ ѐࢶ • ੿੸ Type Checkerܳ ాೠ पࣻ ߑ૑

Slide 57

Slide 57 text

:-1: • ҃਋ী ٮۄ ࠛ೙ਃೠ ௏٘੄ ୶о

Slide 58

Slide 58 text

:-1: • ҃਋ী ٮۄ ࠛ೙ਃೠ ௏٘੄ ୶о • (ই૒਷) ׮ࣗ ࠗ઒ೠ ఋੑ दझమ

Slide 59

Slide 59 text

“Python is dynamically typed and we like it that way!” - Guido van Rossum

Slide 60

Slide 60 text

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

Slide 61

Slide 61 text

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

Slide 62

Slide 62 text

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

Slide 63

Slide 63 text

ߛ௼࢟۞٘ܳ ݅٘ח ۨ੉פझ౟ীࢲ ੷൞ ৬ ೣԋೡ ࢲߡ ূ૑פয ٜ࠙ਸ ଺Ҋ ੓ णפ׮. Python, aiohttp, django

Slide 64

Slide 64 text

Questions github: sunghyunzz email: [email protected]