Giter VIP home page Giter VIP logo

python_type_checking's Introduction

Python Type Checking

Some code to understand Python type checking.

sales1.py

A script with today's my understanding. I do not fully utilize type checking.

sales2.py

I hear that mypy runs sanity check on types. I write code to set string as int property and mypy does not complain. Why doesn't it warn my mistake? What mypy offers is potential mistakes like putting a string into a property which should an integer?

sales3.py

mypy reports errors on reduce functions. My code returns value of int or float correctly, but mypy thinks it is wrong.

This is understandable. Real values will be known when code really executes. Software such as mypy cannot know what values are actually thrown to functions such as reduce().

I added more of type hints to helper functions that reduce() uses, but mypy raises errors anyway.

So, here is my question. What is mypy for, if it does not understand code correctly. In what ways does it help users?

When I define a class such as order, and I want its itemlines property to be a list of another class Itemline, I can write List[Itemline], which is nice. When I do this, definition of class Itemline should be come before definition of class Order. This is understandable.

Conclusion

Type hints and checking helps me become more organized about data structure I am trying to create.

typing package "does not enforce function and variable type annotations." Still it helps me to think ahead about data structure.

mypy seems not to be able to analyze my code correctly, although I give extensive type hints. I do not understand what mypy is for.

History

October 11th, 2020: Written.

python_type_checking's People

Contributors

aidken avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.