Commit Graph

44 Commits

Author SHA1 Message Date
Stephen Morton
bd728fbfae use Literal[_MISSING_TYPE.MISSING] for dataclasses.field defaults (#13256) 2024-12-18 19:21:31 +01:00
Avasam
3719f02dbf Using precise code for pyright: ignore and re-enabling various pyright tests (#12576) 2024-08-22 03:34:52 +02:00
Marti Raudsepp
1ace5718de Fix inferred type of is_dataclass(Any) (#12517) 2024-08-14 23:09:11 +02:00
Max Muoto
6b2033a783 Improve accuracy of dataclasses.Field (#12082) 2024-06-24 11:25:50 +02:00
Neil Girdhar
6246a38e8e Use TypeIs in is_dataclass (#11929) 2024-05-26 13:29:24 +02:00
Shantanu
d9cf43c4c3 Mark pos-only __class_getitem__ args (#11970) 2024-05-18 23:27:51 +02:00
Alexandru Mărășteanu
4df0725b48 Use Any for field type in make_dataclass (#11657)
Co-authored-by: Alex Waygood <alex.waygood@gmail.com>
2024-03-25 23:05:14 +00:00
renovate[bot]
48106feed7 chore(deps): update pytype and pyright (#11595) 2024-03-14 09:28:09 +01:00
Shantanu
470a13ab09 Use PEP 570 syntax in stdlib (#11250) 2024-03-09 14:50:16 -08:00
Sebastian Rittau
53a8193d64 Update typing_extensions imports in stdlib (#11244)
Co-authored-by: AlexWaygood <alex.waygood@gmail.com>
2024-01-05 08:15:19 -08:00
Sebastian Rittau
23604858a6 Remove Python 3.7 branches (#11238) 2024-01-05 11:39:39 +01:00
Nikita Sobolev
30fd9b4ffa dataclasses.make_dataclass has a new parameter in 3.12 (#10006)
Source 810d365b5e/Lib/dataclasses.py (L1403)
2023-04-04 04:50:19 +03:00
Alex Waygood
dd2818a41d Stdlib: add container default values (#9909) 2023-03-21 09:12:34 +01:00
Alex Waygood
4ca0c48425 Improve the first overload of is_dataclass (#9758) 2023-02-19 06:26:11 -08:00
Max Murin
75cd302215 dataclass: switch order of decorator overloads (#9743) 2023-02-16 16:22:56 -08:00
Marc Mueller
88a761ed4e Export DataclassInstance protocol from _typeshed (#9676) 2023-02-04 15:58:11 +00:00
Alex Waygood
33a62ae42d Add more defaults to the stdlib (#9606)
Continuing work towards #8988.

The first five commits were created using stubdefaulter on various Python versions; the following commits were all created manually by me to fix various problems. The main things this adds that weren't present in #9501 are:

- Defaults in Windows-only modules and Windows-only branches (because I'm running a Windows machine)
- Defaults in non-py311 branches
- Defaults for float parameters
- Defaults for overloads
2023-01-29 01:51:23 +00:00
Alex Waygood
32ebe323f5 Use a TypeGuard for dataclasses.is_dataclass(); refine asdict(), astuple(), fields(), replace() (#9362) 2023-01-28 15:14:22 +00:00
Jelle Zijlstra
ddfaca3200 stdlib: add argument default values (#9501) 2023-01-18 09:37:34 +01:00
Nikita Sobolev
6e84b56016 Add metaclasses for string.Template and dataclasses.InitVar (#8852) 2022-10-05 14:41:24 -07:00
Alex Waygood
e88a182573 Simplify __all__ definitions in modules beginning with 'a' to 'l' (#8026) 2022-06-07 15:40:48 +02:00
Jelle Zijlstra
0b7df3b2aa dataclasses: fix and sort stubtest complaints (#7888) 2022-05-19 19:13:22 -07:00
Sam Ezeh
ef878e925f Add weakref_slot parameter to @dataclass decorator (#7772)
https://github.com/python/cpython/pull/92160
2022-05-03 10:37:06 +01:00
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
Alex Waygood
97a74bc1aa Import from collections.abc wherever possible (#7635) 2022-04-18 12:50:37 +02:00
Alex Waygood
b63c963077 Use conditional overloads to simplify several stdlib functions (#7540) 2022-03-25 08:47:03 +01:00
Alex Waygood
e9db3bd50a Add __all__ for modules beginning with 'c' and 'd' (#7303) 2022-02-19 17:51:27 -08:00
Alex Waygood
96b06adf2c Add missing descriptor methods in dataclasses and functools (#7203) 2022-02-15 06:45:49 -08:00
Stephen Rosen
38d32a916c Use enum trick for dataclasses.MISSING (#7127)
The goal of this change is to fix the behavior of
`dataclasses.Field`. Several attributes of a `Field` may have a value
of `MISSING` (a sentinel value). As a result, attributes of Field may
be checked against `MISSING` as in

     [f for f in fields(obj) if f.default is MISSING]

`default`, `default_factory`, and `kw_only` are the attributes
which may have a value of `MISSING`.

This workaround of defining `_MISSING_TYPE` as an enum, and `MISSING`
as its only member, allows `... | Literal[_MISSING_TYPE.MISSING]` to
be used in type annotations for these attributes. This is very
slightly inaccurate -- primarily in that `_MISSING_TYPE` isn't really
an enum -- but this allows for use in `Literal`.
After PEP 661 (Sentinel Values), there may be a more accurate way of
writing this, but for now this works.

This adjustment is only applied to the attributes of Field, not the
arguments to functions and methods.
2022-02-04 21:21:12 -08:00
Alex Waygood
a40d79a4e6 Use lowercase type everywhere (#6853) 2022-01-08 16:09:29 +01:00
Alex Waygood
8d5d2520ac Use PEP 585 syntax wherever possible (#6717) 2021-12-28 11:31:43 +01:00
Stephen Rosen
df0a724c0f Always import Protocol from typing in stubs (#6617) 2021-12-17 13:12:22 +01:00
Sebastian Rittau
c900c0e769 Fix Python 3.10.1 problems pointed out by stubtest (#6604)
Co-authored-by: Akuli <akuviljanen17@gmail.com>
2021-12-16 07:05:45 -08:00
Akuli
994b69ef8f Use lowercase tuple where possible (#6170) 2021-10-14 17:18:19 -07:00
Bas van Beek
b9e1d7d522 Replace Mapping with types.MappingProxyType (#6013)
Mark `Signature.parameters` and `.return_annotation` as read-only properties
2021-09-09 09:58:53 +02:00
Akuli
ce11072dbe Big diff: use lower-case list and dict (#5888) 2021-08-08 09:26:35 -07:00
Kyle Altendorf
4b77d454bd dataclasses: Allow Any keys for the metadata field (#5823) 2021-08-08 15:50:52 +02:00
Akuli
ee487304d7 Big diff: Use new "|" union syntax (#5872) 2021-08-08 11:05:21 +02:00
Eric Traut
ea4be02bab Redefined dataclasses.KW_ONLY (#5826)
Redefined dataclasses.KW_ONLY so it's a type alias rather than a class instance. Class instances are illegal to use within a type annotation.
2021-08-01 08:06:48 -07:00
Shantanu
04f0113d16 dataclasses: work around default factory issues (#5718) 2021-07-01 08:07:00 +02:00
Shantanu
4bfdba71cb dataclasses: update for py310 (#5525) 2021-05-25 14:43:06 +02:00
Sebastian Rittau
45916045c8 Upgrade black to 21.4b0 and reformat (#5250)
This introduces newlines before decorators.
2021-04-26 13:58:27 +02:00
Adrian Freund
ff91f5630b Added match_args parameter to dataclass (bpo-43764) (#5212)
https://bugs.python.org/issue43764
2021-04-12 15:42:43 -07:00
Ivan Levkivskyi
16ae4c6120 Re-organize directory structure (#4971)
See discussion in #2491

Co-authored-by: Ivan Levkivskyi <ilevkivskyi@dropbox.com>
2021-01-27 12:00:39 +00:00