Commit graph

129 commits

Author SHA1 Message Date
Arseny Boykov
c68b4cc08f
Use inspect.getattr_static instead of _safe_get_attributes
This uses builtins.dir, which will trigger custom __getattibute__ if any, and will trigger __get__ on __dict__ descriptor.
2024-10-05 14:41:17 +02:00
Arseny Boykov
d0173d8e39
Prevent user-defined code execution during attribute scanning 2024-09-28 22:37:06 +02:00
Arseny Boykov
c26e978cce
Remove leading underscores from seen and depth args 2024-09-28 13:09:13 +02:00
Arseny Boykov
d33070f21e
Apply suggestions from code review
Co-authored-by: sobolevn <mail@sobolevn.me>
2024-09-28 13:03:14 +02:00
Arseny Boykov
4941a3fdb2 Use generator expression with any() instead of classic loop 2024-09-28 01:39:39 +02:00
Arseny Boykov
e91c28bcaf Fix breaking out from outer loop when handling property 2024-09-28 01:35:46 +02:00
Arseny Boykov
22eeb8f762 Ues consistent grammar throughout comments 2024-09-28 01:21:52 +02:00
Arseny Boykov
96d5315531 Add clarification for function search priority 2024-09-28 01:19:48 +02:00
Arseny Boykov
53c18d114b Rephrase comment explaining _depth argument 2024-09-28 01:03:03 +02:00
Arseny Boykov
2606480ffa Correctly unwrap descriptor function 2024-09-28 01:01:27 +02:00
Arseny Boykov
19297bc9ee Fix incorrect variable reference 2024-09-28 00:42:42 +02:00
Arseny Boykov
3654f51b82 Add explanation for _depth use and tests 2024-09-28 00:42:26 +02:00
Arseny Boykov
3b65acf8d6 Improve zero argument support for super() in dataclasses 2024-09-27 20:10:37 +02:00
sobolevn
9c7657f099
gh-113878: Add doc parameter to dataclasses.field (gh-114051)
If using `slots=True`, the `doc` parameter ends up in the `__slots__` dict. The `doc` parameter is also in the corresponding `Field` object.
2024-09-27 12:20:49 -04:00
Eric V. Smith
5c6e3b7150
gh-90562: Support zero argument super with dataclasses when slots=True (gh-124455)
Co-authored-by: @wookie184
Co-authored-by: Carl Meyer <carl@oddbird.net>
2024-09-24 21:26:26 -04:00
Jelle Zijlstra
950fab46ad
dataclasses: Avoid using private class (#124465)
typing.get_origin() does what we need here, without reaching into
typing internals. This shouldn't change any behavior (so I am going
to skip news), but it sets a good example for other users introspecting
typing objects.
2024-09-24 14:02:18 -07:00
sobolevn
ac918ccad7
gh-123935: Fix typo in _get_slots in dataclasses.py (#123941) 2024-09-12 10:29:06 +03:00
Xie Yanbo
253c6a0b2f
Fix typos in comments and test code (#122846) 2024-08-11 21:16:41 -07:00
Jelle Zijlstra
7b7b90d1ce
gh-119180: Add annotationlib module to support PEP 649 (#119891)
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
2024-07-23 21:16:50 +00:00
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