*Memo:
- My post explains type hint (1).
- My post explains type hint (3).
- My post explains type hint (4).
- My post explains type hint (5).
- My post explains type hint (6).
- My post explains type hint (7).
assert_type() can ask a type checker to check if the static type of val is typ at pre-runtime as shown below:
*Memo:
- The 1st parameter is
val(Required-PO-Type:Any):- It's a value.
- The 2nd parameter is
typ(Required-PO-Type:Type):- It's a type.
- Nothing happens if the static type of
valistypwhile error occurs if the static type ofvalisn'ttyp. - At runtime, it gets the 1st argument.
# At pre-runtime
from typing import assert_type
v: int = 100
assert_type(v, int) # No error
assert_type(v, float) # Error
# At runtime
from typing import assert_type
v: int = 100
print(assert_type(v, int)) # 100
print(assert_type(v, float)) # 100
cast() can cast the static type of a value to other (or same) static type as shown below:
*Memo:
- The 1st parameter is
typ(Required-PO-Type:Any):- It's a type.
- The 2nd parameter is
val(Required-PO-Type:Type):- It's a value.
- At runtime, it gets the 1st argument.
# At pre-runtime
from typing import cast, reveal_type
v1: int = 100
v1 = 200 # No error
v1 = 'Hello' # Error
# `int` is casted to `str`.
v2 = cast(str, v1)
reveal_type(v2) # builtins.str
v2 = 200 # Error
v2 = 'Hello' # No error
# At runtime
from typing import cast
v: int = 100
print(cast(str, v)) # 100
At runtime, error doesn't occur with a wrong type hint as shown below:
v: int | list[float] = 'Hello'
# No error
print(v)
# Hello
complex accepts float, int and bool, float accepts int and bool and int accepts bool as shown below:
*Memo:
- These types behave like having the relationship of a supertype and subtype even though they actually don't have such a relationship except
intandbool:- I call such types supertype-like types and subtype-like types.
-
boolis a subtype ofintaccording to the doc.
-
PEP 484 explains the numeric tower of
complex,floatandint.
<complex>:
v: complex = 2.3+4.5j # complex
v = 2.3 # float
v = 23 # int
v = True # bool
# No error
<float>:
v: float = 2.3 # float
v = 23 # int
v = True # bool
# No error
v = 2.3+4.5j # complex
# Error
<int>:
v: int = 23 # int
v = True # bool
# No error
v = 2.3+4.5j # complex
v = 2.3 # float
# Error
<bool>:
v: bool = True # bool
# No error
v = 2.3+4.5j # complex
v = 2.3 # float
v = 23 # int
# Error
Any and object can be used to accept all types as shown below:
*Memo:
-
Anycan accept and behave like all types becauseAnyis the special type to indicate an unconstrained type:- Type checkers ignore
Anyso it's equivalent to no type hint. - We can say that
Anyis a supertype-like type of all other types.
- Type checkers ignore
-
objectcan accept all types but cannot behave like all types exceptobjectbecauseobjectis one specific type which is a supertype of all other types:- Type checkers check
objectas one specific type.
- Type checkers check
- Use
Anyandobjectif using more specific type is impossible:- Use
objectprior toAnybecauseobjectis more specific.
- Use
<Any>:
from typing import Any
v: Any = 'Hello'
def func(x: Any = None) -> Any:
pass
from typing import Any
v: Any = 'Hello' # `Any` behaves like `str`.
print(v[2]) # No error
print(v.upper()) # No error
print(v + ' World') # No error
<object>:
v: object = 'Hello'
def func(x: object = None) -> object:
pass
v: object = 'Hello' # `object` behaves like `object`.
print(v[2]) # Error
print(v.upper()) # Error
print(v + ' World') # Error
Top comments (0)