Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

NewType for Callables in Python

I would like to define two types Shape and Square. Here Square is a subclass of Shape.

Shape = NewType('Shape', tuple[int, int])
Square = NewType('Square', Shape)

def square_area(sh: Square) -> int:
    a, b = sh
    return a * b

def shape_area(sh: Shape) -> int:
    a, b = sh
    return a * b

I would define an additional type SquareCalculator as follows:

SquareCalculator = NewType('Calculator', Callable[[Square], int])

def get_calc() -> SquareCalculator:
    return square_area

The above code leads to the following errors in mypy:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

newtyp.py:10: error: String argument 1 "Calculator" to NewType(...) does not match variable name "SquareCalculator"
newtyp.py:13: error: Incompatible return value type (got "Callable[[Square], int]", expected "SquareCalculator")
Found 2 errors in 1 file (checked 1 source file)

Is there a way around this? Note that I would like to maintain the difference in Shape and Square to ensure type-checking catches errors when they are inter-changed.

>Solution :

SquareCalculator should be a type alias, not a newtype, otherwise you need to use typing.cast.

Current syntax:

from typing import TypeAlias
from collections.abc import Callable

SquareCalculator: TypeAlias = Callable[[Square], int]

Python 3.12 syntax:

from collections.abc import Callable

type SquareCalculator = Callable[[Square], int]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading