Questions tagged [type-hinting]

Type hinting binds function arguments to specific objects or strongly types them.

2326 questions
9 answers

How do I type hint a method with the type of the enclosing class?

I have the following code in Python 3: class Position: def __init__(self, x: int, y: int): self.x = x self.y = y def __add__(self, other: Position) -> Position: return Position(self.x + other.x, self.y +…
Michael van Gerwen
  • 12,011
  • 3
  • 11
  • 8
4 answers

How to specify multiple return types using type-hints

I have a function in python that can either return a bool or a list. Is there a way to specify the return types using type hints? For example, is this the correct way to do it? def foo(id) -> list or bool: ...
Yahya Uddin
  • 26,997
  • 35
  • 140
  • 231
3 answers

How do I add default parameters to functions when using type hinting?

If I have a function like this: def foo(name, opts={}): pass And I want to add type hints to the parameters, how do I do it? The way I assumed gives me a syntax error: def foo(name: str, opts={}: dict) -> str: pass The following doesn't throw…
  • 9,656
  • 4
  • 34
  • 51
4 answers

How can I specify the function type in my type hints?

How can I specify the type hint of a variable as a function type? There is no typing.Function, and I could not find anything in the relevant PEP, PEP 483.
  • 11,356
  • 5
  • 40
  • 74
8 answers

Type annotations for *args and **kwargs

I'm trying out Python's type annotations with abstract base classes to write some interfaces. Is there a way to annotate the possible types of *args and **kwargs? For example, how would one express that the sensible arguments to a function are…
  • 22,455
  • 16
  • 75
  • 126
2 answers

How to specify "nullable" return type with type hints

Suppose I have a function: def get_some_date(some_argument: int=None) -> %datetime_or_None%: if some_argument is not None and some_argument == 1: return datetime.utcnow() else: return None How do I specify the return type…
  • 5,351
  • 4
  • 23
  • 26
5 answers

How should I use the Optional type hint?

I'm trying to understand how to use the Optional type hint. From PEP-484, I know I can use Optional for def test(a: int = None) either as def test(a: Union[int, None]) or def test(a: Optional[int]). But how about following examples? def test(a :…
  • 7,797
  • 12
  • 50
  • 95
1 answer

How to annotate types of multiple return values?

How do I use type hints to annotate a function that returns an Iterable that always yields two values: a bool and a str? The hint Tuple[bool, str] is close, except that it limits the return value type to a tuple, not a generator or other type of…
Richard Hansen
  • 51,690
  • 20
  • 90
  • 97
4 answers

What are type hints in Python 3.5?

One of the most talked-about features in Python 3.5 is type hints. An example of type hints is mentioned in this article and this one while also mentioning to use type hints responsibly. Can someone explain more about them and when they should be…
  • 20,055
  • 8
  • 52
  • 73
7 answers

Python type hinting without cyclic imports

I'm trying to split my huge class into two; well, basically into the "main" class and a mixin with additional functions, like so: file: import class Main(object, MyMixin): def func1(self, xxx): ...…
  • 8,747
  • 4
  • 44
  • 64
5 answers

Type hinting a collection of a specified type

Using Python 3's function annotations, it is possible to specify the type of items contained within a homogeneous list (or other collection) for the purpose of type hinting in PyCharm and other IDEs? A pseudo-python code example for a list of…
Eric W.
  • 7,148
  • 3
  • 20
  • 27
3 answers

Type hint for a file or file-like object?

Is there any correct type hint to use for a file or file-like object in Python? For example, how would I type-hint the return value of this function? def foo() -> ???: return open('bar')
Mark Amery
  • 143,130
  • 81
  • 406
  • 459
4 answers

Difference between defining typing.Dict and dict?

I am practicing using type hints in Python 3.5. One of my colleague uses typing.Dict: import typing def change_bandwidths(new_bandwidths: typing.Dict, user_id: int, user_name: str) -> bool: …
  • 8,383
  • 13
  • 61
  • 109
2 answers

Python void return type annotation

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…
  • 18,872
  • 15
  • 48
  • 69
5 answers

How do I annotate types in a for-loop?

I want to annotate a type of a variable in a for-loop. I tried this but it didn't work: for i: int in range(5): pass What I expect is working autocomplete in PyCharm 2016.3.2, but using pre-annotation didn't work: i: int for i in range(5): …
  • 3,960
  • 3
  • 16
  • 26
2 3
99 100