Type hinting binds function arguments to specific objects or strongly types them.
Questions tagged [type-hinting]
2326 questions
1188
votes
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 +…
![](../../users/profiles/4665133.webp)
Michael van Gerwen
- 12,011
- 3
- 11
- 8
653
votes
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:
...
![](../../users/profiles/3006737.webp)
Yahya Uddin
- 26,997
- 35
- 140
- 231
652
votes
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…
![](../../users/profiles/1383785.webp)
josh
- 9,656
- 4
- 34
- 51
525
votes
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.
![](../../users/profiles/1458283.webp)
Jon
- 11,356
- 5
- 40
- 74
469
votes
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…
![](../../users/profiles/1128289.webp)
Praxeolitic
- 22,455
- 16
- 75
- 126
391
votes
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…
![](../../users/profiles/447967.webp)
exfizik
- 5,351
- 4
- 23
- 26
365
votes
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 :…
![](../../users/profiles/6373357.webp)
jacobcan118
- 7,797
- 12
- 50
- 95
361
votes
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…
![](../../users/profiles/712605.webp)
Richard Hansen
- 51,690
- 20
- 90
- 97
347
votes
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…
![](../../users/profiles/1934182.webp)
Vaulstein
- 20,055
- 8
- 52
- 73
325
votes
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:
main.py file:
import mymixin.py
class Main(object, MyMixin):
def func1(self, xxx):
...
mymixin.py…
![](../../users/profiles/1760858.webp)
velis
- 8,747
- 4
- 44
- 64
307
votes
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…
![](../../users/profiles/1294495.webp)
Eric W.
- 7,148
- 3
- 20
- 27
291
votes
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')
![](../../users/profiles/1709587.webp)
Mark Amery
- 143,130
- 81
- 406
- 459
267
votes
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:
…
![](../../users/profiles/1019455.webp)
joe
- 8,383
- 13
- 61
- 109
264
votes
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…
![](../../users/profiles/1137187.webp)
Tregoreg
- 18,872
- 15
- 48
- 69
253
votes
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):
…
![](../../users/profiles/7190191.webp)
grepcake
- 3,960
- 3
- 16
- 26