Skip to content

WIP: Python 3.12 posts #53

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions posts/concatenate.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
---
author: orsinium
id: 730
published: 2023-08-10
topics:
- typing
Expand Down
1 change: 1 addition & 0 deletions posts/param-spec.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
---
author: orsinium
id: 729
published: 2023-08-08
topics:
- typing
Expand Down
64 changes: 64 additions & 0 deletions posts/type-params.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
---
author: orsinium
published: 2023-08-17
pep: 695
python: "3.12"
topics:
- typing
---

# Type parameter syntax

[PEP 695](https://peps.python.org/pep-0695/) (will land in Python 3.12) introduced a new, more concise, way to declare type parameters.

Before:

```python
from typing import Generic, Iterable, TypeVar

T = TypeVar('T')

def max(args: Iterable[T]) -> T:
...

class Queue(Generic[T]):
def push(self, item: T) -> None:
...
```

After:

```python
from typing import Iterable

def max[T](args: Iterable[T]) -> T:
...

class Queue[T]:
def push(self, item: T) -> None:
...
```

And you can also add constraints to type variables.

Before:

```python
from typing import Callable, TypeVar

C = TypeVar('C', bound=Callable)

def decorator(f: C) -> C:
...
```

After:

```python
from typing import Callable

def decorator[C: (Callable, )](f: C) -> C:
...
```

The main benefit of the new syntax is that type constraints are now scoped to the class or function that uses them. The old `TypeVar` syntax made you declare a global variable at the top of the file that might be far from the class or function declaration that needs them and is often reused in multiple places.
2 changes: 2 additions & 0 deletions posts/typed-dict.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
published: 2021-03-25
id: 656
author: orsinium
topics:
- typing
traces:
- [module: typing, type: TypedDict]
pep: 589
Expand Down
2 changes: 2 additions & 0 deletions posts/typing-self.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
author: orsinium
id: 711
published: 2022-12-03
topics:
- typing
traces:
- [module: typing, type: Self]
pep: 673
Expand Down
14 changes: 14 additions & 0 deletions posts/website-announcement.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
author: orsinium
published: 2023-08-15
---

# Website announcement

Great news everyone! We extracted all our recent posts as Markdown, organized them, and made them more accessible. Now we have:

* 🌐 Website: [pythonetc.orsinium.dev](https://pythonetc.orsinium.dev/)
* 📢 RSS: [pythonetc.orsinium.dev/index.xml](https://pythonetc.orsinium.dev/index.xml)
* 🧑‍💻️ GitHub: [github.com/life4/pythonetc](https://github.com/life4/pythonetc)

If you want to write a guest post, just send us a PR on GitHub. The README tells what you can write about and how. Thank you all for staying with us all these years ❤️
1 change: 1 addition & 0 deletions todo.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,5 @@ These are ideas for future posts. Let me know if you want to write a guest post
+ asyncio.TaskGroup
+ contextvars
+ itertools.pairwise
+ typing.deprecated <https://peps.python.org/pep-0702/>
+ <https://peps.python.org/pep-0692/>