*Memo:
- My post explains type hints (1).
- My post explains type hints (3).
- My post explains type hints (4).
- My post explains type hints (5).
- My post explains type hints (6).
- My post explains type hints (7).
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(superclass) and subtype(subclass) even though they actually don't except
intandbool:-
boolis the subclass 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
Setting multiple types to a type hint narrows(limits) the usage of object attributes as shown below:
<str | list[str]>:
*Memo:
- Both
strandlisthaveindex()with one required and two optional parameters soindex()with one, two or three arguments works. - Both
strandlisthavecount()butstr.count()has one required and two optional parameters whilelist.count()has one required parameter socount()with one argument works. - Only
strhasupper()soupper()doesn't work.
v: str | list[str] = 'hello world'
print(v.index('rl'))
print(v.index('rl', 6))
print(v.index('rl', 6, 11))
print(v.count('ll'))
# No error
print(v.count('l', 0))
print(v.count('l', 0, 5))
print(v.upper())
# Error
v = ['A', 'B', 'C', 'D', 'E']
# No error
<Cls1 | Cls2>:
*Memo:
- Both
Cls1andCls2haveadd()butstr.add()hasxandyrequired andzoptionalfloatparameters whilelist.add()hasaandyrequiredintparameters soadd()with two positional or one positional andykeywordintarguments works. - Only
Cls1hasPIandsub()soPIandsub()don't work.
class Cls1:
PI: float = 3.14
def add(self, x:float, y:float, z:float = 4) -> float:
return x+y+z
def sub(self, x:float, y:float) -> float:
return x-y
class Cls2:
def add(self, a:int, y:int) -> int:
return a+y
cls1: Cls1 | Cls2 = Cls1()
print(cls1.add(5, 2))
print(cls1.add(5, y=2))
# No error
print(cls1.add(x=5, y=2))
print(cls1.add(5, 2, 6))
print(cls1.add(5.3, 2.8))
print(cls1.PI)
print(cls1.sub(5, 2))
# Error
Using --strict, the variable without a type hint doesn't get error while the function without type hints gets the errors as shown below:
*Memo:
- Error doesn't occur whether
selfhas a type hint or not in the method which is the function within a class. - The reason why the variable without a type hint doesn't get error is because always needing to type-hint a variable is too strict and too much, needing to write more code, making code complicated.
- Not using
--strict, both the variable and function without type hints don't get error.
v = 'Hello'
# No error
def func(x):
return x
# error: Function is missing a type annotation
def func(x) -> str:
return x
# error: Function is missing a type annotation for one or more arguments
def func(x: str):
return x
# error: Function is missing a return type annotation
class MyCls:
# ↓↓↓↓↓↓↓↓↓↓↓ With a type hint
def func1(self: MyCls) -> None:
return None
# ↓↓↓↓ Without a type hint
def func2(self) -> None:
return None
# No error
Top comments (0)