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 Slide

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

    View Slide

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

    View Slide

  4. View Slide

  5. ੿੸ ఋੑ ঱য

    ز੸ ఋੑ ঱য

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

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


    ... x ?

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View 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 Slide

  18. 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 Slide

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

    View Slide

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

    View Slide

  21. Python 3.6+

    View Slide

  22. ఋੑ ൦౴

    View Slide

  23. ఋੑ ൦౴

    View Slide

  24. ఋੑ ൦౴

    View Slide

  25. ఋੑ ൦౴

    View Slide

  26. ఋੑ ൦౴

    View Slide

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

    View Slide

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

    View Slide

  29. ఋੑ ൦౴

    View Slide

  30. ఋੑ ൦౴

    View Slide

  31. ఋੑ ൦౴

    View Slide

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

    View Slide

  33. ఋੑ ൦౴

    View Slide

  34. ఋੑ ൦౴

    View Slide

  35. ఋੑ ൦౴

    View Slide

  36. ఋੑ ൦౴

    View Slide

  37. ఋੑ ൦౴

    View Slide

  38. ఋੑ ൦౴

    View Slide

  39. ఋੑ ൦౴

    View Slide

  40. View Slide

  41. ఋੑ ൦౴

    View Slide

  42. ఋੑ ൦౴

    View Slide

  43. View Slide

  44. View Slide

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

    View Slide

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

    View Slide

  47. View Slide

  48. mypy
    mypy ࢸ஖ೞӝ

    View Slide

  49. mypy
    ੿੸ ఋੑ Ѩࢎೞӝ

    View Slide

  50. mypy
    ੿੸ ఋੑ Ѩࢎೞӝ

    View Slide

  51. mypy
    ੿੸ ఋੑ Ѩࢎೞӝ

    View Slide

  52. View Slide

  53. 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 Slide

  54. mypy

    View Slide

  55. mypy

    View Slide

  56. mypy

    View Slide

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

    View Slide

  58. github.com/python/mypy

    View Slide

  59. Q & A

    View Slide

  60. хࢎ೤פ׮
    [email protected]

    View Slide