annotations

How to annotate Django view's methods?

How to annotate Django view's methods? Question: I’d like to use Python type hints in my Django project. What’s the proper way to annotate get/post methods of a simple class-based view in Django? I’ve searched the Django code itself but it doesn’t seem to contain any type hints. Asked By: planetp || Source Answers: [UPDATE …

Total answers: 3

Optional[Type[Foo]] raises TypeError in Python 3.5.2

Optional[Type[Foo]] raises TypeError in Python 3.5.2 Question: This code: #!/usr/bin/env python from typing import Optional, Type class Foo(object): pass class Bar(Foo): pass def test_me() -> Optional[Type[Foo]]: print(“Hi there!”) return Bar if __name__ == “__main__”: test_me() will raise TypeError on 3.5.2: Traceback (most recent call last): File “./test.py”, line 11, in <module> def test_me() -> Optional[Type[Foo]]: …

Total answers: 3

Does Python type hint (annotations) cause some run-time effects?

Does Python type hint (annotations) cause some run-time effects? Question: Can Python function annotations and type hints (PEP 3107 and PEP 484) cause some run-time effects? Could it made the code faster? Or shrink the usage of memory? Or otherwise it would make code more slow? Asked By: Y.N || Source Answers: According to the …

Total answers: 2

How to annotate a type that's a class object (instead of a class instance)?

How to annotate a type that's a class object (instead of a class instance)? Question: What is the proper way to annotate a function argument that expects a class object instead of an instance of that class? In the example below, some_class argument is expected to be a type instance (which is a class), but …

Total answers: 1

void return type annotation

Python void return type annotation Question: In python 3.x, it is common to use return type annotation of a function, such as: def foo() -> str: return "bar" What is the correct annotation for the "void" type? I’m considering 3 options: def foo() -> None: not logical IMO, because None is not a type, def …

Total answers: 2

How to extract tag offsets in xml document using Python BeautifulSoup

How to extract tag offsets in xml document using Python BeautifulSoup Question: I need some help finding the text offset of certain tags in an XML document. I have a data set following the format illustrated below where the ROOT element contains several RECORDs though each RECORD contains only one TEXT element. In the text …

Total answers: 2

How to annotate a generator in python3?

How to annotate a generator in python3? Question: Python 3.x supports (optional) function annotations: def add_ints(x:int, y:int) -> int : return x+y I sometimes encounter problems as to how to represent a given “type” can be represented, and this time, I have a function that returns a generator: def myfunc(x: [int]) -> “generator that returns …

Total answers: 2

putting current class as return type annotation

putting current class as return type annotation Question: In python 3 I can make arguments and return type annotations. Example: class Graph: def __init__(self, V: int, E: int, edges: list): pass @classmethod def fromfile(cls, readobj: type(sys.stdin)): pass def V(self) -> int: pass def E(self) -> int: pass The problem is I can’t make an annotation …

Total answers: 2

What does -> mean in Python function definitions?

What does -> mean in Python function definitions? Question: I’ve recently noticed something interesting when looking at Python 3.3 grammar specification: funcdef: ‘def’ NAME parameters [‘->’ test] ‘:’ suite The optional ‘arrow’ block was absent in Python 2 and I couldn’t find any information regarding its meaning in Python 3. It turns out this is …

Total answers: 11

How to space overlapping annotations

How to space overlapping annotations Question: I want to annotate the bars in a graph with some text but if the bars are close together and have comparable height, the annotations are above ea. other and thus hard to read (the coordinates for the annotations were taken from the bar position and height). Is there …

Total answers: 4