Python typing: Difference between revisions
From wikinotes
No edit summary |
|||
(12 intermediate revisions by the same user not shown) | |||
Line 4: | Line 4: | ||
<blockquote> | <blockquote> | ||
<source lang="python"> | <source lang="python"> | ||
from typing import Dict, List, Tuple, | from typing import Dict, List, Tuple | ||
from typing import Optional, Union | |||
class Project: | class Project: | ||
def | def ex_param_w_return(user: User) -> bool | ||
def | def ex_type_w_default(active: bool=False) -> None: | ||
def | def ex_no_return() -> None: | ||
def ex_generics(data: List[Dict]): | |||
def ex_optional() -> Optional[str]: # nullable type | |||
def ex_type_or_other_type(id: Union[str, int]) -> None: | |||
</source> | </source> | ||
</blockquote><!-- Example --> | </blockquote><!-- Example --> | ||
Line 20: | Line 24: | ||
= Common Types = | = Common Types = | ||
<blockquote> | <blockquote> | ||
== functions/lambdas == | |||
<blockquote> | |||
<syntaxhighlight lang="python"> | |||
from typing import Callable | |||
def run_callback(cb: Callable[[str, int], bool]) -> None: | |||
</syntaxhighlight> | |||
</blockquote><!-- functions/lambdas --> | |||
== dates/times == | |||
<blockquote> | |||
<syntaxhighlight lang="python"> | |||
import datetime | |||
def foo() -> datetime.datetime: | |||
return datetime.datetime.now() | |||
def foo() -> datetime.date: | |||
return datetime.date.today() | |||
</syntaxhighlight> | |||
</blockquote><!-- dates/times --> | |||
== open file objects == | == open file objects == | ||
<blockquote> | <blockquote> | ||
Line 36: | Line 62: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
</blockquote><!-- open file objects --> | </blockquote><!-- open file objects --> | ||
== context managers == | |||
<blockquote> | |||
<syntaxhighlight lang="python"> | |||
@contextlib.contextmanager | |||
def foo() -> Iterator[str]: | |||
print('foo') | |||
yield 'bar' | |||
print('baz') | |||
</syntaxhighlight> | |||
</blockquote><!-- context managers --> | |||
== dataclasses (structs) == | |||
<blockquote> | |||
They're already typed. See [[python datatypes]] | |||
<syntaxhighlight lang="python"> | |||
import dataclasses | |||
@dataclasses.dataclass | |||
class User | |||
id: int | |||
name: str | |||
</syntaxhighlight> | |||
</blockquote><!-- dataclasses (structs) --> | |||
</blockquote><!-- Common Types --> | </blockquote><!-- Common Types --> | ||
= Implementing Generics = | |||
<blockquote> | |||
{{ TODO | | |||
improve, w/ usage example }} | |||
<syntaxhighlight lang="python"> | |||
from typing import Generic, TypeVar | |||
T = TypeVar('T') | |||
class Range(Generic[T]): | |||
start: T | |||
end: T | |||
</syntaxhighlight> | |||
</blockquote><!-- Implementing Generics --> |
Latest revision as of 22:56, 13 February 2024
Type annotations in python.
Example
from typing import Dict, List, Tuple from typing import Optional, Union class Project: def ex_param_w_return(user: User) -> bool def ex_type_w_default(active: bool=False) -> None: def ex_no_return() -> None: def ex_generics(data: List[Dict]): def ex_optional() -> Optional[str]: # nullable type def ex_type_or_other_type(id: Union[str, int]) -> None:
Common Types
functions/lambdas
from typing import Callable def run_callback(cb: Callable[[str, int], bool]) -> None:dates/times
import datetime def foo() -> datetime.datetime: return datetime.datetime.now() def foo() -> datetime.date: return datetime.date.today()open file objects
from typing import IO # either bytes/str def foo() -> IO: return open('foo.txt', 'r') def foo() -> IO[bytes]: return open('foo.txt', 'rb') def foo() -> IO[str]: return open('foo.txt', 'r')context managers
@contextlib.contextmanager def foo() -> Iterator[str]: print('foo') yield 'bar' print('baz')dataclasses (structs)
They're already typed. See python datatypes
import dataclasses @dataclasses.dataclass class User id: int name: str
Implementing Generics
TODO:
improve, w/ usage example
from typing import Generic, TypeVar T = TypeVar('T') class Range(Generic[T]): start: T end: T