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

Fix subscriptable type usage in Python <3.9 #15604

Merged
merged 3 commits into from
May 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions changelog.d/15604.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix subscriptable type usage in Python <3.9.
3 changes: 1 addition & 2 deletions synapse/storage/controllers/stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@
# limitations under the License.

import logging
from collections import Counter
from typing import TYPE_CHECKING, Collection, List, Tuple
from typing import TYPE_CHECKING, Collection, Counter, List, Tuple
Copy link
Member

Choose a reason for hiding this comment

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

I'm somewhat surprised this works and that you don't need to separately import Counter from collections and typing!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Should we worry at all? Maybe this doesn't have any tests 🤔

We do this elsewhere which I can align with:

from collections import Counter
from typing import TYPE_CHECKING, Any, Dict, Iterable, Optional, Tuple
from typing_extensions import Counter as CounterType

from collections import Counter
from typing import Collection, Generator, Iterable, List, Optional, TextIO, Tuple
import attr
from typing_extensions import Counter as CounterType

Copy link
Member

Choose a reason for hiding this comment

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

I don't think we need to worry about it:

>>> from typing import Counter as CounterType
>>> from collections import Counter
>>> x = CounterType()
>>> isinstance(x, Counter)
True

Just a weird way to do it, I guess this is why they're making them scriptable.


from synapse.api.errors import SynapseError
from synapse.storage.database import LoggingTransaction
Expand Down
2 changes: 1 addition & 1 deletion tests/test_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ def test_branch_no_conflict(self) -> Generator[defer.Deferred, Any, None]:

self.dummy_store.register_events(graph.walk())

context_store: dict[str, EventContext] = {}
context_store: Dict[str, EventContext] = {}

for event in graph.walk():
context = yield defer.ensureDeferred(
Expand Down