3.2. Typing Basic

3.2.1. Int

  • Used to inform static type checker that the variable should be int

Declaration:

>>> data: int
>>> data: int = 1
>>> data: int = -1

Example:

>>> data: int
>>>
>>> data = 1        # ok
>>> data = -1       # ok
>>> data = 'hello'  # error

3.2.2. Float

  • Used to inform static type checker that the variable should be float

Declaration:

>>> data: float
>>> data: float = 0.0
>>> data: float = 1.23
>>> data: float = -1.23

Example:

>>> data: float
>>>
>>> data = 1.0        # ok
>>> data = -1.0       # ok
>>> data = 'hello'    # error

3.2.3. Str

  • Used to inform static type checker that the variable should be str

Declaration:

>>> data: str
>>> data: str = ''
>>> data: str = 'hello'

Example:

>>> data: str
>>>
>>> data = 'Mark'           # ok
>>> data = 'Watney'         # ok
>>> data = 'Mark Watney'    # ok

3.2.4. Bool

  • Used to inform static type checker that the variable should be bool

Declaration:

>>> data: bool
>>> data: bool = True
>>> data: bool = False

Example:

>>> data: bool
>>>
>>> data = True     # ok
>>> data = False    # ok
>>> data = None     # error

3.2.5. None

  • Used to inform static type checker that the variable should be None

Declaration:

>>> data: None
>>> data: None = None

Example:

>>> data: None
>>>
>>> data = True     # error
>>> data = False    # error
>>> data = None     # ok

3.2.6. Errors

  • Types are not Enforced

  • This code will run without any problems

  • Types are not required, and never will be

  • Although mypy, pyre-check or pytypes will throw error

>>> name: int = 'Mark Watney'

3.2.7. Use Case - 0x01

>>> firstname: str = 'Mark'
>>> lastname: str = 'Watney'
>>> age: int = 40
>>> adult: bool = True

3.2.8. Assignments

Code 3.26. Solution
"""
* Assignment: Typing Basic Int
* Complexity: easy
* Lines of code: 3 lines
* Time: 2 min

English:
    1. Declare proper types for variables
    2. Run doctests - all must succeed

Polish:
    1. Zadeklaruj odpowiedni typ zmiennych
    2. Uruchom doctesty - wszystkie muszą się powieść

Tests:
    >>> import sys; sys.tracebacklimit = 0

    >>> assert a == 1, \
    'Do not modify variable `a` value, just add type annotation'
    >>> assert b == 0, \
    'Do not modify variable `b` value, just add type annotation'
    >>> assert c == -1, \
    'Do not modify variable `c` value, just add type annotation'
"""

# Declare proper types for variables
a: ... = 1
b: ... = 0
c: ... = -1

Code 3.27. Solution
"""
* Assignment: Typing Basic Float
* Complexity: easy
* Lines of code: 3 lines
* Time: 2 min

English:
    1. Declare proper types for variables
    2. Run doctests - all must succeed

Polish:
    1. Zadeklaruj odpowiedni typ zmiennych
    2. Uruchom doctesty - wszystkie muszą się powieść

Tests:
    >>> import sys; sys.tracebacklimit = 0

    >>> assert a == 1.0, \
    'Do not modify variable `a` value, just add type annotation'
    >>> assert b == 0.0, \
    'Do not modify variable `b` value, just add type annotation'
    >>> assert c == -1.0, \
    'Do not modify variable `c` value, just add type annotation'
"""

# Declare proper types for variables
a: ... = 1.0
b: ... = 0.0
c: ... = -1.0


Code 3.28. Solution
"""
* Assignment: Typing Basic Logic
* Complexity: easy
* Lines of code: 3 lines
* Time: 2 min

English:
    1. Declare proper types for variables
    2. Run doctests - all must succeed

Polish:
    1. Zadeklaruj odpowiedni typ zmiennych
    2. Uruchom doctesty - wszystkie muszą się powieść

Tests:
    >>> import sys; sys.tracebacklimit = 0

    >>> assert a == True, \
    'Do not modify variable `a` value, just add type annotation'
    >>> assert b == False, \
    'Do not modify variable `b` value, just add type annotation'
    >>> assert c == None, \
    'Do not modify variable `c` value, just add type annotation'
"""

# Declare proper types for variables
a: ... = True
b: ... = False
c: ... = None

Code 3.29. Solution
"""
* Assignment: Typing Basic Str
* Complexity: easy
* Lines of code: 3 lines
* Time: 2 min

English:
    1. Declare proper types for variables
    2. Run doctests - all must succeed

Polish:
    1. Zadeklaruj odpowiedni typ zmiennych
    2. Uruchom doctesty - wszystkie muszą się powieść

Tests:
    >>> import sys; sys.tracebacklimit = 0

    >>> assert a == '', \
    'Do not modify variable `a` value, just add type annotation'
    >>> assert b == 'abc', \
    'Do not modify variable `b` value, just add type annotation'
    >>> assert c == '123', \
    'Do not modify variable `c` value, just add type annotation'
"""

# Declare proper types for variables
a: ... = ''
b: ... = 'abc'
c: ... = '123'