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

Commit

Permalink
No untyped defs in register_new_matrix_user
Browse files Browse the repository at this point in the history
This one might be contraversial. `request_registration` has three
dependency-injection arguments used for testing. I'm removing the
injection of the `requests` module and using `unitest.mock.patch` in the
test cases instead.

Doing `reveal_type(requests)` and `reveal_type(requests.get)` before the
change:

```
synapse/_scripts/register_new_matrix_user.py:45: note: Revealed type is "Any"
synapse/_scripts/register_new_matrix_user.py:46: note: Revealed type is "Any"
```

And after:

```
synapse/_scripts/register_new_matrix_user.py:44: note: Revealed type is "types.ModuleType"
synapse/_scripts/register_new_matrix_user.py:45: note: Revealed type is "def (url: Union[builtins.str, builtins.bytes], params: Union[Union[_typeshed.SupportsItems[Union[builtins.str, builtins.bytes, builtins.int, builtins.float], Union[builtins.str, builtins.bytes, builtins.int, builtins.float, typing.Iterable[Union[builtins.str, builtins.bytes, builtins.int, builtins.float]], None]], Tuple[Union[builtins.str, builtins.bytes, builtins.int, builtins.float], Union[builtins.str, builtins.bytes, builtins.int, builtins.float, typing.Iterable[Union[builtins.str, builtins.bytes, builtins.int, builtins.float]], None]], typing.Iterable[Tuple[Union[builtins.str, builtins.bytes, builtins.int, builtins.float], Union[builtins.str, builtins.bytes, builtins.int, builtins.float, typing.Iterable[Union[builtins.str, builtins.bytes, builtins.int, builtins.float]], None]]], builtins.str, builtins.bytes], None] =, data: Union[Any, None] =, headers: Union[Any, None] =, cookies: Union[Any, None] =, files: Union[Any, None] =, auth: Union[Any, None] =, timeout: Union[Any, None] =, allow_redirects: builtins.bool =, proxies: Union[Any, None] =, hooks: Union[Any, None] =, stream: Union[Any, None] =, verify: Union[Any, None] =, cert: Union[Any, None] =, json: Union[Any, None] =) -> requests.models.Response"
```
  • Loading branch information
David Robertson committed Apr 8, 2022
1 parent 8fb1187 commit d960589
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 33 deletions.
3 changes: 1 addition & 2 deletions synapse/_scripts/register_new_matrix_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import sys
from typing import Callable, Optional

import requests as _requests
import requests
import yaml


Expand All @@ -33,7 +33,6 @@ def request_registration(
shared_secret: str,
admin: bool = False,
user_type: Optional[str] = None,
requests=_requests,
_print: Callable[[str], None] = print,
exit: Callable[[int], None] = sys.exit,
) -> None:
Expand Down
62 changes: 31 additions & 31 deletions tests/scripts/test_new_matrix_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from unittest.mock import Mock
from unittest.mock import Mock, patch

from synapse._scripts.register_new_matrix_user import request_registration

Expand Down Expand Up @@ -52,16 +52,16 @@ def post(url, json=None, verify=None):
out = []
err_code = []

request_registration(
"user",
"pass",
"matrix.org",
"shared",
admin=False,
requests=requests,
_print=out.append,
exit=err_code.append,
)
with patch("synapse._scripts.register_new_matrix_user.requests", requests):
request_registration(
"user",
"pass",
"matrix.org",
"shared",
admin=False,
_print=out.append,
exit=err_code.append,
)

# We should get the success message making sure everything is OK.
self.assertIn("Success!", out)
Expand All @@ -88,16 +88,16 @@ def get(url, verify=None):
out = []
err_code = []

request_registration(
"user",
"pass",
"matrix.org",
"shared",
admin=False,
requests=requests,
_print=out.append,
exit=err_code.append,
)
with patch("synapse._scripts.register_new_matrix_user.requests", requests):
request_registration(
"user",
"pass",
"matrix.org",
"shared",
admin=False,
_print=out.append,
exit=err_code.append,
)

# Exit was called
self.assertEqual(err_code, [1])
Expand Down Expand Up @@ -140,16 +140,16 @@ def post(url, json=None, verify=None):
out = []
err_code = []

request_registration(
"user",
"pass",
"matrix.org",
"shared",
admin=False,
requests=requests,
_print=out.append,
exit=err_code.append,
)
with patch("synapse._scripts.register_new_matrix_user.requests", requests):
request_registration(
"user",
"pass",
"matrix.org",
"shared",
admin=False,
_print=out.append,
exit=err_code.append,
)

# Exit was called
self.assertEqual(err_code, [1])
Expand Down

0 comments on commit d960589

Please sign in to comment.