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

Python Type Hinting and Static Type Checking

Python Type Hinting and Static Type Checking

ChangHui Lee

August 17, 2019
Tweet

More Decks by ChangHui Lee

Other Decks in Programming

Transcript

  1. PyCon Korea 2019
    ੿੸ ఋੑ Ѩࢎ۽
    ؊ ա਷ Python ௏٘ ੘ࢿೞӝ
    ੉ହ൞

    View full-size slide

  2. ߊ಴੗
    blurfx
    • ࣽୌೱ؀೟Ү (2014 ~ )
    • ֏ट௏ܻই (2018.03 ~ 2019.07)
    • ೖ೒ಎ٘ஹಌפ (2019.07 ~ )

    View full-size slide

  3. ౵੉ॆਸ फযೞח ࢎۈٜ਷
    ౵੉ॆਸ ৵ फযೡөਃ?

    View full-size slide

  4. ੿੸ ఋੑ ঱য

    ز੸ ఋੑ ঱য

    View full-size slide

  5. ੿੸ ఋੑ ঱য
    Writing Code Build Test Run/Deploy
    য়ܨܳ ߊѼೞח ҳр

    View full-size slide

  6. ز੸ ఋੑ ঱য
    Writing Code Test Run/Deploy
    য়ܨܳ ߊѼೞח ҳр

    View full-size slide

  7. ੿੸ ఋੑ Ѩࢎо ޖ঺੉Ҋ
    ೞݶ ޖ঺੉ જই૑աਃ?

    View full-size slide

  8. ೐۽ં౟о ੘ਵݶ ѐߊ੗੄ ࣻب ੸Ҋ, ߡӒ੄ ࣻب ੸૑݅
    icons from https://icons8.com

    View full-size slide

  9. ೐۽ં౟о ੘ਵݶ ѐߊ੗੄ ࣻب ੸Ҋ, ߡӒ੄ ࣻب ੸૑݅
    icons from https://icons8.com
    ೐۽ં౟о ழ૑Ҋ ѐߊ੗о ݆ই૑ݶ Ӓ ݅ఀ ߡӒب ݆ই૘פ׮

    View full-size slide

  10. ೐۽ં౟о ੘ਵݶ ѐߊ੗੄ ࣻب ੸Ҋ, ߡӒ੄ ࣻب ੸૑݅
    icons from https://icons8.com
    ೐۽ં౟о ழ૑Ҋ ѐߊ੗о ݆ই૑ݶ Ӓ ݅ఀ ߡӒب ݆ই૘פ׮
    ز੸ ఋੑ ঱য۽ ੘ࢿೠ ௏٘ח ೐۽ં౟о ழ૕ࣻ۾

    п ё୓о যڃ чਸ ঌҊ ੓ח૑ ൨ٜয૘פ׮
    var x 


    ... x ?

    View full-size slide

  11. Writing Code
    Static Type
    Checking
    Test Run/Deploy
    ੿੸ ఋੑ ୓௼۽ ఋੑী য়ܨо ੓ח૑ Ѩࢎ

    View full-size slide

  12. ௏٘੄ оةࢿ੉ ৢۄоҊ
    ఋੑਵ۽ ߊࢤೡ ࣻ ੓ח ߡӒܳ ৘ߑ೧઻ਃ

    View full-size slide

  13. ௏٘੄ оةࢿ੉ ৢۄоҊ
    ఋੑਵ۽ ߊࢤೡ ࣻ ੓ח ߡӒܳ ৘ߑ೧઻ਃ

    View full-size slide

  14. To Type or Not to Type:
    Quantifying Detectable Bugs in JavaScript
    http://earlbarr.com/publications/typestudy.pdf

    View full-size slide

  15. http://earlbarr.com/publications/typestudy.pdf
    To Type or Not to Type

    View full-size slide

  16. Conclusion:
    we found that using Flow or TypeScript
    could have prevented 15% of the public
    bugs for public projects on GitHub.
    http://earlbarr.com/publications/typestudy.pdf
    To Type or Not to Type

    View full-size slide

  17. Conclusion:
    we found that using Flow or TypeScript
    could have prevented 15% of the public
    bugs for public projects on GitHub.
    http://earlbarr.com/publications/typestudy.pdf
    To Type or Not to Type

    View full-size slide

  18. ౵੉ॆ਷ ز੸ ఋੑ ঱যੋؘ
    ఋੑਸ যڌѱ ઻ਃ?

    View full-size slide

  19. Function Annotation
    Type Annotation
    Variable Annotation
    Python 3.0+
    Python 3.5+
    Python 3.6+

    View full-size slide

  20. ఋੑ ൦౴

    View full-size slide

  21. ఋੑ ൦౴

    View full-size slide

  22. ఋੑ ൦౴

    View full-size slide

  23. ఋੑ ൦౴

    View full-size slide

  24. ఋੑ ൦౴

    View full-size slide

  25. • ੿ࣻ৬ ޙ੗ৌ݅ ೲਊೞח ܻझ౟
    • ޙ੗ৌ, पࣻ, ࠗ਎ ࣽࢲ۽ ػ ౚ೒
    • ޙ੗ৌ ః৬ ߄੉౟ чਸ о૓ ٤࣊ցܻ
    • ઁ֎ܼ ೣࣻ৬ ௿ېझ
    ఋੑ ൦౴

    View full-size slide

  26. https://imgflip.com/i/186jmm

    View full-size slide

  27. ఋੑ ൦౴

    View full-size slide

  28. ఋੑ ൦౴

    View full-size slide

  29. ఋੑ ൦౴

    View full-size slide

  30. TypeError: Too many parameters for typing.List; actual 2, expected 1
    ఋੑ ൦౴

    View full-size slide

  31. ఋੑ ൦౴

    View full-size slide

  32. ఋੑ ൦౴

    View full-size slide

  33. ఋੑ ൦౴

    View full-size slide

  34. ఋੑ ൦౴

    View full-size slide

  35. ఋੑ ൦౴

    View full-size slide

  36. ఋੑ ൦౴

    View full-size slide

  37. ఋੑ ൦౴

    View full-size slide

  38. ఋੑ ൦౴

    View full-size slide

  39. ఋੑ ൦౴

    View full-size slide

  40. http://blog.daum.net/junjungirl/109

    View full-size slide

  41. “ no type checking happens at runtime ”
    — PEP 484

    View full-size slide

  42. mypy
    mypy ࢸ஖ೞӝ

    View full-size slide

  43. mypy
    ੿੸ ఋੑ Ѩࢎೞӝ

    View full-size slide

  44. mypy
    ੿੸ ఋੑ Ѩࢎೞӝ

    View full-size slide

  45. mypy
    ੿੸ ఋੑ Ѩࢎೞӝ

    View full-size slide

  46. Incompatible types in assignment (expression has type "str", variable has type "int")
    List item 1 has incompatible type "int"; expected "str"
    List item 2 has incompatible type "float"; expected "str"
    Argument 1 to "append" of "list" has incompatible type "bytes"; expected "str"

    View full-size slide

  47. mypy.readthedocs.io
    ؊ ੗ࣁೠ ղਊ਷...

    View full-size slide

  48. github.com/python/mypy

    View full-size slide