제 3회 파이썬 격월 세미나에서 발표한 [Python Type Hints] 발표 슬라이드입니다. (https://www.facebook.com/groups/pythonkorea/)
Python Type Hintspresenter: str = "sunghyunzz"
View Slide
PEP 484 - Type Hints• PEP 3107 - Function Annotations
PEP 484 - Type Hints• PEP 3107 - Function Annotations• 2014-09-29
PEP 484 - Type Hints• PEP 3107 - Function Annotations• 2014-09-29• Python 3.5
def add(a: int, b: int) -> int:return a + b
def print_sum(a: int, b: int) -> None:print(a + b)
def get_list(a: int, b: int) -> list:return [a, b]
def get_list(a: int, b: int) -> list:return [a, b]from typing import Listdef get_list(a: int, b: int) -> List[int]:return [a, b]
def get_list(a: int, b: int) -> List:return ['a', a, 'b', b, 'a + b', a + b]
def get_list(a: int, b: int) -> list:return [a, b]from typing import Any, Listdef get_list(a: int, b: int) -> List[Any]:return ['a', a, 'b', b, 'a + b', a + b]
def func(text: str) -> str:result: int = some_complex_func(text)return str(result)
class Person:def __init__(self, name: str) -> None:self.name: str = nameself.length_of_name: int = len(name)
from typing import List, Unionresult: List[Union[int, str]] = [1, 'a', 2, 'b']
from typing import Optional, Unionassert Optional[int] == Union[int, None]
from typing import Dict, Setdef 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}
from typing import Tupledef get_tuple(a: int, b: int) -> Tuple[int]:return a, b # Expected type 'Tuple[int]', got 'Tuple[int, int]' instead
from typing import Tupledef get_tuple(a: int, b: int) -> Tuple[int, int]:return a, b
from typing import Tupledef get_tuple() -> Tuple[int, int, int, int, int, int, int, int, int, int]:return 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
from typing import Tupledef get_tuple() -> Tuple[int, ...]:return 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
from typing import Callabledef add_lazy(a: int, b: int):def f() -> int:return a + breturn f
from typing import Callabledef add_lazy(a: int, b: int) -> Callable[[], int]:def f() -> int:return a + breturn f
from typing import Callabledef run(f: Callable[[int, str], int], a: int, b: str) -> int:return f(a, b)
def run(f: Callable[..., int], *args) -> int:return f(args)
def run(f: Callable[..., int], *args: int) -> int:return f(args)
def run(f: Callable[[Dict[str, int]], int],**kwargs: int) -> int:return f(kwargs)
IntReturningFunction = Callable[..., int]def run(f: IntReturningFunction) -> int:return f(1, 2, 3, 4, 5)
def fetch(_id: int, category: int) -> Transaction:pass
EntityID = intdef fetch(_id: EntityID, category: int) -> Transaction:pass
class Person:def get_children(self) -> List[Person]:pass
class Person:def get_children(self) -> List[Person]:passNameError: name 'Person' is not defined
class Person:def get_children(self) -> List[Person]:passdef get_children(self) -> List['Person']:pass
from abc import ABCMeta, abstractmethodfrom typing import Generic, TypeVarT = TypeVar('T')class Person:passclass Mapper(Generic[T], metaclass=ABCMeta):@classmethod@abstractmethoddef from_dict(cls, request: dict) -> T:passdef convert(mapper: Mapper[Person], data: dict) -> Person:return mapper.from_dict(data)
python2 supportfrom typing import Listdef hello(): # type: () -> Noneprint 'hello'class Example:def method(self, lst, opt=0, *args, **kwargs):# type: (List[str], int, *str, **bool) -> int"""Docstring comes after type comment."""
# some other package (math.py)def add(a, b):return a + b
# stub (math.pyi)def add(a: float, b: float) -> float: …
python/typeshed• contains external type annotations for thePython standard library and Python builtins
python/typeshed• contains external type annotations for thePython standard library and Python builtins• as well as third party packages.
[mypy]disallow_untyped_defs = Truestrict_optional = Truewarn_redundant_casts = True
:+1:• ޙࢲച ӝמ (ӝઓ docstring convention )
:+1:• ޙࢲച ӝמ (ӝઓ docstring convention )• IDE/ী٣ఠ ఋੑ ୶ۿ ѐࢶ
:+1:• ޙࢲച ӝמ (ӝઓ docstring convention )• IDE/ী٣ఠ ఋੑ ୶ۿ ѐࢶ• Type Checkerܳ ాೠ पࣻ ߑ
:-1:• ҃ী ٮۄ ࠛਃೠ ٘ ୶о
:-1:• ҃ী ٮۄ ࠛਃೠ ٘ ୶о• (ই) ࣗ ࠗೠ ఋੑ दझమ
“Python is dynamically typed andwe like it that way!”- Guido van Rossum
ۨפझীࢲח• ఋੑ ݺदо ٘ оةࢿਸ ֫ੋҊ ਵݴ ۽ં ҙܻ࠺ਊਸ ծ ࣻ Ҋ ౸ױೞৈ بੑ೮णפ.
ۨפझীࢲח• ఋੑ ݺदо ٘ оةࢿਸ ֫ੋҊ ਵݴ ۽ં ҙܻ࠺ਊਸ ծ ࣻ Ҋ ౸ױೞৈ بੑ೮णפ.• नӏ ۽ંח CI җীࢲ mypyܳ प೯פ.
ۨפझীࢲח• ఋੑ ݺदо ٘ оةࢿਸ ֫ੋҊ ਵݴ ۽ં ҙܻ࠺ਊਸ ծ ࣻ Ҋ ౸ױೞৈ بੑ೮णפ.• नӏ ۽ંח CI җীࢲ mypyܳ प೯פ.• ఋੑ दझమ ೠ҅۽ ੋ೧ ࠛਃೠ ٘ܳ ࢿ೧ঠ ೡ ٸীח @typing.no_type_check
ߛ࢟۞٘ܳ ݅٘ח ۨפझীࢲ ൞৬ ೣԋೡ ࢲߡ ূפয ٜ࠙ਸ Ҋ णפ.Python, aiohttp, django
Questionsgithub: sunghyunzzemail: [email protected]