Commit bc23aa1
committed
Fix type annotations for xml_field_validator/serializer
There are 2 issues with the annotations:
1. Function types are contravariant rather than covariant in their
argument types. This means that `Callable[[BaseXmlModel], Any]` is a
subtype rather than a supertype of `Callable[[MyModel], Any]` and so
using `Callable[[BaseXmlModel], Any]` as a lower bound of TypeVars
doesn't work. Since Python doesn't support upper bounds on TypeVars,
the solution is to push the TypeVar inside. To see the issue run mypy
on the example from the documentation[1]. Currently, you get these
errors:
```
t.py:13: error: Value of type variable "ValidatorFuncT" of function cannot be "Callable[[type[Plot], XmlElementReader, str], list[float]]" [type-var]
t.py:21: error: Value of type variable "SerializerFuncT" of function cannot be "Callable[[Plot, XmlElementWriter, list[float], str], None]" [type-var]
```
This PR resolves these (however, validate_space_separated_list needs
to be decorated with @classmethod).
2. A recent commit[2] has a typo where ValidatorFuncT was bound by
SerializerFunc instead of ValidatorFunc.
[1] https://pydantic-xml.readthedocs.io/en/latest/pages/misc.html#custom-xml-serialization
[2] ec4b5471 parent 526259e commit bc23aa1
File tree
5 files changed
+29
-18
lines changed- .github/workflows
- examples/xml-serialization-decorator
- pydantic_xml
- tests
5 files changed
+29
-18
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
33 | 33 | | |
34 | 34 | | |
35 | 35 | | |
36 | | - | |
| 36 | + | |
37 | 37 | | |
38 | 38 | | |
39 | 39 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
11 | 11 | | |
12 | 12 | | |
13 | 13 | | |
| 14 | + | |
14 | 15 | | |
15 | | - | |
16 | | - | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
17 | 20 | | |
18 | 21 | | |
19 | 22 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | | - | |
| 3 | + | |
4 | 4 | | |
5 | 5 | | |
6 | 6 | | |
| |||
292 | 292 | | |
293 | 293 | | |
294 | 294 | | |
295 | | - | |
296 | | - | |
297 | | - | |
298 | | - | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
299 | 298 | | |
300 | 299 | | |
301 | 300 | | |
302 | 301 | | |
303 | 302 | | |
304 | 303 | | |
305 | 304 | | |
306 | | - | |
| 305 | + | |
| 306 | + | |
| 307 | + | |
307 | 308 | | |
308 | 309 | | |
309 | 310 | | |
310 | 311 | | |
311 | 312 | | |
312 | 313 | | |
313 | | - | |
314 | | - | |
315 | | - | |
316 | | - | |
| 314 | + | |
| 315 | + | |
| 316 | + | |
317 | 317 | | |
318 | 318 | | |
319 | 319 | | |
320 | 320 | | |
321 | 321 | | |
322 | 322 | | |
323 | 323 | | |
324 | | - | |
| 324 | + | |
325 | 325 | | |
326 | 326 | | |
327 | 327 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
20 | 20 | | |
21 | 21 | | |
22 | 22 | | |
| 23 | + | |
23 | 24 | | |
24 | 25 | | |
| 26 | + | |
25 | 27 | | |
| 28 | + | |
26 | 29 | | |
27 | 30 | | |
28 | 31 | | |
| |||
137 | 140 | | |
138 | 141 | | |
139 | 142 | | |
140 | | - | |
141 | | - | |
142 | 143 | | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
143 | 148 | | |
144 | 149 | | |
145 | 150 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
13 | 13 | | |
14 | 14 | | |
15 | 15 | | |
| 16 | + | |
16 | 17 | | |
17 | | - | |
18 | | - | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
19 | 22 | | |
20 | 23 | | |
21 | 24 | | |
| |||
0 commit comments