Skip to content

Commit cbe739a

Browse files
committed
Merge branch 'master' into fastapi
2 parents f108c53 + dfc0e2c commit cbe739a

File tree

10 files changed

+101
-0
lines changed

10 files changed

+101
-0
lines changed

python-annotations/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Python 3.14 Preview: Lazy Annotations
2+
3+
This folder contains sample code for the Real Python tutorial [Python 3.14 Preview: Lazy Annotations](https://realpython.com/python-annotations/).

python-annotations/calculator.pyi

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
type Number = int | float
2+
3+
class Calculator:
4+
history: list[Number]
5+
6+
def __init__(self) -> None: ...
7+
def add(self, a: Number, b: Number) -> Number: ...
8+
9+
def add(a: Number, b: Number) -> Number: ...

python-annotations/fib.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from typing import Annotated
2+
3+
4+
def fib(n: int) -> int:
5+
return n if n < 2 else fib(n - 2) + fib(n - 1)
6+
7+
8+
def increment(x: Annotated[int, fib(35)]) -> int:
9+
return x + 1
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from __future__ import annotations
2+
3+
from dataclasses import dataclass
4+
from typing import Any, Optional
5+
6+
7+
@dataclass
8+
class LinkedList:
9+
head: Node
10+
11+
12+
@dataclass
13+
class Node:
14+
value: Any
15+
next: Optional[Node] = None
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from dataclasses import dataclass
2+
from typing import Any, Optional
3+
4+
5+
@dataclass
6+
class LinkedList:
7+
head: "Node"
8+
9+
10+
@dataclass
11+
class Node:
12+
value: Any
13+
next: Optional["Node"] = None

python-annotations/linked_list.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from dataclasses import dataclass
2+
from typing import Any, Optional
3+
4+
5+
@dataclass
6+
class LinkedList:
7+
head: Node # noqa
8+
9+
10+
@dataclass
11+
class Node:
12+
value: Any
13+
next: Optional[Node] = None # noqa

python-annotations/models.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from dataclasses import dataclass
2+
3+
from validators import validate_email
4+
5+
6+
@dataclass
7+
class User:
8+
email: str
9+
password: str
10+
11+
def __post_init__(self):
12+
validate_email(self)
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
annotated-types==0.7.0
2+
dnspython==2.7.0
3+
email_validator==2.2.0
4+
idna==3.10
5+
polars==1.31.0
6+
pydantic==2.11.7
7+
pydantic_core==2.33.2
8+
typeguard==4.4.4
9+
typing-inspection==0.4.1
10+
typing_extensions==4.14.1

python-annotations/rtti.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from typeguard import typechecked
2+
3+
4+
@typechecked
5+
def add(a: int, b: int) -> int:
6+
return a + b

python-annotations/validators.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from __future__ import annotations
2+
3+
from typing import TYPE_CHECKING
4+
5+
if TYPE_CHECKING:
6+
from models import User
7+
8+
9+
def validate_email(user: User):
10+
if user.email is None:
11+
raise ValueError("email is required")

0 commit comments

Comments
 (0)