type-hinting

Pipe notation for more than two types in a type hint

Pipe notation for more than two types in a type hint Question: I am trying: def foo(x: int | float | str): pass foo(0) and get the error: TypeError: unsupported operand type(s) for |: ‘type’ and ‘type’ Is it possible to use more than two types with pipe notation or I have to write Union? …

Total answers: 1

Forward function signature in VSCode

Forward function signature in VSCode Question: Say I have a function in src/f1.py with the following signature: def my_func(a1: int, a2: bool) -> float: … In a separate file src/f2.py I create a dictionary: from src.f1 import my_func my_dict = { "func": my_func } In my final file src/test1.py I call the function: from src.f2 …

Total answers: 1

How to find type hints for psycopg2 functions

How to find type hints for psycopg2 functions Question: How can one find what type hints to use when annotating my python code for to package functions eg. What will thepsycopg2.connect return so that I can put it in place of ??? eg: def sql_connect(sql_config: dict = None) -> ???: db = psycopg2.connect( host=sql_config["host"], port=sql_config["port"], …

Total answers: 1

Typehint Union Dictionary branching error

Typehint Union Dictionary branching error Question: I want to implement a function like the one below, but it throws a type hint warning. def test(flag: bool)->Dict[str, int]| Dict[str, str]: a: Dict[str, str]| Dict[str, int] = {} if flag: a[‘a’] = 1 else: a[‘a’] = ‘hello’ return post_process(a) With the following warning by Pylance: Argument of …

Total answers: 1

How do I type the `__prepare__` method for a metaclass?

How do I type the `__prepare__` method for a metaclass? Question: I’m trying to write a simple metaclass that intercepts every function declaration and replaces it with a dummy function: from dataclasses import dataclass from typing import Any, Mapping @dataclass class DummyCall: args: tuple[Any, …] kwargs: dict[str, Any] def _dummy_function(*args: Any, **kwargs: Any) -> DummyCall: …

Total answers: 1

Using typeguard decorator: @typechecked in Python, whilst evading circular imports?

Using typeguard decorator: @typechecked in Python, whilst evading circular imports? Question: Context To prevent circular imports in Python when using type-hints, one can use the following construct: # controllers.py from __future__ import annotations from typing import TYPE_CHECKING if TYPE_CHECKING: from models import Book class BookController: def __init__(self, book: "Book") -> None: self.book = book Where …

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

"at" sign (@) in Python type hints (suggested by Pylance / Pyright)

"at" sign (@) in Python type hints (suggested by Pylance / Pyright) Question: The July 2022 release of the Python extension for Visual Studio Code introduced "Inlay Type Hints", which automatically suggests return types of functions that don’t have an explicit annotation. To enable it, you can set "python.analysis.inlayHints.functionReturnTypes": true to your IDE user settings …

Total answers: 1

How to type hint pandas.NA as a possible output

How to type hint pandas.NA as a possible output Question: I have Pandas lambda function which I use with .apply. This function will output a dictionary with string keys and values that are either strings or pd.NA. When I try to type hint the function I get an error: def _the_function(x: str) -> dict[str, str …

Total answers: 1