Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix default cache dataloader raise key error on non-existing key #3569

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
12 changes: 12 additions & 0 deletions RELEASE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
Release type: patch

Calling `.clean(key)` on default dataloader with non-existing `key` will not throw `KeyError` error anymore. Example:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue (documentation): Redundant phrase 'KeyError error'

Consider changing 'KeyError error' to just 'KeyError' to avoid redundancy.

```python
async def load_data(keys):
return [str(key) for key in keys]

dataloader = DataLoader(load_fn=load_data)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion (documentation): Mention import statement for DataLoader

Consider adding a comment or a line to indicate that DataLoader should be imported from the relevant module to avoid confusion.

Suggested change
dataloader = DataLoader(load_fn=load_data)
```suggestion
```python
from some_module import DataLoader
dataloader = DataLoader(load_fn=load_data)

dataloader.clean(42) # does not throw KeyError anymore
```

This is a patch release, so no breaking changes.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion (documentation): Improve sentence flow

Consider rephrasing to 'This is a patch release with no breaking changes.'

Suggested change
This is a patch release, so no breaking changes.
This is a patch release with no breaking changes.

2 changes: 1 addition & 1 deletion strawberry/dataloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def set(self, key: K, value: Future[T]) -> None:
self.cache_map[self.cache_key_fn(key)] = value

def delete(self, key: K) -> None:
del self.cache_map[self.cache_key_fn(key)]
self.cache_map.pop(self.cache_key_fn(key), default=None)

def clear(self) -> None:
self.cache_map.clear()
Expand Down
2 changes: 2 additions & 0 deletions tests/test_dataloaders.py
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,8 @@ async def idx(keys: List[int]) -> List[Tuple[int, int]]:

loader = DataLoader(load_fn=idx, cache=False)

loader.clean(1) # no effect on non-cached values
Dartt0n marked this conversation as resolved.
Show resolved Hide resolved

assert await loader.load_many([1, 2, 3]) == [(1, 1), (2, 1), (3, 1)]

loader.clear(1)
Expand Down
Loading