Skip to content
Merged
Changes from 1 commit
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
7863721
Create FuncBuilder class.
ericvsmith Oct 13, 2023
54d5b11
More work on combining exec calls.
ericvsmith Oct 13, 2023
0d28b3e
Fix __doc__ computation by moving it after __init__ has been set.
ericvsmith Oct 13, 2023
fc26b17
Fix recursive repr.
ericvsmith Oct 14, 2023
6d2af0a
Added blurb.
ericvsmith Oct 14, 2023
d6b3c05
Merge branch 'main' into dataclasses-batch-exec
ericvsmith Oct 14, 2023
6f4f5bf
Merge branch 'main' of github.com:python/cpython into dataclasses-bat…
ericvsmith Oct 14, 2023
402f9e1
Changed __repr__ to use _recursive_repr as a decorator.
ericvsmith Oct 14, 2023
809d392
Use a nested f-string instead of '+' to build the method decorator.
ericvsmith Oct 14, 2023
b788858
Change 'create_fn' back to '__create_fn__', because pprint looks for …
ericvsmith Oct 18, 2023
cf9e424
Merge branch 'main' into dataclasses-batch-exec
ericvsmith Mar 24, 2024
8f6187d
Merge error: Fix missing close paren.
ericvsmith Mar 24, 2024
7925ad8
Merge error: recursive_repr changes.
ericvsmith Mar 24, 2024
211fe70
Merge error: use func_builder.
ericvsmith Mar 24, 2024
80371d2
Merge branch 'main' of github.com:python/cpython into dataclasses-bat…
ericvsmith Mar 24, 2024
9a2ca67
Call the recursive_repr decorator.
ericvsmith Mar 24, 2024
b41be1b
Pre-build the function body strings with required 2 spaces, so they d…
ericvsmith Mar 25, 2024
c3b7edf
Add a note about how much speedup can be expected from batching up ca…
ericvsmith Mar 25, 2024
e8c224f
Fix markup for "exec".
ericvsmith Mar 25, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Pre-build the function body strings with required 2 spaces, so they d…
…on't have to be added later.
  • Loading branch information
ericvsmith committed Mar 25, 2024
commit b41be1bdc2e36f84dea49012a849a42161c7c165
42 changes: 21 additions & 21 deletions Lib/dataclasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,7 @@ def add_fn(self, name, args, body, *, locals=None, return_type=MISSING,
else:
return_annotation = ''
args = ','.join(args)
body = '\n'.join(f' {b}' for b in body)
body = '\n'.join(body)

# Compute the text of the entire function, add it to the text we're generating.
self.src.append(f'{f' {decorator}\n' if decorator else ''} def {name}({args}){return_annotation}:\n{body}')
Expand Down Expand Up @@ -525,8 +525,8 @@ def _field_assign(frozen, name, value, self_name):
# self_name is what "self" is called in this function: don't
# hard-code "self", since that might be a field name.
if frozen:
return f'__dataclass_builtins_object__.__setattr__({self_name},{name!r},{value})'
return f'{self_name}.{name}={value}'
return f' __dataclass_builtins_object__.__setattr__({self_name},{name!r},{value})'
return f' {self_name}.{name}={value}'


def _field_init(f, frozen, globals, self_name, slots):
Expand Down Expand Up @@ -646,11 +646,11 @@ def _init_fn(fields, std_fields, kw_only_fields, frozen, has_post_init,
if has_post_init:
params_str = ','.join(f.name for f in fields
if f._field_type is _FIELD_INITVAR)
body_lines.append(f'{self_name}.{_POST_INIT_NAME}({params_str})')
body_lines.append(f' {self_name}.{_POST_INIT_NAME}({params_str})')

# If no body lines, use 'pass'.
if not body_lines:
body_lines = ['pass']
body_lines = [' pass']

_init_params = [_init_param(f) for f in std_fields]
if kw_only_fields:
Expand All @@ -675,16 +675,16 @@ def _frozen_get_del_attr(cls, fields, func_builder):

func_builder.add_fn('__setattr__',
('self', 'name', 'value'),
(f'if {condition}:',
' raise FrozenInstanceError(f"cannot assign to field {name!r}")',
f'super(cls, self).__setattr__(name, value)'),
(f' if {condition}:',
' raise FrozenInstanceError(f"cannot assign to field {name!r}")',
f' super(cls, self).__setattr__(name, value)'),
locals=locals,
overwrite_error=True)
func_builder.add_fn('__delattr__',
('self', 'name'),
(f'if {condition}:',
' raise FrozenInstanceError(f"cannot delete field {name!r}")',
f'super(cls, self).__delattr__(name)'),
(f' if {condition}:',
' raise FrozenInstanceError(f"cannot delete field {name!r}")',
f' super(cls, self).__delattr__(name)'),
locals=locals,
overwrite_error=True)

Expand Down Expand Up @@ -889,7 +889,7 @@ def _hash_add(cls, fields, func_builder):
self_tuple = _tuple_str('self', flds)
func_builder.add_fn('__hash__',
('self',),
[f'return hash({self_tuple})'],
[f' return hash({self_tuple})'],
unconditional_add=True)

def _hash_exception(cls, fields, func_builder):
Expand Down Expand Up @@ -1100,7 +1100,7 @@ def _process_class(cls, init, repr, eq, order, unsafe_hash, frozen,
flds = [f for f in field_list if f.repr]
func_builder.add_fn('__repr__',
('self',),
['return f"{self.__class__.__qualname__}(' +
[' return f"{self.__class__.__qualname__}(' +
', '.join([f"{f.name}={{self.{f.name}!r}}"
for f in flds]) + ')"'],
locals={'__dataclasses_recursive_repr': recursive_repr},
Expand All @@ -1114,11 +1114,11 @@ def _process_class(cls, init, repr, eq, order, unsafe_hash, frozen,
field_comparisons = ' and '.join(terms) or 'True'
func_builder.add_fn('__eq__',
('self', 'other'),
[ 'if self is other:',
' return True',
'if other.__class__ is self.__class__:',
f' return {field_comparisons}',
'return NotImplemented'])
[ ' if self is other:',
' return True',
' if other.__class__ is self.__class__:',
f' return {field_comparisons}',
' return NotImplemented'])

if order:
# Create and set the ordering methods.
Expand All @@ -1136,9 +1136,9 @@ def _process_class(cls, init, repr, eq, order, unsafe_hash, frozen,
# '(other.x,other.y)'.
func_builder.add_fn(name,
('self', 'other'),
[ 'if other.__class__ is self.__class__:',
f' return {self_tuple}{op}{other_tuple}',
'return NotImplemented'],
[ ' if other.__class__ is self.__class__:',
f' return {self_tuple}{op}{other_tuple}',
' return NotImplemented'],
overwrite_error='Consider using functools.total_ordering')

if frozen:
Expand Down