GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — example-code-2e/10-dp-1class-func at master · FerMatPy/example-code-2e

Expand file tree

/

10-dp-1class-func

/
Copy path

Directory actions

More options

More options

Directory actions

More options

More options

Latest commit

 

History

History
History
 
 
/

10-dp-1class-func

/
Copy path

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
monkeytype
monkeytype
 
 
pytypes
pytypes
 
 
untyped
untyped
 
 
README.rst
README.rst
 
 
classic_strategy.py
classic_strategy.py
 
 
classic_strategy_test.py
classic_strategy_test.py
 
 
promotions.py
promotions.py
 
 
requirements.txt
requirements.txt
 
 
strategy.py
strategy.py
 
 
strategy_best.py
strategy_best.py
 
 
strategy_best2.py
strategy_best2.py
 
 
strategy_best3.py
strategy_best3.py
 
 
strategy_best4.py
strategy_best4.py
 
 
strategy_param.py
strategy_param.py
 
 
strategy_param_test.py
strategy_param_test.py
 
 
strategy_test.py
strategy_test.py
 
 

README.rst

Outline

Sample code for Chapter 10 - "Design patterns with first class functions"

From the book "Fluent Python" by Luciano Ramalho (O'Reilly, 2015) http://shop.oreilly.com/product/0636920032519.do

Notes

No issues on file with zero type hints

Running Mypy on classic_strategy.py from the first edition, with no type hints:

$ mypy classic_strategy.py
Success: no issues found in 1 source file

Type inference at play

When the Order.due method made first assignment to discount as discount = 0, Mypy complained:

mypy classic_strategy.py
classic_strategy.py:68: error: Incompatible types in assignment (expression has type "float", variable has type "int")
Found 1 error in 1 file (checked 1 source file)

To fix it, I made the first assigment as discount = 0. I never explicitly declared a type for discount.

Mypy ignores functions with no annotations

Mypy did not raise any issues with this test case:

def test_bulk_item_promo_with_discount(customer_fidelity_0):
    cart = [LineItem('banana', 30, .5),
            LineItem('apple', 10, 1.5)]
    order = Order(customer_fidelity_0, 10, BulkItemPromo())
    assert order.total() == 30.0
    assert order.due() == 28.5

The second argument to Order is declared as Sequence[LineItem]. Mypy only checks the body of a function the signature as at least one annotation, like this:

def test_bulk_item_promo_with_discount(customer_fidelity_0) -> None:
    cart = [LineItem('banana', 30, .5),
            LineItem('apple', 10, 1.5)]
    order = Order(customer_fidelity_0, 10, BulkItemPromo())
    assert order.total() == 30.0
    assert order.due() == 28.5

Now Mypy complains that "Argument 2 of Order has incompatible type".

However, even with the annotation in the test function signature, Mypy did not find any problem when I mistyped the name of the cart argument. Here, cart_plain should be cart:

def test_bulk_item_promo_with_discount(customer_fidelity_0) -> None:
    cart = [LineItem('banana', 30, .5),
            LineItem('apple', 10, 1.5)]
    order = Order(customer_fidelity_0, cart_plain, BulkItemPromo())
    assert order.total() == 30.0
    assert order.due() == 28.5

Hypotesis: cart_plain is a function decorated with @pytest.fixture, and at the top of the test file I told Mypy to ignore the Pytest import:

import pytest  # type: ignore
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.