Commit graph

110 commits

Author SHA1 Message Date
Victor Stinner 4c6d4f5cb3
gh-120417: Remove unused imports in the stdlib (#120420) 2024-06-12 20:56:42 +02:00
Nikita Sobolev fa9b9cb113
gh-118033: Fix __weakref__ not set for generic dataclasses (#118099) 2024-05-09 11:36:17 +03:00
Eric V. Smith 8945b7ff55
gh-109870: Dataclasses: batch up exec calls (gh-110851)
Instead of calling `exec()` once for each function added to a dataclass, only call `exec()` once per dataclass. This can lead to speed improvements of up to 20%.
2024-03-25 19:59:14 -04:00
et-repositories 75935746be
gh-116647: Fix recursive child in dataclasses (#116790) 2024-03-19 08:58:40 -06:00
Aviel Boag a22d05f04c
gh-105866: fix dataclass with slots=True, weakref_slot=True (#105870)
Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com>
Co-authored-by: Kirill Podoprigora <kirill.bast9@mail.ru>
Co-authored-by: Carl Meyer <carl@oddbird.net>
2024-03-18 18:53:14 -06:00
Nikita Sobolev edc9d85c68
gh-109653: Just import recursive_repr in dataclasses (gh-109822) 2024-03-05 13:12:00 -05:00
keithasaurus 2d3f6b56c5
gh-114087: Speed up dataclasses._asdict_inner (#114088) 2024-01-18 09:03:20 -07:00
Phillip Schanely 339fc3c224
gh-114198: Rename dataclass __replace__ argument to 'self' (gh-114251)
This change renames the dataclass __replace__ method's first argument
name from 'obj' to 'self'.
2024-01-18 11:01:17 -05:00
Nikita Sobolev b6000d2874
gh-109409: Fix inheritance of frozen dataclass from non-frozen dataclass mixins (gh-109437)
Fix inheritance of frozen dataclass from non-frozen dataclass mixins
2023-10-12 09:05:23 -04:00
Serhiy Storchaka 5b9a3fd6a0
gh-110273: dataclasses.replace() now raise TypeError for all invalid arguments (GH-110274)
dataclasses.replace() now raises TypeError instead of ValueError if
specify keyword argument for a field declared with init=False or miss keyword
argument for required InitVar field.
2023-10-04 09:20:14 +03:00
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