Type Hints Flashcards

(12 cards)

1
Q

What are type hints in Python?

A

Optional annotations that describe expected data types to improve readability, debugging, and tooling (not enforced at runtime).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Common basic type hints?

A

int, float, str, bool

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is Optional[T]?

A

Value can be type T or None
πŸ‘‰ Optional[T] = Union[T, None]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is Union?

A

Allows multiple possible types
πŸ‘‰ Union[int, str]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Difference between List, Dict, Tuple?

A

List[T] β†’ sequence of same type
Dict[K, V] β†’ key-value pairs
Tuple[T1, T2] β†’ fixed structure and length

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Why are type hints useful?

A

Better readability, fewer bugs, IDE support, scalable code.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is TypeVar?

A

A way to write generic functions that preserve input type in output.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What does TypeVar guarantee?

A

Input type = Output type (type consistency without restriction).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is a Protocol?

A

Defines required behavior (methods), not exact type.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How does Protocol work?

A

Any object with required methods is accepted (no inheritance needed).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Where are type hints used in real software?

A

APIs (FastAPI), data pipelines, large backend systems, interfaces (Protocol).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Ultra-Short Revision
Type hints β†’ documentation + safety
Optional β†’ maybe None
Union β†’ multiple types
List/Dict/Tuple β†’ structured data
TypeVar β†’ same type in/out
Protocol β†’ behavior-based typing

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly