annotations

How to type annotate reduce function?

How to type annotate reduce function? Question: I’m writing a very thin wrapper on top of list and I want to define a method called reduce, but I’m struggling to annotate it properly such that pylance, mypy & pylint cut their complaints whenever I use the method, or even define it. I was perturbed to …

Total answers: 1

Extract annotations by layer from a PDF in Python

Extract annotations by layer from a PDF in Python Question: I have a PDF with annotations (markups) stored in different layers. Each layer has a specific name. I need to extract the annotations with their layer name. In particular, I’m interested only in the location of the annotation (as in, the bounding box of it) …

Total answers: 2

How do I read extended annotations from Annotated?

How do I read extended annotations from Annotated? Question: PEP 593 added extended annotations via Annotated. But neither the PEP nor the documentation for Annotated describes how we’re supposed to access the underlying annotations. How are we supposed to read the extra annotations stored in the Annotated object? from typing import Annotated class Foo: bar: …

Total answers: 2

Python typing dict of dicts

Python typing dict of dicts Question: How do I correctly type hint this python structure: people={ "john" : {"likes": "apples", "dislikes": "organges"}, "aisha": {"likes": "kittens", "dislikes": "ravens"} } EDIT: There can be any keys specified – e.g. "mary","joseph", "carl"… I understand that the value-dict can be typed as such class _Preferences(TypedDict): likes: str dislikes: str …

Total answers: 2

Type hints without value assignment in Python

Type hints without value assignment in Python Question: I was under the impression that typing module in Python is mostly for increasing code readability and for code documentation purposes. After playing around with it and reading about the module, I’ve managed to confuse myself with it. Code below works even though those two variables are …

Total answers: 2

Matplotlib annotations: x coordinate constant and y dynamic

Matplotlib annotations: x coordinate constant and y dynamic Question: Suppose this is the code: ax1 = plt.subplot2grid((1, 1), (0, 0)) x = range(100) y = range(100) ax1.annotate((y[-1]), (x[-1], y[-1]), bbox=dict(boxstyle=’larrow’, fc=’0.8′, ec=’None’)) The arrow is too close to the graph, I would like it more to the rightexample. The proposed solution is to add a …

Total answers: 1

Typing an unimported module

Typing an unimported module Question: I have a Python package that has an optional [extras] dependency, yet I want to adhere to typing on all methods. The situation is that in my file, I have this class MyClass: def __init__(self, datastore: Datastore): # <- Datastore is azureml.core.Datastore … def my_func(self): from azureml.core import Datastore … …

Total answers: 2

Python annotation's type for nested list with mix of numbers and lists

Python annotation's type for nested list with mix of numbers and lists Question: It is known that an annotation’s type for [[1, 2, 3], [4, 5, 6], [7, 8, 9]] is List[List[int]]. But what is the annotation’s type for nested list with mix of numbers and lists [[1, 1], 2, [1 , 1]]? Asked By: …

Total answers: 1

Which one is the correct way of using Python type hints with Django Models?

Which one is the correct way of using Python type hints with Django Models? Question: Which one of these is the correct way to use type annotations for django models? from typing import TypeVar, Generic from app.models import User _T = TypeVar("_T", bound=User) def func(user: Generic[_T]): user.name = ‘John’ user.save() Or from typing import Type …

Total answers: 1