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

Commit 38ae8c7

Browse files
author
David Robertson
committed
Opt for simpler Literal[...] type
I would like to see a StrEnum but I am somewhat anxious of making that change right now. I think we should get validation in place first and then look at propagating better types into the application.
1 parent 8143310 commit 38ae8c7

File tree

3 files changed

+6
-23
lines changed

3 files changed

+6
-23
lines changed

synapse/rest/client/account.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
from urllib.parse import urlparse
2020

2121
from pydantic import StrictBool, StrictStr, constr
22+
from typing_extensions import Literal
2223

2324
from twisted.web.server import Request
2425

@@ -46,7 +47,6 @@
4647
ClientSecretType,
4748
EmailRequestTokenBody,
4849
MsisdnRequestTokenBody,
49-
ThreepidMedium,
5050
)
5151
from synapse.rest.models import RequestBodyModel
5252
from synapse.types import JsonDict
@@ -710,7 +710,7 @@ def __init__(self, hs: "HomeServer"):
710710
class PostBody(RequestBodyModel):
711711
address: StrictStr
712712
id_server: Optional[StrictStr] = None
713-
medium: ThreepidMedium
713+
medium: Literal["email", "msisdn"]
714714

715715
async def on_POST(self, request: SynapseRequest) -> Tuple[int, JsonDict]:
716716
"""Unbind the given 3pid from a specific identity server, or identity servers that are
@@ -744,7 +744,7 @@ def __init__(self, hs: "HomeServer"):
744744
class PostBody(RequestBodyModel):
745745
address: StrictStr
746746
id_server: Optional[StrictStr] = None
747-
medium: ThreepidMedium
747+
medium: Literal["email", "msisdn"]
748748

749749
async def on_POST(self, request: SynapseRequest) -> Tuple[int, JsonDict]:
750750
if not self.hs.config.registration.enable_3pid_changes:

synapse/rest/client/models.py

-7
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
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 enum import Enum
1514
from typing import TYPE_CHECKING, Dict, Optional
1615

1716
from pydantic import Extra, StrictInt, StrictStr, constr, validator
@@ -20,12 +19,6 @@
2019
from synapse.util.threepids import validate_email
2120

2221

23-
class ThreepidMedium(str, Enum):
24-
# Per advice at https://pydantic-docs.helpmanual.io/usage/types/#enums-and-choices
25-
email = "email"
26-
msisdn = "msisdn"
27-
28-
2922
class AuthenticationData(RequestBodyModel):
3023
"""
3124
Data used during user-interactive authentication.

tests/rest/client/test_models.py

+3-13
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,14 @@
1414
import unittest as stdlib_unittest
1515

1616
from pydantic import BaseModel, ValidationError
17+
from typing_extensions import Literal
1718

18-
from synapse.rest.client.models import EmailRequestTokenBody, ThreepidMedium
19+
from synapse.rest.client.models import EmailRequestTokenBody
1920

2021

2122
class ThreepidMediumEnumTestCase(stdlib_unittest.TestCase):
2223
class Model(BaseModel):
23-
medium: ThreepidMedium
24+
medium: Literal["email", "msisdn"]
2425

2526
def test_accepts_valid_medium_string(self) -> None:
2627
"""Sanity check that Pydantic behaves sensibly with an enum-of-str
@@ -29,18 +30,7 @@ def test_accepts_valid_medium_string(self) -> None:
2930
simultaneously.
3031
"""
3132
model = self.Model.parse_obj({"medium": "email"})
32-
self.assertIsInstance(model.medium, str)
3333
self.assertEqual(model.medium, "email")
34-
self.assertEqual(model.medium, ThreepidMedium.email)
35-
self.assertIs(model.medium, ThreepidMedium.email)
36-
37-
self.assertNotEqual(model.medium, "msisdn")
38-
self.assertNotEqual(model.medium, ThreepidMedium.msisdn)
39-
self.assertIsNot(model.medium, ThreepidMedium.msisdn)
40-
41-
def test_accepts_valid_medium_enum(self) -> None:
42-
model = self.Model.parse_obj({"medium": ThreepidMedium.email})
43-
self.assertIs(model.medium, ThreepidMedium.email)
4434

4535
def test_rejects_invalid_medium_value(self) -> None:
4636
with self.assertRaises(ValidationError):

0 commit comments

Comments
 (0)