static-typing

Python how to type hint a Callable with __wrapped__

Python how to type hint a Callable with __wrapped__ Question: When passing around functions, I normally type hint them with typing.Callable. The docs for collections.abc.Callable state that it has four dunder methods: class collections.abc.Callable ABCs for classes that provide respectively the methods __contains__(), __hash__(), __len__(), and __call__(). At one point, I want to check if …

Total answers: 2

Python equivalent of Typescript interface

Python equivalent of Typescript interface Question: Recently I have been working with Typescript a lot, it allows to express things like: interface Address { street: string; housenumber: number; housenumberPostfix?: string; } interface Person { name: string; adresses: Address[] } const person: Person = { name: ‘Joe’, adresses: [ { street: ‘Sesame’, housenumber: 1 }, { …

Total answers: 8

Should you put quotes around type annotations in python

Should you put quotes around type annotations in python Question: What’s the difference between these two functions? I’ve seen people put quotes around type annotations and other times leave them out but I couldn’t find why people choose to use one or the other. def do_something(entity: Entity): pass def do_something(entity: ‘Entity’): pass Are there advantages …

Total answers: 2

Proper type annotation of Python functions with yield

Proper type annotation of Python functions with yield Question: After reading Eli Bendersky’s article on implementing state machines via Python coroutines I wanted to… see his example run under Python3 and also add the appropriate type annotations for the generators I succeeded in doing the first part (but without using async defs or yield froms, …

Total answers: 3

Python 3 and static typing

Python 3 and static typing Question: I didn’t really pay as much attention to Python 3’s development as I would have liked, and only just noticed some interesting new syntax changes. Specifically from this SO answer function parameter annotation: def digits(x:’nonnegative number’) -> “yields number’s digits”: # … Not knowing anything about this, I thought …

Total answers: 5