Skip to content

Commit 34ff8f4

Browse files
committed
Fix fast_replace annotation
1 parent 24e8a13 commit 34ff8f4

1 file changed

Lines changed: 9 additions & 5 deletions

File tree

src/ezmsg/util/messages/util.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,36 @@
11
from dataclasses import replace as slow_replace
22
import os
3-
import typing
3+
from typing import Any, TypeVar
44

55

6-
T = typing.TypeVar("T")
6+
T = TypeVar("T")
77

88

9-
def fast_replace(arr: typing.Generic[T], **kwargs) -> T:
9+
def fast_replace(arr: T, **kwargs: Any) -> T:
1010
"""
1111
Fast replacement of dataclass fields with reduced safety.
1212
1313
Unlike dataclasses.replace, this function does not check for type compatibility,
1414
nor does it check that the passed in fields are valid fields for the dataclass
1515
and not flagged as init=False.
1616
17+
BEWARE: This function is not type safe and may lead to runtime errors if
18+
used incorrectly. It implicitly assumes arr has a __dict__ attribute and
19+
that kwargs are valid init parameters for the dataclass of arr.
20+
1721
User code may choose to use this replace or the legacy replace according to their needs.
1822
To force ezmsg to use the legacy replace, set the environment variable:
1923
EZMSG_DISABLE_FAST_REPLACE
2024
Unset the variable to use this replace function.
2125
2226
:param arr: The dataclass instance to create a modified copy of.
23-
:type arr: typing.Generic[T]
27+
:type arr: T
2428
:param kwargs: Field values to update in the new instance.
2529
:return: A new instance of the same type with updated field values.
2630
:rtype: T
2731
"""
2832
out_kwargs = arr.__dict__.copy() # Shallow copy
29-
out_kwargs.update(**kwargs)
33+
out_kwargs.update(kwargs)
3034
return arr.__class__(**out_kwargs)
3135

3236

0 commit comments

Comments
 (0)