#Python-Intermediate
#Python
Type Checking/Annotations in Python
- Type Annotations are a new feature added in
PEP 484
that allow for adding type hints
to variables.
- They are used to
inform
someone reading the code what the type of a variable
should be.
- This brings a sense of statically typed control to the dynamically typed Python
- Usage
- Annotate Argument
- Annotate Return type
- Annotate Variable
- Benefits
- Makes the code
easier to read
for you and your team,
- Encourages you to have types in mind,
- Helps to
identify type-related issues
,
- Enables proper type checking.
- Python interpreter actually does not automatically conduct any type checking whatsoever.
- That means those
annotations have no effects during runtime
, even a ‘wrong’ type is passed
- With annotation,
the IDE is aware
of the type of the data object
- And gives warning if type is miss-matched
Basic
Without Annotation
def midrange(data):
return (max(data) + min(data)) / 2
print(mr([1, 4, 8]))
print(mr({1: 1, 2: 4, 3: 8}))
With Annotation
def midrange(data) -> float:
return 'Amrit'
print(midrange({1: 1, 2: 4, 3: 8}))
def midrange(data: list):
return (max(data) + min(data)) / 2
print(midrange({1: 1, 2: 4, 3: 8}))
Advance
from typing import List, TypeVar, Union, cast
def fib_list(n: int = 0) -> List[int]:
fib_numbers: List[int] = [0, 1]
for _ in range(n):
fib_numbers.append(fib_numbers[-1] + fib_numbers[-2])
fib_numbers = "Amrit"
return fib_numbers
print(f"fib_list(10) = {fib_list(10)}")
T = TypeVar('T')
A = TypeVar('A', str, bytes)
def upcase(s: Union[str, bytes]) -> Union[str, bytes]:
if isinstance(s, str):
return s.upper()
elif isinstance(s, bytes):
print('byte')
return bytes(x - 0x20 if 0x61 <= x <= 0x7A else x for x in s)
else:
raise TypeError("need str or bytes")
print(upcase(b"amrit"))
def type_cast():
value = [130]
value_float = cast(List[int], value)
value.append("string")
value_float.append("string")
Reference