-
-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
228 additions
and
82 deletions.
There are no files selected for viewing
19 changes: 19 additions & 0 deletions
19
python/examples/python-types-at-pyweb-2024-12/defined_complex_type_alias.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
ReturnType = dict[str, int] | ||
result: ReturnType = { | ||
"blue": 1, | ||
"green": 0, | ||
} | ||
#result: ReturnType = { | ||
# "blue": "ok", | ||
# "green": 0, | ||
#} | ||
|
||
# ------------------------------------------ | ||
|
||
UnionReturnType = dict[str, str | int] | ||
ret: UnionReturnType = { | ||
"blue": "ok", | ||
"green": 0, | ||
} | ||
|
||
|
8 changes: 8 additions & 0 deletions
8
python/examples/python-types-at-pyweb-2024-12/defined_type_alias.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
LabelType = str | ||
|
||
txt = "hello" | ||
label: LabelType = txt | ||
|
||
print(label) | ||
|
||
|
23 changes: 23 additions & 0 deletions
23
python/examples/python-types-at-pyweb-2024-12/defined_types.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
from typing import Literal | ||
LevelType = Literal["debug", "info", "warning"] | ||
|
||
size: LevelType = "debug" | ||
#size = "error" | ||
|
||
# ------------------------------------------ | ||
|
||
from datetime import datetime | ||
from typing import TypedDict | ||
HistoryType = TypedDict('HistoryType', { | ||
"date" : datetime, | ||
"level": LevelType, | ||
"text": str, | ||
}) | ||
|
||
event: HistoryType = { | ||
"date": datetime.now(), | ||
"level": "debug", | ||
"text": "Demo typing", | ||
} | ||
|
||
|
22 changes: 10 additions & 12 deletions
22
python/examples/python-types-at-pyweb-2024-12/generics.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,24 @@ | ||
from typing import TypeVar | ||
from typing import Protocol, Self | ||
|
||
from abc import abstractmethod | ||
|
||
class SupportsAdd(Protocol): | ||
@abstractmethod | ||
def __add__(self, other: Self) -> Self: | ||
pass | ||
|
||
|
||
# def add[T](x: T, y: T) -> T: | ||
# return x + y | ||
T = TypeVar('T', bound=SupportsAdd) | ||
|
||
#T = TypeVar('T', int, str) | ||
# T = TypeVar('T', bound=SupportsAdd) | ||
# def add(x: T, y: T) -> T: | ||
# return x + y | ||
# ------------------------------- | ||
def add(x: T, y: T) -> T: | ||
return x + y | ||
|
||
z = add(2, 3) | ||
print(z) | ||
|
||
def add[T: SupportsAdd](x: T, y: T) -> T: | ||
# ------------------------------- | ||
def adder[T: SupportsAdd](x: T, y: T) -> T: | ||
return x + y | ||
|
||
z = add(2, 3) | ||
print(z + 2) | ||
#print(z + "2") | ||
z = adder(3, 4) | ||
print(z) |
4 changes: 4 additions & 0 deletions
4
python/examples/python-types-at-pyweb-2024-12/generics_cannot_be_any_type.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
def add[T](x: T, y: T) -> T: | ||
return x + y | ||
|
||
add(2, 3) |
8 changes: 8 additions & 0 deletions
8
python/examples/python-types-at-pyweb-2024-12/generics_limit_types_by_listing.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
from typing import TypeVar | ||
|
||
T = TypeVar('T', int, str) | ||
|
||
def add(x: T, y: T) -> T: | ||
return x + y | ||
|
||
add(2, 3) |
7 changes: 7 additions & 0 deletions
7
python/examples/python-types-at-pyweb-2024-12/generics_plain.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
def do_something[T](x: T) -> T: | ||
return x | ||
|
||
y = do_something(23) | ||
print(y + 19) | ||
#print(y + "19") | ||
|
15 changes: 15 additions & 0 deletions
15
python/examples/python-types-at-pyweb-2024-12/this_or_none.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
text = "a,b,c" | ||
|
||
print(text.split()) | ||
print(text.split(None)) | ||
print(text.split(",")) | ||
|
||
|
||
|
||
|
||
def split(text, sep: str | None=None): | ||
return text.split(sep) | ||
|
||
print(split(text)) | ||
print(split(text, None)) | ||
print(split(text, ",")) |
14 changes: 14 additions & 0 deletions
14
python/examples/python-types-at-pyweb-2024-12/type_annotation.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
def add(x: int, y: int) -> int: | ||
return x + y | ||
|
||
|
||
def multiply(x, y): | ||
res: int = x * y | ||
return res | ||
|
||
|
||
a: int = 23 | ||
|
||
z: int = add(a, 19) | ||
|
||
result = multiply(a, 19) |
Oops, something went wrong.