Skip to content

Commit 5b4f01f

Browse files
committed
Add structural subtyping
Signed-off-by: Jakub Walczak <jakub.walczak@p.lodz.pl>
1 parent 68d61e4 commit 5b4f01f

6 files changed

Lines changed: 359 additions & 10 deletions

File tree

_toc.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@ parts:
88
- file: context_managers
99
- file: iterators
1010
- file: generators
11+
- file: generics
1112
- file: structural_subtyping
1213
- file: higher_order_functions
1314
- file: decorators
1415
- file: metaclasses
1516
- file: descriptors
1617
- file: code_inspect
1718
- file: dynamic_execution
18-
- file: generics
1919
- file: weakref
2020
- file: coroutines_and_concurrency
2121
- file: parallelism

generics.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,11 @@ kernelspec:
1616
````{admonition} This chapter is not ready yet?
1717
:class: important
1818
Writing a book takes time and for that chapter I did not have enough of it. Please, return later.
19-
````
19+
````
20+
21+
22+
## Variance types
23+
Let us explain slightly here the type of type variance we have in Python what was explained in PEP483 {cite}`pep-0483`. In general, types can be:
24+
1. covariant,
25+
2. contravariant,
26+
3. invariant

intro.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,12 @@ Iterating over sources of data
158158
Generating content and `yield` keyword
159159
:::
160160

161+
:::{grid-item-card} [Generic collections](generics.md) 📦
162+
:class-header: bg-light
163+
164+
Type-safe collection handling
165+
:::
166+
161167
:::{grid-item-card} [Structural subtyping](structural_subtyping.md) 👣
162168
:class-header: bg-light
163169

@@ -200,12 +206,6 @@ Stack tracing, callable signature management, and more
200206
And its pros and cons
201207
:::
202208

203-
:::{grid-item-card} [Generic collections](generics.md) 📦
204-
:class-header: bg-light
205-
206-
Type-safe collection handling
207-
:::
208-
209209
:::{grid-item-card} [Weak references](weakref.md) 🔗
210210
:class-header: bg-light
211211

references.bib

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,4 +234,28 @@ @techreport{pep-0479
234234
month = {November},
235235
url = {https://peps.python.org/pep-0479/},
236236
urldate = {2025-10-21}
237+
}
238+
239+
@techreport{pep-0544,
240+
author = {Levkivskyi, Ivan and Lehtosalo, Jukka and Langa, Łukasz},
241+
title = {{PEP} 544 -- Protocols: Structural subtyping (static duck typing)},
242+
institution = {Python Software Foundation},
243+
type = {Python Enhancement Proposal},
244+
number = {544},
245+
year = {2017},
246+
month = {March},
247+
url = {https://peps.python.org/pep-0544/},
248+
urldate = {2025-10-23}
249+
}
250+
251+
@techreport{pep-0483,
252+
author = {van Rossum, Guido and Levkivskyi, Ivan},
253+
title = {{PEP} 483 -- The Theory of Type Hints},
254+
institution = {Python Software Foundation},
255+
type = {Python Enhancement Proposal},
256+
number = {483},
257+
year = {2014},
258+
month = {December},
259+
url = {https://peps.python.org/pep-0483/},
260+
urldate = {2025-10-23}
237261
}

special_attributes.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,10 @@ There are two main exceptions:
292292

293293
## Of Classes
294294

295+
### Attributes
296+
297+
#### `__mro__`
298+
295299
## Of Objects
296300

297301
### Attributes

0 commit comments

Comments
 (0)