DEV Community

Super Kai (Kazuya Ito)
Super Kai (Kazuya Ito)

Posted on • Edited on

Type hint in Python (2)

Buy Me a Coffee

*Memo:

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 val is typ while error occurs if the static type of val isn't typ.
  • 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
Enter fullscreen mode Exit fullscreen mode
# At runtime

from typing import assert_type

v: int = 100
print(assert_type(v, int))   # 100
print(assert_type(v, float)) # 100
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode
# At runtime

from typing import cast

v: int = 100
print(cast(str, v)) # 100
Enter fullscreen mode Exit fullscreen mode

At runtime, error doesn't occur with a wrong type hint as shown below:

v: int | list[float] = 'Hello'
# No error

print(v)
# Hello
Enter fullscreen mode Exit fullscreen mode

complex accepts float, int and bool, float accepts int and bool and int accepts bool as shown below:

*Memo:

<complex>:

v: complex = 2.3+4.5j # complex
v = 2.3               # float
v = 23                # int
v = True              # bool
# No error
Enter fullscreen mode Exit fullscreen mode

<float>:

v: float = 2.3 # float
v = 23         # int
v = True       # bool
# No error

v = 2.3+4.5j   # complex
# Error
Enter fullscreen mode Exit fullscreen mode

<int>:

v: int = 23  # int
v = True     # bool
# No error

v = 2.3+4.5j # complex
v = 2.3      # float
# Error
Enter fullscreen mode Exit fullscreen mode

<bool>:

v: bool = True # bool
# No error

v = 2.3+4.5j   # complex
v = 2.3        # float
v = 23         # int
# Error
Enter fullscreen mode Exit fullscreen mode

Any and object can be used to accept all types as shown below:

*Memo:

  • Any can accept and behave like all types because Any is the special type to indicate an unconstrained type:
    • Type checkers ignore Any so it's equivalent to no type hint.
    • We can say that Any is a supertype-like type of all other types.
  • object can accept all types but cannot behave like all types except object because object is one specific type which is a supertype of all other types:
    • Type checkers check object as one specific type.
  • Use Any and object if using more specific type is impossible:
    • Use object prior to Any because object is more specific.

<Any>:

from typing import Any

v: Any = 'Hello'

def func(x: Any = None) -> Any:
    pass
Enter fullscreen mode Exit fullscreen mode
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
Enter fullscreen mode Exit fullscreen mode

<object>:

v: object = 'Hello'

def func(x: object = None) -> object:
    pass
Enter fullscreen mode Exit fullscreen mode
v: object = 'Hello' # `object` behaves like `object`.

print(v[2])         # Error
print(v.upper())    # Error
print(v + ' World') # Error
Enter fullscreen mode Exit fullscreen mode

Top comments (0)