Serhiy Storchaka
6f3c138dfa
gh-108751: Add copy.replace() function (GH-108752)
...
It creates a modified copy of an object by calling the object's
__replace__() method.
It is a generalization of dataclasses.replace(), named tuple's _replace()
method and replace() methods in various classes, and supports all these
stdlib classes.
2023-09-06 23:55:42 +03:00
Eric V. Smith
e4275f4df3
gh-107838: In dataclasses, improve error message when a non-default field follows a default field. (gh-107842)
...
Add the name of the previous default argument field in an error message.
2023-08-10 10:39:13 -04:00
Tom Niget
2aaa83d5f5
Fix typo in comment (gh-107389)
2023-07-28 10:52:29 -04:00
Raymond Hettinger
18cfc1eea5
Small speedup for dataclass __eq__ and __repr__ ( #104904 )
...
Faster __repr__ with str.__add__ moved inside the f-string. For __eq__ comp;are field by field instead of building temporary tuples.
Co-authored-by: Shantanu <12621235+hauntsaninja@users.noreply.github.com>
2023-05-30 12:35:30 -04:00
Alex Waygood
7b8d7f56b6
gh-103000: Optimise `dataclasses.asdict` for the common case ( #104364 )
...
Co-authored-by: David Ellis <ducksual@gmail.com>
2023-05-10 22:43:51 +01:00
Nikita Sobolev
99aab61062
gh-104035: Do not ignore user-defined `__{get,set}state__` in slotted frozen dataclasses ( #104041 )
2023-05-01 09:19:06 -06:00
Nikita Sobolev
b57f55c23e
gh-103449: Fix a bug in dataclass docstring generation ( #103454 )
2023-04-17 17:33:22 -06:00
David Ellis
d034590294
gh-103000: Optimise dataclasses asdict/astuple for common types ( #103005 )
...
Co-authored-by: Carl Meyer <carl@oddbird.net>
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
2023-04-10 22:50:58 +01:00
Shantanu
718e86671f
gh-98886: Fix issues with dataclass fields with special underscore names ( #102032 )
...
This commit prefixes `__dataclass` to several things in the locals dict:
- Names like `_dflt_` (which cause trouble, see first test)
- Names like `_type_` (not known to be able to cause trouble)
- `_return_type` (not known to able to cause trouble)
- `_HAS_DEFAULT_FACTORY` (which causes trouble, see second test)
In addition, this removes `MISSING` from the locals dict. As far as I can tell, this wasn't needed even in the initial implementation of dataclasses.py (and tests on that version passed with it removed). This makes me wary :-)
This is basically a continuation of #96151 , where fixing this was welcomed in https://github.com/python/cpython/pull/98143#issuecomment-1280306360
2023-03-25 14:40:11 -07:00
Nikita Sobolev
8ec6486462
gh-103027: Update `dataclass.make_dataclass` docstring (gh-103028)
...
* gh-103027: Update `dataclass.make_dataclass` docstring
2023-03-25 15:36:38 -04:00
Alex Waygood
baf4eb083c
gh-102947: Improve traceback when calling `fields()` on a non-dataclass ( #102948 )
2023-03-23 14:17:54 +00:00
Nikita Sobolev
d97757f793
gh-102069: Fix `__weakref__` descriptor generation for custom dataclasses ( #102075 )
2023-03-13 15:42:05 -06:00
T
71e37d9079
gh-98169 dataclasses.astuple support DefaultDict ( #98170 )
...
Co-authored-by: Pieter Eendebak <pieter.eendebak@gmail.com>
2023-03-13 14:46:35 -06:00
Ilya V. Schurov
767d3a8f6f
gh-88071: Update docstrings of dataclass' astuple and asdict ( #101806 )
...
Update dataclasses.astuple and dataclasses.asdict docstrings to reflect that they deep copy objects in the field values.
2023-03-10 17:27:41 -07:00
Nikita Sobolev
b48be8fa18
gh-102103: add `module` argument to `dataclasses.make_dataclass` ( #102104 )
2023-03-10 17:26:46 -07:00
Xuehai Pan
ee6f8413a9
gh-102578: Optimise setting and deleting mutable attributes on non-dataclass subclasses of frozen dataclasses (gh-102573)
2023-03-10 19:21:22 -05:00
Alex Waygood
401d7a7f00
gh-102515: Remove unused imports in the `Lib/` directory ( #102516 )
2023-03-08 11:45:38 +00:00
Carl Meyer
0a7936a38f
gh-90104: avoid RecursionError on recursive dataclass field repr (gh-100756)
...
Avoid RecursionError on recursive dataclass field repr
2023-01-05 19:19:40 -05:00
Shantanu
29f98b46b7
gh-96151: Use a private name for passing builtins to dataclass. This now allows for a field named BUILTIN (gh-98143)
2022-10-31 08:31:01 -04:00
Tiger
c46a423a52
bpo-35540 dataclasses.asdict now supports defaultdict fields (gh-32056)
2022-10-06 17:11:59 -07:00
Nikita Sobolev
4f380db1a5
gh-96142: add missing params to `dataclass._DataclassParams` (gh-96382)
2022-10-04 09:53:28 -07:00
Shantanu
72b5a55bc8
Add comment to subtle dataclass code (gh-96133)
...
In the PR that made this change, 1st1 left a "note to self: add a
comment explaining this". This comment was never added.
https://github.com/python/cpython/pull/9518/files#r280608117
I was reading this code and it wasn't obvious to me why we weren't
exec-ing directly into locals. So I got to learn something new :-)
https://docs.python.org/3/reference/executionmodel.html#interaction-with-dynamic-features
2022-10-03 17:30:04 -07:00
larryhastings
00b5a08c80
gh-97799: use inspect.get_annotations in dataclass ( #97800 )
...
dataclass used to get the annotations on a class object using
cls.__dict__.get('__annotations__'). Now that it always imports
inspect, it can use inspect.get_annotations, which is modern
best practice for coping with annotations.
2022-10-03 15:46:09 -07:00
Roman Novak
b8c528694e
Fix minor docstring issues in `dataclasses.py`. (gh-93024)
...
Previously, when using `functools.wrap` around them (and inherit their docstrings), sphinx renders the docstrings badly and raises warnings about wrong indent.
2022-07-26 10:48:58 -04:00
Tom Fryers
a10cf2f6b3
Improve dataclass docstring (gh-94686)
2022-07-09 14:26:24 -04:00
Serhiy Storchaka
f9433fff47
gh-89828: Do not relay the __class__ attribute in GenericAlias ( #93754 )
...
list[int].__class__ returned type, and isinstance(list[int], type)
returned True. It caused numerous problems in code that checks
isinstance(x, type).
2022-06-18 11:34:57 +03:00
Bluenix
5849af7a80
GH-93521: For dataclasses, filter out `__weakref__` slot if present in bases (GH-93535)
2022-06-07 20:53:08 -04:00
Eric V. Smith
5f9c0f5ddf
Add weakref_slot to dataclass decorator, to allow instances with slots to be weakref-able. ( #92160 )
2022-05-02 10:36:39 -06:00
Hugo van Kemenade
6881ea936e
bpo-47126: Update to canonical PEP URLs specified by PEP 676 (GH-32124)
2022-03-30 12:00:27 +01:00
Arie Bovenberg
82e9b0bb0a
bpo-46382 dataclass(slots=True) now takes inherited slots into account (GH-31980)
...
Do not include any members in __slots__ that are already in a base class's __slots__.
2022-03-19 17:01:17 -04:00
Eric V. Smith
e029c53e1a
bpo-44674: Use unhashability as a proxy for mutability for default dataclass __init__ arguments. (GH-29867)
...
`@dataclass` in 3.10 prohibits using list, dict, or set as default values. It does this to avoid the mutable default problem. This test is both too strict, and not strict enough. Too strict, because some immutable subclasses should be safe, and not strict enough, because other mutable types should be prohibited. With this change applied, `@dataclass` now uses unhashability as a proxy for mutability: if objects aren't hashable, they're assumed to be mutable.
2021-12-11 16:12:17 -05:00
Serhiy Storchaka
446be16686
bpo-45663: Fix is_dataclass() for dataclasses which are subclasses of types.GenericAlias (GH-29294)
2021-12-05 22:42:50 +02:00
Serhiy Storchaka
1fd4de5bdd
bpo-45662: Fix the repr of InitVar with a type alias to the built-in class (GH-29291)
...
For example, InitVar[list[int]].
2021-12-05 22:41:58 +02:00
Eric V. Smith
d3062f672c
bpo-44649: Fix dataclasses(slots=True) with a field with a default, but init=False (GH-29692)
...
Special handling is needed, because for non-slots dataclasses the instance attributes are not set: reading from a field just references the class's attribute of the same name, which contains the default value. But this doesn't work for classes using __slots__: they don't read the class's attribute. So in that case (and that case only), initialize the instance attribute. Handle this for both normal defaults, and for fields using default_factory.
2021-11-22 08:26:12 -05:00
Eric V. Smith
f7638dd0f9
Added kw_only parameter to make_dataclasses. (GH-29679)
2021-11-20 18:25:56 -05:00
Landon Yarrington
5b4a7675bc
Fix dataclassses spelling (GH-28837)
2021-10-09 15:17:52 -04:00
Binbin
17b16e13bb
Fix typos in multiple files (GH-26689)
...
Co-authored-by: Terry Jan Reedy <tjreedy@udel.edu>
2021-06-12 22:47:44 -04:00
Sean Grady
7e6f237569
Fix dataclass comment typo _eq__ -> __eq__ (GH-26433)
2021-05-28 16:57:18 -04:00
Eric V. Smith
99ad742ea9
bpo-44015: dataclasses should allow KW_ONLY to be specified only once per class (GH-25841)
...
bpo-44015: Raise a TypeError if KW_ONLY is specified more than once.
2021-05-03 03:24:53 -04:00
Eric V. Smith
823fbf4e0e
If using a frozen class with slots, add __getstate__ and __setstate__ to set the instance values. (GH-25786)
2021-05-01 13:27:30 -04:00
Yurii Karabas
c24199184b
bpo-42269: Add slots parameter to dataclass decorator (GH-24171)
...
Add slots parameter to dataclass decorator and make_dataclass function.
2021-04-30 22:14:30 -04:00
Eric V. Smith
c0280532dc
Add keyword-only fields to dataclasses. (GH=25608)
2021-04-25 20:42:39 -04:00
Pablo Galindo
b0544ba77c
bpo-38605: Revert making 'from __future__ import annotations' the default (GH-25490)
...
This reverts commits 044a1048ca
and 1be456ae9d
, adapting the code to changes that happened after it.
2021-04-21 12:41:19 +01:00
Eric V. Smith
76beadb8ff
Fix typo in a dataclasses comment. (GH-25454)
2021-04-17 09:53:24 -04:00
Eric V. Smith
c1a66bdd6f
Remove an unnecessary copy of the 'namespace' parameter to make_dataclass(). (GH-25372)
2021-04-12 21:02:02 -04:00
Eric V. Smith
750f484752
bpo-43764: Add match_args=False parameter to dataclass decorator and to make_dataclasses function. (GH-25337)
...
Add match_args=False parameter to dataclass decorator and to make_dataclass function.
2021-04-10 21:28:42 -04:00
Brandt Bucher
d92c59f486
bpo-43764: Fix `__match_args__` generation logic for dataclasses (GH-25284)
2021-04-08 12:54:34 -07:00
Iurii Kemaev
376ffc6ac4
bpo-43176: Fix processing of empty dataclasses (GH-24484)
...
When a dataclass inherits from an empty base, all immutability checks are omitted. This PR fixes this and adds tests for it.
Automerge-Triggered-By: GH:ericvsmith
2021-04-05 22:14:01 -07:00
Zackery Spytz
75220674c0
bpo-36470: Allow dataclasses.replace() to handle InitVars with default values (GH-20867)
...
Co-Authored-By: Claudiu Popa <pcmanticore@gmail.com>
Automerge-Triggered-By: GH:ericvsmith
2021-04-05 12:41:01 -07:00
Brandt Bucher
145bf269df
bpo-42128: Structural Pattern Matching (PEP 634) (GH-22917)
...
Co-authored-by: Guido van Rossum <guido@python.org>
Co-authored-by: Talin <viridia@gmail.com>
Co-authored-by: Pablo Galindo <pablogsal@gmail.com>
2021-02-26 14:51:55 -08:00