Commit graph

124 commits

Author SHA1 Message Date
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
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
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
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