Skip to content

Commit

Permalink
upgrade to Python 3.12. (#298)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucas-six authored Jan 27, 2025
1 parent d069356 commit 262c7c7
Show file tree
Hide file tree
Showing 13 changed files with 426 additions and 279 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@
.DS_Store

playground/
a.py
8 changes: 4 additions & 4 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,19 @@ repos:
# supported by your project here, or alternatively use
# pre-commit's default_language_version, see
# https://pre-commit.com/#top_level-default_language_version
language_version: python3.11
language_version: python3.12
- repo: https://github.com/pycqa/isort
rev: 5.13.2
hooks:
- id: isort
name: isort (python)
language_version: python3.11
language_version: python3.12
- repo: https://github.com/pre-commit/mirrors-mypy
rev: v1.14.1
hooks:
- id: mypy
additional_dependencies: [pydantic, types-redis, aiomqtt]
language_version: python3.11
language_version: python3.12
- repo: https://github.com/PyCQA/pylint
rev: v3.3.3
hooks:
Expand All @@ -58,7 +58,7 @@ repos:
types-redis,
aiomqtt,
]
language_version: python3.11
language_version: python3.12
- repo: https://github.com/asottile/pyupgrade
rev: v3.19.1
hooks:
Expand Down
4 changes: 4 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@
"isort.severity": {
"E": "Error"
},
"mypy-type-checker.args": [
"--enable-incomplete-feature=NewGenericSyntax"
],
"pylint.args": [
"--py-version=3.12",
"--load-plugins=pylint.extensions.bad_builtin,pylint_pydantic",
"--ignore=CVS,.git,__pycache__,.mypy_cache,tests",
"--disable=no-self-argument",
Expand Down
2 changes: 1 addition & 1 deletion Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,4 @@ pylint-pydantic = "*"
#pre-commit = "*"

[requires]
python_version = "3.11"
python_version = "3.12"
540 changes: 280 additions & 260 deletions Pipfile.lock

Large diffs are not rendered by default.

9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,13 @@
- [Literal: `typing.Literal`](https://lucas-six.github.io/python-cookbook/cookbook/core/type_hint/type_hint_for_literal)
- [Any: `typing.Any` and `object`](https://lucas-six.github.io/python-cookbook/cookbook/core/type_hint/type_hint_for_any)
- [Type objects](https://lucas-six.github.io/python-cookbook/cookbook/core/type_hint/type_hint_for_type)
- [Type Alias](https://lucas-six.github.io/python-cookbook/cookbook/core/type_hint/type_hint_for_type_alias)
- [Callable objects](https://lucas-six.github.io/python-cookbook/cookbook/core/type_hint/type_hint_for_callable)
- [Class Variables: `typing.ClassVar`](https://lucas-six.github.io/python-cookbook/cookbook/core/type_hint/type_hint_for_class_var)
- [Restricting Inheritance and Overriding: `@typing.final`](https://lucas-six.github.io/python-cookbook/cookbook/core/type_hint/type_hint_for_inheritance)
- [`typing.NoReturn`](https://lucas-six.github.io/python-cookbook/cookbook/core/type_hint/type_hint_for_noreturn)
- [Generic Function and Class](https://lucas-six.github.io/python-cookbook/cookbook/core/type_hint/type_hint_for_generic)
- [Override](https://lucas-six.github.io/python-cookbook/cookbook/core/type_hint/type_hint_for_override)

### I/O, File-Like Object

Expand Down Expand Up @@ -233,9 +236,7 @@

- [**`aiomqtt`**: Async](https://lucas-six.github.io/python-cookbook/cookbook/system_services/mqtt_aiomqtt)

## Recipes

### Language Core (语言核心)
## TODO: Language Core (语言核心)

- Parallelism and Concurrent (并发)
- [Multi-Threads Parallelism for **I/O-bound** tasks](https://lucas-six.github.io/python-cookbook/recipes/core/multi_threads)
Expand Down Expand Up @@ -272,7 +273,7 @@
## More Details

- [Linux Cookbook](https://lucas-six.github.io/linux-cookbook/)
- [Cookbook collections](https://lucas-six.github.io/)
- [Lucas' Cookbook](https://lucas-six.github.io/)

## License

Expand Down
1 change: 1 addition & 0 deletions cookbook/core/text/str_fmt_spec.md
Original file line number Diff line number Diff line change
Expand Up @@ -170,3 +170,4 @@ Combination examples:

- [PEP 3101 – Advanced String Formatting](https://peps.python.org/pep-3101/)
- [PEP 498 – Literal String Interpolation](https://peps.python.org/pep-0498/)
- [PEP 701 – Syntactic formalization of f-strings](https://peps.python.org/pep-0701/)
1 change: 0 additions & 1 deletion cookbook/core/type_hint/type_hint.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ are handled by evaluating them in `globalns` and `localns` namespaces.
- [PEP 3107 – Function Annotations](https://peps.python.org/pep-3107/)
- [PEP 484 – Type Hints](https://peps.python.org/pep-0484/)
- [PEP 483 – The Theory of Type Hints](https://peps.python.org/pep-0483/)
- [PEP 586 – Literal Types](https://peps.python.org/pep-0586/)
- [PEP 563 – Postponed Evaluation of Annotations](https://peps.python.org/pep-0563/)
- [`mypy` Documentation](https://mypy.readthedocs.io/en/latest/)
- [GitHub - `typeshed`](https://github.com/python/typeshed)
Expand Down
49 changes: 49 additions & 0 deletions cookbook/core/type_hint/type_hint_for_generic.md
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/)
34 changes: 34 additions & 0 deletions cookbook/core/type_hint/type_hint_for_override.md
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/)
28 changes: 28 additions & 0 deletions cookbook/core/type_hint/type_hint_for_type_alias.md
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)
18 changes: 14 additions & 4 deletions cookbook/core/type_hint/type_hint_for_typeddict.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,28 @@
## Recipes

```python
from typing import TypedDict, NotRequired
from typing import TypedDict, NotRequired, Unpack

class MyDict(TypedDict):
class KwArgs(TypedDict):
key_a: int
key_b: float
label: NotRequired[str]


# functional syntax: alternative syntax
MyDict = TypedDict('MyDict', {'key_a': int, 'key_b': float, 'label': NotRequired})
KwArgs = TypedDict('MyDict', {'key_a': int, 'key_b': float, 'label': NotRequired})


d: MyDict = {}
# Using `Unpack` since Python 3.12.
def func(**kwargs: Unpack[KwArgs]) -> None:
pass


d: KwArgs = {'key_a': 1, 'key_b': 1.2}
func(**d)
func(key_a=1, key_b=1.2)
d1: dict[int, float] = {'key_a': 1, 'key_b': 1.2}
func(**d1) # WRONG!
```

## More Details
Expand All @@ -29,4 +38,5 @@ d: MyDict = {}
- [Python - `typing.TypedDict` module](https://docs.python.org/3/library/typing.html#typing.TypedDict)
- [Python - `typing.NotRequired` module](https://docs.python.org/3/library/typing.html#typing.NotRequired)
- [PEP 655 – Marking individual `TypedDict` items as required or potentially-missing](https://peps.python.org/pep-0655/)
- [PEP 692 – Using TypedDict for more precise **kwargs typing](https://peps.python.org/pep-0692/)
- [typing - `Required`/`NotRequired`](https://typing.readthedocs.io/en/latest/spec/typeddict.html#required-notrequired)
10 changes: 5 additions & 5 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -197,4 +197,4 @@ exclude = [".git", "**/__pycache__", "**/.mypy_cache"]
reportGeneralTypeIssues = "none"
reportUnboundVariable = "none"
stubPath = ""
pythonVersion = "3.11"
pythonVersion = "3.12"

0 comments on commit 262c7c7

Please sign in to comment.