Skip to content

Commit

Permalink
make flax.core.copy add_or_replace optional
Browse files Browse the repository at this point in the history
ignore .envrc (direnv files)


format


add test


fix
  • Loading branch information
PhilipVinc committed Jul 31, 2023
1 parent 675e34d commit 940ff5d
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,7 @@ build/
.pytype
.vscode/*
/.devcontainer
docs/**/tmp
docs/**/tmp

# used by direnv
.envrc
8 changes: 6 additions & 2 deletions flax/core/frozen_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"""Frozen Dictionary."""

import collections
from typing import Any, TypeVar, Mapping, Dict, Tuple, Union, Hashable
from typing import Any, Dict, Hashable, Optional, Mapping, Tuple, TypeVar, Union

from flax import serialization
import jax
Expand Down Expand Up @@ -111,7 +111,11 @@ def __hash__(self):
self._hash = h
return self._hash

def copy(self, add_or_replace: Mapping[K, V]) -> 'FrozenDict[K, V]':
def copy(
self, add_or_replace: Optional[Mapping[K, V]] = None
) -> 'FrozenDict[K, V]':
if add_or_replace is None:
add_or_replace = {}
"""Create a new FrozenDict with additional or replaced entries."""
return type(self)({**self, **unfreeze(add_or_replace)}) # type: ignore[arg-type]

Expand Down
12 changes: 12 additions & 0 deletions tests/core/core_frozen_dict_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,18 @@ def test_utility_copy(self, x, add_or_replace, actual_new_x):
new_x == actual_new_x and isinstance(new_x, type(actual_new_x))
)

@parameterized.parameters(
{
'x': {'a': 1, 'b': {'c': 2}},
},
{
'x': FrozenDict({'a': 1, 'b': {'c': 2}}),
},
)
def test_utility_copy_singlearg(self, x):
new_x = copy(x)
self.assertTrue(new_x == x and isinstance(new_x, type(x)))

@parameterized.parameters(
{
'x': {'a': 1, 'b': {'c': 2}},
Expand Down

0 comments on commit 940ff5d

Please sign in to comment.