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

今から始める型安全 Python / Start Python Type Hints

今から始める型安全 Python / Start Python Type Hints

#stapy

https://speakerdeck.com/yamitzky/mastering-type-safety-in-python-3-dot-9-era の内容を加筆して削ったものです

Mitsuki Ogasahara

March 10, 2021
Tweet

More Decks by Mitsuki Ogasahara

Other Decks in Technology

Transcript

  1. ࠓ͔Β࢝ΊΔܕ҆શ1ZUIPO
    খּݪΈ͖ͭ!ZBNJU[LZ
    TUBQZ

    View Slide

  2. Agenda
    ಋೖ
    جຊฤܕͷ֓ཁͱجຊจ๏
    1ZUIPOϓϩάϥϜΛܕ҆શʹ͠Α͏
    ܕͰ1ZUIPOΛศརʹ͠Α͏
    ·ͱΊ

    View Slide

  3. 2ීஈɺ1ZUIPOͰܕॻ͖·͔͢ʁ
    1. ࠓ೔ͷ໨ඪ
    ˡʮܕΛ࢝Ί͍ͨʯ
    ^

    View Slide

  4. 1Z$PO+1ͷϕεττʔΫͷ಺༰Λ
    ʮ࢝ΊΔʯਓ޲͚ʹՃචɾमਖ਼
    CJUMZQZUZQF
    1. ࠓ೔ͷ಺༰

    View Slide

  5. 1. ͓લɺ୭Αʁ
    w খּݪޫو!ZBNJU[LZ
    w +9௨৴ࣾऔక໾$%0
    w ޷͖ͳٕज़
    5ZQF4DSJQUɺ1ZUIPO ೥ʙ

    αʔόʔϨεɺ(SBQI2-FUDʜ
    ˞54ϩΰ$$#:.JDSPTPGU

    View Slide

  6. 1. JX௨৴ࣾʹ͍ͭͯ
    ʮࠓى͖͍ͯΔ͜ͱΛ໌Β͔ʹ͢ΔʯใಓతςΫϊϩδʔϕϯνϟʔ
    1Z$PO+1
    γϧόʔεϙϯαʔ
    ར༻ٕज़ ٕज़ͱͷؔΘΓ
    8%#13&44
    ʮαʔόʔϨεಛूʯدߘ
    ࣄۀ
    هऀͷ͍ͳ͍௨৴ࣾͱͯ͠ɺUP#ɾUP$ͰχϡʔεࣄۀΛల։

    View Slide

  7. 1. JX௨৴ࣾʹ͍ͭͯ
    "*ϫΫνϯ઀छ༧ଌ
    "*ۓٸ৘ใαʔϏε
    ʮ'"45"-&35ʯ

    View Slide

  8. Python ͷνʔϜ։ൃ΍
    େن໛ͳ Python ϓϩδΣΫτ
    ෆຬ͕͋Γ·ͤΜ͔ʁ
    1. Python ͷෆຬ

    View Slide

  9. 1. Python ͷෆຬ

    View Slide

  10. ˞ฐࣾ๭ϓϩδΣΫτʹ΋͋Γ·ͨ͠🤦
    1. Python ͷෆຬ

    View Slide

  11. 1. Python ͷෆຬ

    View Slide

  12. ಈతܕ෇͖ݴޠͷෆຬ
    ʮܕʯͰղܾͰ͖·͢💡
    1. Python ͷෆຬ

    View Slide

  13. 1ZUIPOͷܕͷ֓ཁͱجຊจ๏
    جຊฤ
    02

    View Slide

  14. ɾਖ਼໊ࣜশ͸ʮ5ZQF)JOU ܕώϯτ
    ʯ
    ɾ1&1Ͱ1ZUIPO ʙ
    ͔Βೖͬͨ࢓༷
    ɾ໨త੩తղੳɺϦϑΝΫλϦϯάɺίʔυੜ੒ͳͲ
    ɾ1ZUIPO͕੩తܕ෇͖ݴޠʹͳͬͨΘ͚Ͱ͸ͳ͘ɺ
    ɹ೚ҙɾޙ෇͚ͷܕγεςϜ
    1ZUIPOͷܕͬͯͳΜͩΖ͏
    2. ʮܕʯͷ֓ཁ
    ˞͜ͷεϥΠυͰʮܕʯͱݴٴ͢Δࡍ͸ʮܕώϯτʯͷ͜ͱΛݴ͍·͢

    View Slide

  15. 2. ʮܕʯͷ֓ཁ
    ੩తղੳ
    ϦϑΝΫλϦϯά

    View Slide

  16. 1ZUIPO͸ʮ੩తܕ෇͖ݴޠʯͰ͸ͳ͍
    2. ʮܕʯͷ֓ཁ
    2͜ͷίʔυ͸࣮ߦ࣌ΤϥʔʹͳΔʁ🤔
    2ܕͷ৘ใ͸TUSʁJOUʁ🤔

    View Slide

  17. 1ZUIPO͸ʮ੩తܕ෇͖ݴޠʯͰ͸ͳ͍
    2. ʮܕʯͷ֓ཁ
    "࣮ߦ࣌ʹ͸Τϥʔ͸ൃੜ͠ͳ͍🙆
    αʔυύʔςΟʔͷπʔϧΛ૊Έ߹Θͤͯ׆༻👍
    "UZQF
    ͷ໭Γ஋ͱͯ͠͸ܕώϯτ͸ແؔ܎

    View Slide

  18. ΤσΟλ
    ิ׬ͳͲ

    1ZUIPOͷʮܕʯΛ
    औΓר͘αʔυύʔςΟʔ
    2. ʮܕʯͷ֓ཁ
    mypy
    pyright
    pytype
    VSCode
    PyCharm
    Vim
    pydantic
    FastAPI
    ੩తղੳ
    ࣮ߦ࣌ͷόϦσʔγϣϯͳͲ
    ࢀߟIUUQTHJUIVCDPNUZQFEEKBOHPBXFTPNFQZUIPOUZQJOH

    View Slide

  19. ɾਖ਼໊ࣜʮ5ZQF)JOUʯΛ঺հ
    ɾ1ZUIPO͸ಈతܕ෇͖ݴޠͰ͋Γɺܕώ
    ϯτ͸ڧ੍͞Εͳ͍
    ɾ1ZUIPOࣗମ͸ܕνΣοΫΛߦΘͳ͍ɻ
    SEQBSUZπʔϧΛ૊Έ߹ΘͤΔ
    ͜͜·Ͱͷ·ͱΊ
    2. ʮܕʯͷ֓ཁ

    View Slide

  20. ม਺
    2. ܕώϯτͷจ๏ม਺

    View Slide

  21. ܕਪ࿦
    2. ܕώϯτͷจ๏ม਺
    ˺
    ܕνΣοΧʔ͕ਪ࿦͢ΔͷͰɺ
    ࣮͸ͦΜͳʹ໘౗͘͘͞ͳ͍🙆

    View Slide

  22. ؔ਺
    2. ܕώϯτͷจ๏ؔ਺

    View Slide

  23. Ҿ਺͚ͩॻ͍ͯ΋0,🙆
    2. ܕώϯτͷจ๏ؔ਺
    ໭Γ஋͚ͩॻ͍ͯ΋0,🙆

    View Slide

  24. DMBTT
    2. ܕώϯτͷจ๏DMBTT
    ˡϓϩύςΟͷఆٛ
    ˡϝιουఆٛ
    ˡม਺ͷܕͱͯ͠ར༻

    View Slide

  25. ϓϩύςΟͷܕ͕Θ͔Βͳ͍ͱ͖
    2. ܕώϯτͷจ๏"OZ
    ˡϓϩύςΟͷఆٛ
    ˡϝιουఆٛ

    View Slide

  26. ҉໧తͳ"OZ
    2. ܕώϯτͷจ๏"OZ
    ˡϓϩύςΟͷఆٛ
    ˡϝιουఆٛ
    ˺

    View Slide

  27. MJTU EJDU TFU
    2. ܕώϯτͷจ๏σʔλߏ଄

    View Slide

  28. /POF͕ೖΔ͔΋͠Εͳ͍ม਺ l͵ΔΆz๷ࢭ

    ˡ/POF͡Όͳ͍͜ͱΛ͔֬ΊΔͱ
    ҆શʹ࢖͑Δ
    ˡ/POFͷՄೳੑͷࢦఠ
    2. ܕώϯτͷจ๏0QUJPOBM

    View Slide

  29. ɾม਺ɺؔ਺
    ɾDMBTT
    ɾMJTUɺEJDUɺTFU
    ɾ0QUJPOBM
    ɾ"OZ
    ͜͜·Ͱͷ·ͱΊ
    2. ܕώϯτجຊฤ·ͱΊ

    View Slide

  30. ʮܕʯॻ͘ͷ໘౗ͦ͘͞͏ʜ🤔ͱࢥͬͨํ΁
    ˠʮυΩϡϝϯτʯͷྫ͔Βิ଍͠·͢
    2.ʮܕʯͱυΩϡϝϯτ

    View Slide

  31. ʮܕʯͱυΩϡϝϯτ
    ʮܕώϯτʯʹ҆શˍεϚʔτͳυΩϡϝϯτ

    View Slide

  32. ʮܕʯͱυΩϡϝϯτ
    ๭03.
    ࠓ·Ͱͷϝλσʔλࢦఆ͸ɺܕ͔Βߟ͑Δͱෆࣗવ
    ๭"1*εΩʔϚ
    ϞσϧͷϓϩύςΟͷظ଴஋͸จࣈྻͰ͋ͬͯɺ$IBS'JFMEɺ4USJOHͷΠϯελϯεͰ͸ͳ͍

    View Slide

  33. ʮܕʯͱυΩϡϝϯτ
    ҆શɾεϚʔτͳϝλσʔλͷࢦఆ
    ˞UZQJOH"OOPUBUFE͸1&1 1ZUIPO
    Ͱಋೖ͞ΕͨɺܕώϯτͱίϯϑϦΫτ͠ͳ͍ϝλσʔλࢦఆ
    ͷํ๏ɻϥΠϒϥϦͷରԠ͕ඞཁͳͷͰɺ؍ଌൣғͩͱ1ZEBOUJD͙Β͍͔͠ରԠ͍ͯ͠ͳ͔ͬͨ
    ˞

    View Slide

  34. PythonϓϩάϥϜΛܕ҆શʹͯ͠ΈΑ͏
    Ԡ༻ฤ
    03

    View Slide

  35. ɾ1ZUIPO͸੩తܕ෇͖ݴޠͰ͸ͳ͍
    ɾ1ZUIPOࣗମ͸ܕνΣοΫΛ͠ͳ͍
    ˠπʔϧͰܕνΣοΫ🚨
    1ZUIPOͷܕώϯτͷݪଇ
    3. ܕνΣοΫ

    View Slide

  36. ɾQZUIPONZQZ
    ɾNJDSPTPGUQZSJHIU
    ɾHPPHMFQZUZQF
    ɾGBDFCPPLQZSF
    ܕνΣοΫπʔϧɺ݁ߏ͋Δ
    3. ܕνΣοΧʔ
    ࡞ΒΕ͍ͯΔݴޠɺΤσΟλ࿈ܞɺܕਪ࿦ͷݡ͞ɺεύϧλ౓ʹҧ͍

    View Slide

  37. 3. ܕνΣοΧʔͷൺֱ(Ұྫ)
    NZQZ QZSJHIU
    ˞ܕώϯτΛશ͘ॻ͔ͳͯ͘΋ɺܕ҆શΛࠓ͔Β࢝ΊΔ͜ͱ͕Ͱ͖Δ
    ໭Γ஋ͷܕ΋ਪ࿦
    SFUVSO࿙ΕΔͱΤϥʔ

    View Slide

  38. ɾܕώϯτΛॻ͍ͯͳ͍ίʔυ͕ଟ͘ଘࡏ
    ɾQZSJHIUʹΑΔܕνΣοΫΛ։࢝
    ʮ'"45"-&35ʯͰ$*ͷܕνΣοΫΛ࢝Ίͨࣄྫ
    3. JX௨৴ࣾͷҰྫ
    ˠ$*ʹ૊ΈࠐΜͰΈͨͱ͜Ζʜ

    View Slide

  39. ˡOPEF؀ڥ͕ඞཁͳͷ͕গ͠σϝϦοτ
    ˡίϚϯυࣗମ͸؆୯

    View Slide

  40. 3. JX௨৴ࣾͷҰྫ

    View Slide

  41. 3. JX௨৴ࣾͷҰྫ
    QZSJHIUDPOpHKTPO
    ˠܕͷΤϥʔΛ8BSOJOHѻ͍ʹ $*Λ௨͢

    View Slide

  42. 3. JX௨৴ࣾͷҰྫ
    ˠ઴ਐతʹΦϓτΠϯ͍ͯ͘͠

    View Slide

  43. 3. ͲΜͳͱ͖ʹܕώϯτॻ͘ʁ
    ˡDMBTTͷϓϩύςΟ͸ॻ͘
    ˡMJTUͷॳظ஋ͳͲ
    ܕਪ࿦Ͱ͖ͳ͍΋ͷ΋ॻ͘
    ˡҾ਺ɾ໭Γ஋΋ॻ͘
    ˡม਺͸ਪ࿦͞ΕΔͷͰ
    ݪଇॻ͔ͳ͍

    View Slide

  44. ϥΠϒϥϦʹʮܕʯ͕ͳ͍ͱΤϥʔʹͳΔ😱
    3. ܕνΣοΫ͋Δ͋Δ
    ༁lCPUPzʹ͸ܕώϯτͳ͍Α
    ˞ܕώϯτͷͳ͍ϥΠϒϥϦ͸݁ߏଟ͍

    View Slide

  45. ཁ໿ʮ͖͋ΒΊͯແࢹ͢Δͷ΋खͰ͢Αʯ
    3. ܕώϯτͷͳ͍ϥΠϒϥϦ
    CJUMZQZUZQF

    View Slide

  46. ϥϯλΠϜΛ΋ͬͱศརʹ͢Δ
    Ԡ༻ฤ
    04

    View Slide

  47. ࠓ·Ͱɿ੩తͳܕνΣοΫͷ࿩
    4. ܕώϯτͱϥϯλΠϜ
    ࣮ߦ࣌ ϥϯλΠϜ
    ʹ
    ܕͷ৘ใ͸औಘͰ͖Δ

    View Slide


  48. EBUBDMBTT
    . ඪ४ϥΠϒϥϦͷ׆༻ྫ
    !EBUBDMBTTσίϨʔλʔΛ͔ͭ͏ͱ
    ࣮ߦ࣌ʹ@@[email protected]@ΛࣗಈͰఆٛͯ͘͠ΕΔ

    View Slide

  49. ˺EBUBDMBTTʴόϦσʔγϣϯʴγϦΞϥΠζ

    QZEBOUJD
    . ϥϯλΠϜόϦσʔγϣϯ

    View Slide


  50. 'BTU"1*
    . API ͷόϦσʔγϣϯ
    ܕώϯτ෇͖ͷؔ਺Λఆ͍͚ٛͯͩ͘͠Ͱɺ
    όϦσʔγϣϯ͖ͭͷ"1*͕Ͱ͖Δ

    View Slide

  51. ܕώϯτ͔Β4XBHHFS 0QFO"1*
    ΋ࣗಈੜ੒
    . FastAPI
    ˞ଞݴޠͰ΋࢖ΘΕΔ"1*࢓༷ɻ"1*ͷܕ͕ఆٛͰ͖Δ

    View Slide

  52. ʮܕώϯτॻ͘ͷ໘౗͍͘͞🤔ʯ
    ʮܕώϯτΛॻ͘ͱศར💡ʯ

    View Slide

  53. ɾσίϨʔλʔɺϝλΫϥεͱ૊Έ߹ΘͤΔͱ%3:ʹ
    ɾܕͷಈతνΣοΫɺ"1*ͷόϦσʔγϣϯɺ4XBHHFS
    ఆٛͳͲ΋Ͱ͖Δ
    ϥϯλΠϜͰͷܕώϯτΛ঺հ
    . ·ͱΊ

    View Slide

  54. ·ͱΊ
    05

    View Slide

  55. ܕώϯτΛॻ͘ͱ/POF΍
    ܕͷϛεΛνΣοΫͰ͖Δ
    ·ͱΊ

    View Slide

  56. ܕώϯτ͸ϝλσʔλͷ
    ҆શɾεϚʔτͳॻ͖ํ
    . ·ͱΊ

    View Slide

  57. ʮܕʯΛॻ͚ͩ͘ͰϥϯλΠϜ͕ศརʹ
    . ·ͱΊ

    View Slide

  58. NZQZ΍QZSJHIUͰܕνΣοΫ
    ਪ࿦͞ΕΔʹॻ͔ͳͯ͘΋࢝ΊΒΕΔ
    ·ͱΊ
    ddddddddddddd

    View Slide

  59. ܕώϯτɺ
    ࢝Ίͨ͘ͳΓ·ͨ͠Ͷʁʁʁʁʁ

    View Slide

  60. Thank you! +9௨৴ࣾͰ͸
    ɾܕΛॻ͖͍ͨ1ZUIPOJTUB
    ɾ.-ΛαʔϏε׆༻͍ͨ͠ΤϯδχΞ
    Λੵۃ࠾༻͍ͯ͠·͢

    View Slide