Commit Graph

92 Commits

Author SHA1 Message Date
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 654d44b3a4
gh-104015: Fix direct invocation of `test_dataclasses` (#104017)
Previously, `python -m test test_dataclasses` passed, but `./python.exe Lib/test/test_dataclasses.py` failed
2023-04-30 17:16:55 +00:00
Rodolfo M. Pereira 8291ae31dd
GH-103805: Lib test f541 linting issue fix (#103812)
This PR makes some minor linting adjustments to the Lib/test module
caught by [ruff](https://github.com/charliermarsh/ruff). The adjustments
are all related to the `F541 f-string without any placeholders` issue.

Issue: https://github.com/python/cpython/issues/103805

<!-- gh-issue-number: gh-103805 -->
* Issue: gh-103805
<!-- /gh-issue-number -->

---------

Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com>
Co-authored-by: Gregory P. Smith <greg@krypto.org>
2023-04-24 23:24:49 +00:00
Nikita Sobolev b57f55c23e
gh-103449: Fix a bug in dataclass docstring generation (#103454) 2023-04-17 17:33:22 -06: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
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
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
Kaushik Kulkarni 67ade403a2
gh-98284: better error message for undefined abstractmethod (#97971) 2022-11-05 09:31:57 -07: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
da-woods 59e09efe88
Fix typo in test_dataclasses.py (gh-95735)
`dataclass` was called as a function when it was almost certainly intended to be a decorator.
2022-08-06 11:56:00 -04:00
Erik De Bonte 5f319308a8
gh-91330: Tests and docs for dataclass descriptor-typed fields (GH-94424)
Co-authored-by: Łukasz Langa <lukasz@langa.pl>
2022-07-05 20:09:41 +02: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
ravi140222 079f0dd719
gh-91578: improved error message when trying to instantiate an abstract class with missing methods (gh-47246) 2022-05-12 11:26:11 -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
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 288af845a3
bpo-46757: Add a test to verify dataclass's __post_init__ isn't being automatically added. (GH-31523) 2022-02-23 00:14:35 -05: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
Nikita Sobolev 07236d562e
bpo-45566: `test_frozen_pickle` checks all `pickle` protocols (GH-29150)
Refs https://github.com/python/cpython/pull/29147

Automerge-Triggered-By: GH:ericvsmith
2021-10-24 06:06:32 -07:00
Landon Yarrington 5b4a7675bc
Fix dataclassses spelling (GH-28837) 2021-10-09 15:17:52 -04:00
Christian Clauss 745c9d9dfc
Fix typos in the Lib directory (GH-28775)
Fix typos in the Lib directory as identified by codespell.

Co-authored-by: Terry Jan Reedy <tjreedy@udel.edu>
2021-10-06 16:13:48 -07:00
Yurii Karabas 0635e201be
bpo-45081: Fix __init__ method generation when inheriting from Protocol (GH-28121)
Co-authored-by: Ken Jin <28750310+Fidget-Spinner@users.noreply.github.com>
2021-09-02 18:17:13 +02: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 94549ee728
Add additional keyword-only tests. (GH-25633) 2021-04-26 13:14:28 -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 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
Brandt Bucher f84d5a1136
bpo-42128: __match_args__ can't be a list anymore (GH-25203) 2021-04-05 19:17:08 -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
Batuhan Taskaya c7437e2c02
bpo-41747: Ensure all dataclass methods uses their parents' qualname (GH-22155)
* bpo-41747: Ensure all dataclass methods uses their parents' qualname

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
2020-10-21 09:49:22 -04:00
Batuhan Taskaya 044a1048ca
bpo-38605: Make 'from __future__ import annotations' the default (GH-20434)
The hard part was making all the tests pass; there are some subtle issues here, because apparently the future import wasn't tested very thoroughly in previous Python versions.

For example, `inspect.signature()` returned type objects normally (except for forward references), but strings with the future import. We changed it to try and return type objects by calling `typing.get_type_hints()`, but fall back on returning strings if that function fails (which it may do if there are future references in the annotations that require passing in a specific namespace to resolve).
2020-10-06 13:03:02 -07:00
Ben Avrahami bef7d299eb
bpo-41905: Add abc.update_abstractmethods() (GH-22485)
This function recomputes `cls.__abstractmethods__`.
Also update `@dataclass` to use it.
2020-10-06 10:40:50 -07:00
Vlad Serebrennikov 138a9b9c2a
bpo-40389: Improve repr of typing.Optional (#19714) 2020-04-29 18:06:39 -07:00
Karthikeyan Singaravelan eef1b027ab Add test cases for dataclasses. (#17909)
* Add test cases for dataclasses.

* Add test for repr output of field.
* Add test for ValueError to be raised when both default and default_factory are passed.
2020-01-09 08:41:46 -05:00
Yury Selivanov d219cc4180 bpo-34776: Fix dataclasses to support __future__ "annotations" mode (#9518) 2019-12-09 15:54:20 +01:00