Ce diaporama a bien été signalé.
Nous utilisons votre profil LinkedIn et vos données d’activité pour vous proposer des publicités personnalisées et pertinentes. Vous pouvez changer vos préférences de publicités à tout moment.

Python typing module

1 002 vues

Publié le

Overview of the Python 3.5 typing module, tips for getting started resources for learning more.

Publié dans : Logiciels
  • Identifiez-vous pour voir les commentaires

  • Soyez le premier à aimer ceci

Python typing module

  1. 1. The typing module in Python 3.5 Ryan Blunden
 @ryan_blunden A N O T H E R C O M P E L L I N G R E A S O N T O S T A R T U S I N G P Y T H O N 3 !
  2. 2. Recently returned to Australia after three years at LinkedIn in 
 Silicon Valley. Now running RabbitBird as a freelance developer, agile coach and technical trainer. Who am I?
  3. 3. What do you know about Python and its approach to typing? Is there anything specific you want to learn from this session? I F  YO U ’R E NOT AN ADVANC E D PYTHON D EVELOPER, DON’T WORRY. About you!
  4. 4. • Background and goals of the typing module • Syntax • Live coding demonstration • Tips for getting started with the typing module in your own projects • Review questions • Q&A. Agenda
  5. 5. By the end of this session, you will be able to: • Explain why the typing module is useful • Describe its basic capabilities • Apply type hinting to a simple function and run a type checker against it • Start thinking about how to use it in your own projects. Session objectives
  6. 6. Any Questions? ? ?
  7. 7. Types! A C R A S H C O U R S E I N B A S I C T Y P I N G T E R M I N O L O G Y F O R P Y T H O N
  8. 8. a = 5  # a starts as a number. a = ‘hello’  # Now a is a string. Python is cool with that. Variable are not bound to specific type. Dynamic typing ‘9’ + 5  # TypeError: Can't convert 'int' object to str implicitly No help from the (C)Python interpreter if you do something illegal with variable types.
  9. 9. // java int a = 5; a = “str”;  // This would cause a compilation error Types are defined and code will not compile unless type information matches what’s defined in code. Static typing
  10. 10. Not concerned about the actual underlying type, just has to satisfy the requirements of the calling code. "When I see a bird that walks like a duck and swims like a duck and quacks like a duck, I call that bird a duck.” - JAMES WHITCOMB RILEY # Knows nothing about the person, object but simply expects # that it will have a method `doGreeting` to call. def greetPerson(person):   print(person.doGreeting())   Duck typing
  11. 11. Better Documentation
 Improve code by including typing information that can be validated with an external type checker. Find bugs
 Enable static analysis tools to find bugs before code is run. Standardisation
 Standardise the typing syntax for the good of the Python ecosystem. So after all these years,why add a typing system to Python?
  12. 12. What started as a presentation in from Guido van Rossum in 2000 eventually evolved into two PEP proposals: PEP 3107 - Syntax PEP 484 - Type hints The recent mypy library provided much of the inspiration and push to finally get a type hinting solution into the Python standard library. The typing module - 15 years in the making!
  13. 13. IT’ S GOAL IS TO IMPROVE C OD E QUALITY. • Allows type information (function annotations) to be defined for function parameters and function return values. • Standardises the syntax for these type annotations. • Allows for adding type information outside of the actual code itself in “stub” files. class Person():     def greeting() -> str:         return ‘Hello there!' #…
 def greetPerson(person: Person) -> None:     print(person.doGreeting()); Whatdoesthetypingmoduledo? THE TY PIN G MODUL E HEL PS DEFINE TYP ES BUT D OESN’ T CHECK THEM.
  14. 14. Common Questions About The Typing Module Q A
  15. 15. Q A Python 2 compatible? Not in code. Yes with stubs.
  16. 16. Q A Which versions of Python 3 are supported? Python 3.5 has the typing module but mypy-lang supports Python 3.2 onwards. Syntax of type annotations introduced in Python 3.0.
  17. 17. Q A Will it slow down,break,crash or do any harm to my code? No. Not. Ever! It does not affect how code runs.
  18. 18. Q A Is Python dictating that I have to use this now? No, and it never will. This is not a plot to turn Python into Java.
  19. 19. Q A Who uses static analysis tools for Python? • Python std lib. • Code review and quality tools (Quantified Code, Codacy). • IDE’s. • Google, Dropbox, probably others.
  20. 20. Let’s Code! https://github.com/ryan-blunden/python-typing
  21. 21. Python type hinting calls for embracing “gradual typing" Apply the type hinting to what you can, when you can. It doesn’t affect how your code runs.
  22. 22. Tips for getting started • Focus more on adding type hints as documentation. The mypy-lang checker still has a long way to go. • If you haven’t already, trial PyCharm and use the type annotations on some of your code to see how it can aid your workflow. • If you’re working with Python 2 code, try working with stubs. • If you’re working with Python 3.2-3.4, use mypy. • Choose one of your favourite open source Python libraries and create a pull request that includes type definitions. • If you use mypy either for type hinting or checking, install it from source 
  23. 23. Conclusion • Helps find bugs • Gradual typing approach • Not required. Use it if you want to. • Improves code documentation (as it is runnable and verifiable) • Helps our IDE’s to help us • Can be used in Python 2 and Python 3
  24. 24. Review Questions • What are some of the benefits that could come from using type hints in Python? • What versions of Python can you use the type hints in? • What risk do the type hints pose if you apply them to existing code? • List one way you could get started in applying type hints to one of your projects today? ?
  25. 25. Wrapping up - Our objectives were... • Explain why the typing module is useful • Describe its basic capabilities • Apply type hinting to a simple function and run a type checker against it • Start thinking about how to use it in your own projects.
  26. 26. Recommended Reading Type Hints - Guido van Rossum - PyCon 2015 Python Typeshed Dynamic typing Static typing Duck Typing PEP 3107 PEP 0484 Gradual typing Python 3.5 typing module documentation Python 3.5 type hinting in PyCharm 5 Mypy-lang Python's New Type Hints in Action… In JavaScript Why you need to install mypy-lang from source at the moment
  27. 27. Thanks!