32
loading...
This website collects cookies to deliver better user experience
$ pip install mypy
Usage:
$ mypy /../Path/your_file.py
int x = “hello”
, it’s going to crash.x = 1
.x: str = 1
, this is not enforcing that x
store a string, it means that x
should store one. Therefore, it won’t crash, because it’s simply documentation — doesn’t change the code behavior.x: str = 1
it returns:error: Incompatible types in assignment (expression has type "int", variable has type "str")
Found 1 error in 1 file (checked 1 source file)
def something(numb: float) -> int:
return 2021 + numb
OR if you don't return anything, just use None
def person(name: str, age: int, height: float) -> None:
print(f'{name} is {age} yo and {height} feet tall.')
numb: float
means it should receive a float and -> int
should return an int.[[1, 2], [3, 4], [5, 6]]
x: list[list[int] = []
No no no... This doesn't work!
>>> TypeError: 'type' object is not subscriptable
from typing import List
x: List[List[int] = [[1, 2], 3, 4]] with capital L
$ mypy ../this_file.py
>>> Success: no issues found in 1 source file.
And now it's a valid type.
from typing import Dict
x: Dict[str: str] = {"State": "Capital"} with capital C
from typing import Set
x: Set[str] = {"a", "b", "c"} with capital C
from typing import List, Dict, Set
Vector = List[float]
def foo(v: Vector) -> Vector:
print(v)
Autocomplete would be: foo(v: List[float]) -> List[float]
Where there’s Vector, List[float] will replace it.
Vectors = List[Vector] (List[List[float]])
def foo(v: Vectors) -> Vector:
pass
Autocomplete would be: foo(v: List[List[float]]) -> List[float]
def foo(output: bool=False):
pass
from typing import Optional
def foo(output: Optional[bool]=False):
pass
from typing import Any
def foo(output: Any):
pass
Writing this is the exact same thing as just doing def foo(output)
from typing import Sequence
def foo(seq: Sequence[str]):
pass
foo(("a", "b", "c"))
foo(["a", "b", "c"])
foo("hello") ("Strings are immutable sequences of Unicode")
$ mypy ../this_file.py
>>> Success: no issues found in 1 source file.
But if you give an *int* or a *set*:
foo(1)
foo({1, 2, 3})
str: "anything that can be indexed"
$ mypy ../this_file.py
>>> error: Argument 1 to "foo" has incompatible type "int"; expected "Sequence"
Found 1 error in 1 file (checked 1 source file)
from typing import Tuple
x: Tuple[int] = (1, 2, 3)
$ mypy ../this_file.py
>>> error: Incompatible types in assignment (expression has type "Tuple[int, int, inti]", variable ha type "Tuple[int]
Found 1 error in 1 file (checked 1 source file)
x: Tuple[int, int, int] = (1, 2, 3)
y: Tuple[int, str, int] = (5, "hello", 10)
$ mypy ../this_file.py
>>> Success: no issues found in 1 source file.
from typing import Callable
|parameters|return|
def foo(func: Callable[[int, int], int] -> None:
func(1, 3)
This function has to have two *int* parameters and return an int
def add(x: int, y: int) -> int:
return x + y
def foo(func: Callable[[int, int], int]) -> None:
func(1, 3)
foo(add)
$ mypy ../this_file.py
>>> Success: no issues found in 1 source file.
But if we remove or add one parameter, we get an error because this function doesn't have the correct parameters inside.
def foo() -> Callable[[int, int], int]:
def add(x: int, y: int) -> int:
return x + y
return add
foo()
OR with lambda
# normal
def foo() -> Callable[[int, int], int]:
return lambda x, y: x + y
# variables specified
def foo() -> Callable[[int, int], int]:
func: Callable[[int, int], int] = lambda x, y: x + y
return func
foo()