Files
typeshed/stdlib
Jim Crist-Harif 23e77931f9 Fix fields arg type in dataclass.make_dataclass (#7761)
The `fields` arg of `dataclass.make_dataclass` should have type:

```
Iterable[str | tuple[str, type] | tuple[str, type, Any]]
```

Previously the 3-tuple had type `tuple[str, type, Field[Any]]`, which
was incorrect for two reasons:

- The third element in the tuple doesn't have to be a ``Field``, it can
be any RHS value valid when defining a dataclass field (e.g.
``myfield: type = ...``). This may be a ``Field``, but it may also be a
default value like ``0``. ``Any`` is the proper type here.
- The type stubs for ``dataclass.field`` lie and say that this function
returns a value with the same type as ``default``. This avoids the need
for a mypy/pyright plugin that understands dataclasses, but also means
there is no way to create a ``Field`` object that these tools
understand, since they don't think ``dataclasses.field`` returns a
``Field`` instance.

With this change, the following valid dataclasses code passes both mypy
and pyright:

```python
from dataclasses import field, make_dataclass

Point = make_dataclass("Point", [("x", int), ("y", int), ("z", int, 0)])
Point2 = make_dataclass("Point2", [("x", int), ("y", int), ("z", int, field(default=0))])
```
2022-04-30 23:02:40 -06:00
..
2021-05-06 18:57:33 +03:00
2022-02-19 12:25:51 -08:00
2022-02-19 12:26:45 -08:00
2022-02-19 12:27:07 -08:00
2022-01-30 16:27:06 -08:00
2022-02-22 20:12:43 +02:00
2022-04-18 21:21:19 +03:00
2022-04-01 18:01:23 +01:00
2022-02-18 01:27:28 -08:00
2022-01-30 16:27:06 -08:00
2022-04-17 01:01:36 +01:00
2022-04-23 18:28:35 -07:00
2021-12-22 20:18:19 -08:00
2021-12-22 20:18:19 -08:00
2021-12-22 20:18:19 -08:00
2022-01-01 14:19:05 +02:00
2022-03-03 15:25:30 -08:00
2022-02-19 19:57:43 -08:00