Commit graph

140 commits

Author SHA1 Message Date
Arseny Boykov
c6b293f6c3
Merge 5bfea4d299 into bad8d6de37 2025-10-19 16:25:40 +02:00
Petr Viktorin
7dfa048bbb
gh-135228: Create __dict__ and __weakref__ descriptors for object (GH-136966)
This partially reverts #137047, keeping the tests for GC collectability of the
original class that dataclass adds `__slots__` to.
The reference leaks solved there are instead solved by having the `__dict__` &
`__weakref__` descriptors not tied to (and referencing) their class.

Instead, they're shared between all classes that need them (within
an interpreter).
The `__objclass__` ol the descriptors is set to `object`, since these
descriptors work with *any* object. (The appropriate checks were already
made in the get/set code, so the `__objclass__` check was redundant.)

The repr of these descriptors (and any others whose `__objclass__` is `object`)
now doesn't mention the objclass.

This change required adjustment of introspection code that checks
`__objclass__` to determine an object's “own” (i.e. not inherited) `__dict__`.
Third-party code that does similar introspection of the internals will also
need adjusting.


Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
2025-08-18 14:25:51 +02:00
Jelle Zijlstra
6859b95cff
gh-135228: When @dataclass(slots=True) replaces a dataclass, make the original class collectible (take 2) (GH-137047)
Remove the `__dict__` and `__weakref__` descriptors from the original class when creating a dataclass from it.

An interesting hack, but more localized in scope than gh-135230.

This may be a breaking change if people intentionally keep the original class around
when using `@dataclass(slots=True)`, and then use `__dict__` or `__weakref__` on the
original class.


Co-authored-by: Alyssa Coghlan <ncoghlan@gmail.com>
Co-authored-by: Petr Viktorin <encukou@gmail.com>
Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
2025-08-12 13:16:54 +02:00
Jelle Zijlstra
6a285f94c6
Revert "gh-135228: When @dataclass(slots=True) replaces a dataclass, make the original class collectible (#136893)" (#137014)
This reverts commit 46cbdf967a.
2025-07-23 08:13:19 -07:00
Jelle Zijlstra
46cbdf967a
gh-135228: When @dataclass(slots=True) replaces a dataclass, make the original class collectible (#136893)
An interesting hack, but more localized in scope than #135230.

This may be a breaking change if people intentionally keep the original class around
when using `@dataclass(slots=True)`, and then use `__dict__` or `__weakref__` on the
original class.

Co-authored-by: Alyssa Coghlan <ncoghlan@gmail.com>
2025-07-21 21:43:34 -07:00
Disconnect3d
69ea1b3a8f
gh-136839: Refactor simple dict.update calls (#136811)
Refactor simple dict.update calls

This commit refactors simple `dict.update({key: value})` calls which can
be done via `dict[key] = value` instead.

I found those cases with the [semgrep](https://semgrep.dev/) tool:

```
$ semgrep --lang python --pattern '$DICT.update({$A: ...})'

┌─────────────────┐
│ 5 Code Findings │
└─────────────────┘

    Lib/dataclasses.py
         1268┆ slots.update({slot: doc})

    Lib/multiprocessing/resource_tracker.py
           50┆ _CLEANUP_FUNCS.update({
           51┆     'semaphore': _multiprocessing.sem_unlink,
           52┆ })
            ⋮┆----------------------------------------
           53┆ _CLEANUP_FUNCS.update({
           54┆     'shared_memory': _posixshmem.shm_unlink,
           55┆ })

    Lib/tkinter/scrolledtext.py
           26┆ kw.update({'yscrollcommand': self.vbar.set})

    Lib/xmlrpc/server.py
          242┆ self.funcs.update({'system.multicall' : self.system_multicall})
```
2025-07-19 10:12:10 -07:00
Jelle Zijlstra
bb5ec6ea6e
gh-82129: Improve annotations for make_dataclass() (#133406)
Co-authored-by: sobolevn <mail@sobolevn.me>
Co-authored-by: Carl Meyer <carl@oddbird.net>
2025-05-05 08:21:32 -07:00
Jelle Zijlstra
1d251b8339
gh-128184: Fix display of signatures with ForwardRefs (#130815)
Co-authored-by: sobolevn <mail@sobolevn.me>
2025-03-04 06:58:37 -08:00
Arseny Boykov
5bfea4d299
Rely on inspect.getmembers_static() 2024-10-05 15:16:54 +02:00
Arseny Boykov
7d679f226d
Put back the comment in _update_func_cell_for__class__ 2024-10-05 14:51:01 +02:00
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
Victorien
3e3a4d2315
gh-118974: Add decorator argument to make_dataclass (gh-122723)
This is to allow the `dataclasses.make_dataclass` infrastructure to be used with another decorator that's compliant with `typing.dataclass_transform`. The new `decorator` argument to `dataclasses.make_dataclass` is `dataclasses.dataclass`, which used to be hard coded.
2024-10-01 09:51:51 -04: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