annotations

Python type annotations: Proper way to annotate functions returning library object

Python type annotations: Proper way to annotate functions returning library object Question: What is the proper way to annotate a type function returns in this code? from requests import Request, Session def make_request(method: str, url: str) -> ??? : # Response object will be returned request = Request(method, url).prepare() session = Session() r = session.send(request) …

Total answers: 2

Set location of annotation on string-axis

Set location of annotation on string-axis Question: I need to add annotations over last three data points on my plot. Below list x2 is used as x-axis which a string list. List x2 is a year-month format converted from list x1. My question is how to adjust the x location of my annotation in string-axis …

Total answers: 1

Problem when using patchify library to create patches

Problem when using patchify library to create patches Question: I am using the patchify library to create patches of a bigger .jpg image. I am using the following code, taken from this YT video: https://www.youtube.com/watch?v=7IL7LKSLb9I&ab_channel=DigitalSreeni When the YT guy reads his images (12 tiff images) he gets the following size for the large_image_stack variable: (12, …

Total answers: 3

Why does PyMupdf Document show the error, no attribute 'new_page', when it is a PDF?

Why does PyMupdf Document show the error, no attribute 'new_page', when it is a PDF? Question: I’m working on annotating a PDF and I want to change its color. I was guided to this helpful link: https://pymupdf.readthedocs.io/en/latest/faq.html#how-to-add-and-modify-annotations I used the code in the link: # -*- coding: utf-8 -*- """ ——————————————————————————- Demo script showing how …

Total answers: 2

How to annotate points in a scatterplot based on a pandas column

How to annotate points in a scatterplot based on a pandas column Question: Wanted ‘Age’ as the x-axis, ‘Pos’ as the y-axis and labels as ‘Player’ Names. But for some reason, not able to do label the points. Code: import numpy as np import matplotlib.pyplot as plt import pandas as pd import adjustText as at …

Total answers: 1

Python return type annotations

Python return type annotations Question: When declaring a function, should we use return annotations? def generate_nine() -> int: return 9 Or can we simply put something like this: def generate_nine(): return 9 Asked By: Juan Alegría || Source Answers: In more detail, Python 2.x has docstrings, which allow you to attach a metadata string to …

Total answers: 2

from __future__ import annotations

from __future__ import annotations Question: Python doc __future__ In python doc about __future__ there is a table below where it shows that annotations "optional in" 3.7.0b1 and "mandatory in" 4.0 but I am still able to use annotations in 3.8.2 without importing annotations then what is the use of it. >>> def add_int(a:int, b:int) -> …

Total answers: 2

collections.Iterable vs typing.Iterable in type annotation and checking for Iterable

collections.Iterable vs typing.Iterable in type annotation and checking for Iterable Question: I found that in Python both collections.Iterable and typing.Iterable can be used in type annotation and checking for whether an object is iterable, i.e., both isinstance(obj, collections.Iterable) and isinstance(obj, typing.Iterable) works. My question is, what are the differences among them? And which one is …

Total answers: 1

How to add a dataclass field without annotating the type?

How to add a dataclass field without annotating the type? Question: When there is a field in a dataclass for which the type can be anything, how can you omit the annotation? @dataclass class Favs: fav_number: int = 80085 fav_duck = object() fav_word: str = ‘potato’ It seems the code above doesn’t actually create a …

Total answers: 4

Check a variable against Union type at runtime in Python 3.6

Check a variable against Union type at runtime in Python 3.6 Question: I’m trying to write a function decorator that uses Python 3.6 type hints to check that a dictionary of arguments respects the type hints and if not raise an error with a clear description of the problem, to be used for HTTP APIs. …

Total answers: 5