Skip to content
This repository was archived by the owner on Apr 26, 2024. It is now read-only.

Use auto_attribs/native type hints for attrs classes. #11692

Merged
merged 20 commits into from
Jan 13, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
Use auto-attribs in synapse.util.
  • Loading branch information
clokep committed Jan 6, 2022
commit e475fd7b8da6e7bdc988a0c27dabcdc0476be280
6 changes: 3 additions & 3 deletions synapse/util/async_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,12 +309,12 @@ def gather_results( # type: ignore[misc]
return deferred.addCallback(tuple)


@attr.s(slots=True)
@attr.s(slots=True, auto_attribs=True)
class _LinearizerEntry:
# The number of things executing.
count = attr.ib(type=int)
count: int
# Deferreds for the things blocked from executing.
deferreds = attr.ib(type=collections.OrderedDict)
deferreds: collections.OrderedDict
Copy link
Contributor

Choose a reason for hiding this comment

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

I wonder if this could just be dict now we're on 3.7+? Probably not worth changing.

Copy link
Member Author

Choose a reason for hiding this comment

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

I think keeping it as OrderedDict is clearer for now!



class Linearizer:
Expand Down
11 changes: 5 additions & 6 deletions synapse/util/caches/dictionary_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,22 +33,21 @@

# This class can't be generic because it uses slots with attrs.
# See: https://github.com/python-attrs/attrs/issues/313
@attr.s(slots=True)
@attr.s(slots=True, auto_attribs=True)
class DictionaryEntry: # should be: Generic[DKT, DV].
"""Returned when getting an entry from the cache

Attributes:
full: Whether the cache has the full or dict or just some keys.
If not full then not all requested keys will necessarily be present
in `value`
known_absent: Keys that were looked up in the dict and were not
there.
known_absent: Keys that were looked up in the dict and were not there.
value: The full or partial dict value
"""

full = attr.ib(type=bool)
known_absent = attr.ib(type=Set[Any]) # should be: Set[DKT]
value = attr.ib(type=Dict[Any, Any]) # should be: Dict[DKT, DV]
full: bool
known_absent: Set[Any] # should be: Set[DKT]
value: Dict[Any, Any] # should be: Dict[DKT, DV]
Comment on lines -36 to +50
Copy link
Contributor

Choose a reason for hiding this comment

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

python-attrs/attrs#313 again! Argh.

Copy link
Contributor

Choose a reason for hiding this comment

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

Now we're on 3.7 I wonder if we can use a stdlib dataclass here? We're not using any of the extra attrs machinery here.

Copy link
Member Author

Choose a reason for hiding this comment

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

Maybe, but I'd rather that be left to a separate PR.

Copy link
Contributor

Choose a reason for hiding this comment

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

Agreed---just thinking out loud!

Copy link
Member Author

Choose a reason for hiding this comment

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

We do this for a bunch of places so would probably be a reasonable change to make!


def __len__(self) -> int:
return len(self.value)
Expand Down