Skip to content

tocodeil/python-type-hints-examples

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Type Hints In Python

1. Basic Type Hints

2. Generic Types

  • Generic lists

    • list[int], list[str], list[int|str|bool]
  • Generic Dictionaries

    • dict[str, int], dict[Any, int]
  • TypedDict

    • use only specific keys in dict
    • Literal
    • NotRequired

3. Types & Functions

  • Simple input/output type hints def x(a: str, b: int) -> list[int]: pass

  • Typing *args

  • Typing **kwargs

  • Default args

  • Generator Functions

  • Warning: Type Hints and Built-in functions

4. Types And Object Oriented Programming

  • Every class is a type

  • Protocols

  • Type Hints for Recursive Structures

class Node: value: int left: Node right: Node

def dfs(): pass

n = Node(10) n.right = Node(12) n.right.right = Node(15) n.right.left = Node(9) n.dfs()

5. Type Vars

  • Unsolved Identity Problem

  • Hello TypeVar

  • Hello ParamSpec

  • Types for Decorators

6. User Defined Type Guards

  • Built-in Type Guards

  • Creating Your Own Type Guard

  • casting

7. NewType and Type Aliases

  • Creating type aliases

  • Defining new types with NewType

About

Example files for the Python Type Hints mini course at: https://www.tocode.co.il/boosters/13

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages