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

Commit 7adcb20

Browse files
authored
Add missing type hints to synapse.util (#9982)
1 parent 22a8838 commit 7adcb20

File tree

9 files changed

+39
-25
lines changed

9 files changed

+39
-25
lines changed

changelog.d/9982.misc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add missing type hints to `synapse.util` module.

mypy.ini

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,15 +71,21 @@ files =
7171
synapse/types.py,
7272
synapse/util/async_helpers.py,
7373
synapse/util/caches,
74+
synapse/util/daemonize.py,
75+
synapse/util/hash.py,
76+
synapse/util/iterutils.py,
7477
synapse/util/metrics.py,
7578
synapse/util/macaroons.py,
79+
synapse/util/module_loader.py,
80+
synapse/util/msisdn.py,
7681
synapse/util/stringutils.py,
7782
synapse/visibility.py,
7883
tests/replication,
7984
tests/test_utils,
8085
tests/handlers/test_password_providers.py,
8186
tests/rest/client/v1/test_login.py,
8287
tests/rest/client/v2_alpha/test_auth.py,
88+
tests/util/test_itertools.py,
8389
tests/util/test_stream_change_cache.py
8490

8591
[mypy-pymacaroons.*]
@@ -175,5 +181,8 @@ ignore_missing_imports = True
175181
[mypy-pympler.*]
176182
ignore_missing_imports = True
177183

184+
[mypy-phonenumbers.*]
185+
ignore_missing_imports = True
186+
178187
[mypy-ijson.*]
179188
ignore_missing_imports = True

synapse/config/saml2.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,13 @@ def read_config(self, config, **kwargs):
164164
config_path = saml2_config.get("config_path", None)
165165
if config_path is not None:
166166
mod = load_python_module(config_path)
167-
_dict_merge(merge_dict=mod.CONFIG, into_dict=saml2_config_dict)
167+
config = getattr(mod, "CONFIG", None)
168+
if config is None:
169+
raise ConfigError(
170+
"Config path specified by saml2_config.config_path does not "
171+
"have a CONFIG property."
172+
)
173+
_dict_merge(merge_dict=config, into_dict=saml2_config_dict)
168174

169175
import saml2.config
170176

synapse/storage/databases/main/keys.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ async def get_server_verify_keys(
5555
"""
5656
keys = {}
5757

58-
def _get_keys(txn: Cursor, batch: Tuple[Tuple[str, str]]) -> None:
58+
def _get_keys(txn: Cursor, batch: Tuple[Tuple[str, str], ...]) -> None:
5959
"""Processes a batch of keys to fetch, and adds the result to `keys`."""
6060

6161
# batch_iter always returns tuples so it's safe to do len(batch)

synapse/util/hash.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,15 @@
1717
import unpaddedbase64
1818

1919

20-
def sha256_and_url_safe_base64(input_text):
20+
def sha256_and_url_safe_base64(input_text: str) -> str:
2121
"""SHA256 hash an input string, encode the digest as url-safe base64, and
2222
return
2323
24-
:param input_text: string to hash
25-
:type input_text: str
24+
Args:
25+
input_text: string to hash
2626
27-
:returns a sha256 hashed and url-safe base64 encoded digest
28-
:rtype: str
27+
returns:
28+
A sha256 hashed and url-safe base64 encoded digest
2929
"""
3030
digest = hashlib.sha256(input_text.encode()).digest()
3131
return unpaddedbase64.encode_base64(digest, urlsafe=True)

synapse/util/iterutils.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,12 @@
3030
T = TypeVar("T")
3131

3232

33-
def batch_iter(iterable: Iterable[T], size: int) -> Iterator[Tuple[T]]:
33+
def batch_iter(iterable: Iterable[T], size: int) -> Iterator[Tuple[T, ...]]:
3434
"""batch an iterable up into tuples with a maximum size
3535
3636
Args:
37-
iterable (iterable): the iterable to slice
38-
size (int): the maximum batch size
37+
iterable: the iterable to slice
38+
size: the maximum batch size
3939
4040
Returns:
4141
an iterator over the chunks
@@ -46,10 +46,7 @@ def batch_iter(iterable: Iterable[T], size: int) -> Iterator[Tuple[T]]:
4646
return iter(lambda: tuple(islice(sourceiter, size)), ())
4747

4848

49-
ISeq = TypeVar("ISeq", bound=Sequence, covariant=True)
50-
51-
52-
def chunk_seq(iseq: ISeq, maxlen: int) -> Iterable[ISeq]:
49+
def chunk_seq(iseq: Sequence[T], maxlen: int) -> Iterable[Sequence[T]]:
5350
"""Split the given sequence into chunks of the given size
5451
5552
The last chunk may be shorter than the given size.

synapse/util/module_loader.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import importlib
1616
import importlib.util
1717
import itertools
18+
from types import ModuleType
1819
from typing import Any, Iterable, Tuple, Type
1920

2021
import jsonschema
@@ -44,8 +45,8 @@ def load_module(provider: dict, config_path: Iterable[str]) -> Tuple[Type, Any]:
4445

4546
# We need to import the module, and then pick the class out of
4647
# that, so we split based on the last dot.
47-
module, clz = modulename.rsplit(".", 1)
48-
module = importlib.import_module(module)
48+
module_name, clz = modulename.rsplit(".", 1)
49+
module = importlib.import_module(module_name)
4950
provider_class = getattr(module, clz)
5051

5152
# Load the module config. If None, pass an empty dictionary instead
@@ -69,11 +70,11 @@ def load_module(provider: dict, config_path: Iterable[str]) -> Tuple[Type, Any]:
6970
return provider_class, provider_config
7071

7172

72-
def load_python_module(location: str):
73+
def load_python_module(location: str) -> ModuleType:
7374
"""Load a python module, and return a reference to its global namespace
7475
7576
Args:
76-
location (str): path to the module
77+
location: path to the module
7778
7879
Returns:
7980
python module object

synapse/util/msisdn.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,19 @@
1717
from synapse.api.errors import SynapseError
1818

1919

20-
def phone_number_to_msisdn(country, number):
20+
def phone_number_to_msisdn(country: str, number: str) -> str:
2121
"""
2222
Takes an ISO-3166-1 2 letter country code and phone number and
2323
returns an msisdn representing the canonical version of that
2424
phone number.
2525
Args:
26-
country (str): ISO-3166-1 2 letter country code
27-
number (str): Phone number in a national or international format
26+
country: ISO-3166-1 2 letter country code
27+
number: Phone number in a national or international format
2828
2929
Returns:
30-
(str) The canonical form of the phone number, as an msisdn
30+
The canonical form of the phone number, as an msisdn
3131
Raises:
32-
SynapseError if the number could not be parsed.
32+
SynapseError if the number could not be parsed.
3333
"""
3434
try:
3535
phoneNumber = phonenumbers.parse(number, country)

tests/util/test_itertools.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
14-
from typing import Dict, List
14+
from typing import Dict, Iterable, List, Sequence
1515

1616
from synapse.util.iterutils import chunk_seq, sorted_topologically
1717

@@ -44,7 +44,7 @@ def test_uneven_parts(self):
4444
)
4545

4646
def test_empty_input(self):
47-
parts = chunk_seq([], 5)
47+
parts = chunk_seq([], 5) # type: Iterable[Sequence]
4848

4949
self.assertEqual(
5050
list(parts),

0 commit comments

Comments
 (0)