Skip to content

Instantly share code, notes, and snippets.

View thejaminator's full-sized avatar

James Chua thejaminator

View GitHub Profile
@thejaminator
thejaminator / showcase.py
Last active April 16, 2024 09:51
Pydantic - How to differentiate between types that look the same
from typing import Literal
from pydantic import BaseModel
class SomethingThatHasAName(BaseModel):
name: str
class User(SomethingThatHasAName):
...
class Product(SomethingThatHasAName):
...
@thejaminator
thejaminator / typesafety.md
Last active February 4, 2023 10:18
Avoiding nested if statements for type safety

It is very common to reference a variable that has not been created yet. It is known as a UnboundLocalError

Style 1 - Nested ifs

from typing import Optional
def some_db_call(arg: int) -> str:
    return "some_db_call"

def func(arg: Optional[int]) -> int:
 if arg: