Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions python-annotations/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Python 3.14 Preview: Lazy Annotations

This folder contains sample code for the Real Python tutorial [Python 3.14 Preview: Lazy Annotations](https://realpython.com/python-annotations/).
9 changes: 9 additions & 0 deletions python-annotations/calculator.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
type Number = int | float

class Calculator:
history: list[Number]

def __init__(self) -> None: ...
def add(self, a: Number, b: Number) -> Number: ...

def add(a: Number, b: Number) -> Number: ...
9 changes: 9 additions & 0 deletions python-annotations/fib.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from typing import Annotated


def fib(n: int) -> int:
return n if n < 2 else fib(n - 2) + fib(n - 1)


def increment(x: Annotated[int, fib(35)]) -> int:
return x + 1
15 changes: 15 additions & 0 deletions python-annotations/forward_references_future.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from __future__ import annotations

from dataclasses import dataclass
from typing import Any, Optional


@dataclass
class LinkedList:
head: Node


@dataclass
class Node:
value: Any
next: Optional[Node] = None
13 changes: 13 additions & 0 deletions python-annotations/forward_references_strings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from dataclasses import dataclass
from typing import Any, Optional


@dataclass
class LinkedList:
head: "Node"


@dataclass
class Node:
value: Any
next: Optional["Node"] = None
13 changes: 13 additions & 0 deletions python-annotations/linked_list.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from dataclasses import dataclass
from typing import Any, Optional


@dataclass
class LinkedList:
head: Node # noqa


@dataclass
class Node:
value: Any
next: Optional[Node] = None # noqa
12 changes: 12 additions & 0 deletions python-annotations/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from dataclasses import dataclass

from validators import validate_email


@dataclass
class User:
email: str
password: str

def __post_init__(self):
validate_email(self)
10 changes: 10 additions & 0 deletions python-annotations/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
annotated-types==0.7.0
dnspython==2.7.0
email_validator==2.2.0
idna==3.10
polars==1.31.0
pydantic==2.11.7
pydantic_core==2.33.2
typeguard==4.4.4
typing-inspection==0.4.1
typing_extensions==4.14.1
6 changes: 6 additions & 0 deletions python-annotations/rtti.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from typeguard import typechecked


@typechecked
def add(a: int, b: int) -> int:
return a + b
11 changes: 11 additions & 0 deletions python-annotations/validators.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from __future__ import annotations

from typing import TYPE_CHECKING

if TYPE_CHECKING:
from models import User


def validate_email(user: User):
if user.email is None:
raise ValueError("email is required")