-
Notifications
You must be signed in to change notification settings - Fork 0
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
13 changed files
with
426 additions
and
279 deletions.
There are no files selected for viewing
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 |
---|---|---|
|
@@ -11,3 +11,4 @@ | |
.DS_Store | ||
|
||
playground/ | ||
a.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
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
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 |
---|---|---|
|
@@ -28,4 +28,4 @@ pylint-pydantic = "*" | |
#pre-commit = "*" | ||
|
||
[requires] | ||
python_version = "3.11" | ||
python_version = "3.12" |
Large diffs are not rendered by default.
Oops, something went wrong.
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
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
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
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,49 @@ | ||
# Type Hint for Generic | ||
|
||
## Recipes | ||
|
||
### Generic Function | ||
|
||
```python | ||
def generic_func[T](arg: T) -> T: | ||
return arg + 1 | ||
``` | ||
|
||
```python | ||
# Python 3.11- | ||
|
||
from typing import TypeVar | ||
|
||
# Generic Type Variables | ||
T = TypeVar['T', int, float] | ||
|
||
def generic_func(arg: T) -> T: | ||
return arg + 1 | ||
``` | ||
|
||
### Generic Class | ||
|
||
```python | ||
class GenericClass[T]: | ||
pass | ||
``` | ||
|
||
```python | ||
# Python 3.11- | ||
|
||
from typing import TypeVar, Generic | ||
|
||
# Generic Type Variables | ||
T = TypeVar['T', int, float] | ||
|
||
class GenericClass(Generic[T]): | ||
pass | ||
``` | ||
|
||
## More Details | ||
|
||
- [Type Hint](type_hint) | ||
|
||
## References | ||
|
||
- [PEP 695 – Type Parameter Syntax](https://peps.python.org/pep-0695/) |
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,34 @@ | ||
# Type Hint for Override | ||
|
||
New in Python **3.12**. | ||
|
||
## Recipes | ||
|
||
```python | ||
from typing import override | ||
|
||
class Parent: | ||
def foo(self) -> int: | ||
return 1 | ||
|
||
def bar(self, x: str) -> str: | ||
return x | ||
|
||
class Child(Parent): | ||
@override | ||
def foo(self) -> int: | ||
return 2 | ||
|
||
@override | ||
def baz(self) -> int: # Type check error: no matching signature in ancestor | ||
return 1 | ||
``` | ||
|
||
## More Details | ||
|
||
- [Type Hint](type_hint) | ||
|
||
## References | ||
|
||
- [Python - `typing.override` module](https://docs.python.org/3/library/typing.html#typing.override) | ||
- [PEP 698 – Override Decorator for Static Typing](https://peps.python.org/pep-0698/) |
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,28 @@ | ||
# Type Hint for Type Alias | ||
|
||
## Recipes | ||
|
||
```python | ||
# Python 3.12+ | ||
|
||
type Vector = list[float] | ||
type Point[T] = tuple[T, T] | ||
type IntOrStrSequence[T: (int, str)] = Sequence[T] # 带约束的 TypeVar | ||
type IntFunc[**P] = Callable[P, int] # ParamSpec | ||
``` | ||
|
||
```python | ||
# Python 3.11- | ||
|
||
from typing import TypeAlias | ||
|
||
Vector: TypeAlias = list[float] | ||
``` | ||
|
||
## More Details | ||
|
||
- [Type Hint](type_hint) | ||
|
||
## References | ||
|
||
- [`typing.TypeAliasType` - Python](https://docs.python.org/zh-cn/3.12/library/typing.html#typing.TypeAliasType) |
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
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 |
---|---|---|
|
@@ -3,15 +3,15 @@ name = "python-cookbook" | |
description = "Recipes for Python. Hands-on code examples and snippets for daily work." | ||
authors = [{ name = "Li Yun", email = "[email protected]" }] | ||
readme = "README.md" | ||
requires-python = "~=3.11" | ||
requires-python = "~=3.12" | ||
license = { file = "LICENSE" } | ||
maintainers = [{ name = "Li Yun", email = "[email protected]" }] | ||
keywords = ["cookbook", "recipe", "fastapi", "mongodb"] | ||
classifiers = [ | ||
"Development Status :: 1 - Planning", | ||
"Programming Language :: Python :: 3 :: Only", | ||
"Programming Language :: Python :: 3.10", | ||
"Programming Language :: Python :: 3.11", | ||
"Programming Language :: Python :: 3.12", | ||
"Programming Language :: Python :: Implementation :: CPython", | ||
"Topic :: Software Development :: Libraries :: Python Modules", | ||
"Topic :: Utilities", | ||
|
@@ -100,7 +100,7 @@ extend_skip = [".gitignore", ".env", ".dockerignore"] | |
extend_skip_glob = [] | ||
|
||
[tool.mypy] | ||
python_version = "3.11" | ||
python_version = "3.12" | ||
plugins = ["pydantic.mypy"] | ||
exclude = [] | ||
follow_imports = "silent" | ||
|
@@ -128,7 +128,7 @@ ignore_missing_imports = true | |
|
||
[tool.pylint.main] | ||
recursive = true | ||
py-version = 3.11 | ||
py-version = 3.12 | ||
jobs = 0 | ||
ignore = "CVS,.git,__pycache__,.mypy_cache,tests" | ||
ignore-paths = "tests" | ||
|
@@ -197,4 +197,4 @@ exclude = [".git", "**/__pycache__", "**/.mypy_cache"] | |
reportGeneralTypeIssues = "none" | ||
reportUnboundVariable = "none" | ||
stubPath = "" | ||
pythonVersion = "3.11" | ||
pythonVersion = "3.12" |