8000 Revert "Fix default port for mongosrv DSNs (#6827)" (#7116) · pydantic/pydantic@0e6cb3e · GitHub
[go: up one dir, main page]

Skip to content

Commit 0e6cb3e

Browse files
authored
Revert "Fix default port for mongosrv DSNs (#6827)" (#7116)
1 parent fb74946 commit 0e6cb3e

File tree

2 files changed

+22
-11
lines changed

2 files changed

+22
-11
lines changed

pydantic/networks.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import dataclasses as _dataclasses
55
import re
66
from ipaddress import IPv4Address, IPv4Interface, IPv4Network, IPv6Address, IPv6Interface, IPv6Network
7-
from typing import TYPE_CHECKING, Any, Union
7+
from typing import TYPE_CHECKING, Any
88

99
from pydantic_core import MultiHostUrl, PydanticCustomError, Url, core_schema
1010
from typing_extensions import Annotated, TypeAlias
@@ -124,10 +124,7 @@ def __hash__(self) -> int:
124124
UrlConstraints(allowed_schemes=['redis', 'rediss'], default_host='localhost', default_port=6379, default_path='/0'),
125125
]
126126
"""A type that will accept any Redis DSN."""
127-
MongoDsn = Union[
128-
Annotated[MultiHostUrl, UrlConstraints(allowed_schemes=['mongodb'], default_port=27017)],
129-
Annotated[MultiHostUrl, UrlConstraints(allowed_schemes=['mongodb+srv'])],
130-
]
127+
MongoDsn = Annotated[MultiHostUrl, UrlConstraints(allowed_schemes=['mongodb', 'mongodb+srv'], default_port=27017)]
131128
"""A type that will accept any MongoDB DSN."""
132129
KafkaDsn = Annotated[Url, UrlConstraints(allowed_schemes=['kafka'], default_host='localhost', default_port=9092)]
133130
"""A type that will accept any Kafka DSN."""

tests/test_networks.py

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -615,14 +615,28 @@ class Model(BaseModel):
615615
assert m.a.hosts() == [{'username': None, 'password': None, 'host': 'localhost', 'port': 27017}]
616616

617617

618-
def test_mongodsn_default_ports():
618+
@pytest.mark.parametrize(
619+
('dsn', 'expected'),
620+
[
621+
('mongodb://user:pass@localhost/app', 'mongodb://user:pass@localhost:27017/app'),
622+
pytest.param(
623+
'mongodb+srv://user:pass@localhost/app',
624+
'mongodb+srv://user:pass@localhost/app',
625+
marks=pytest.mark.xfail(
626+
reason=(
627+
'This case is not supported. '
628+
'Check https://github.com/pydantic/pydantic/pull/7116 for more details.'
629+
)
630+
),
631+
),
632+
],
633+
)
634+
def test_mongodsn_default_ports(dsn: str, expected: str):
619635
class Model(BaseModel):
620-
a: MongoDsn
636+
dsn: MongoDsn
621637

622-
m1 = Model(a='mongodb://user:pass@localhost/app')
623-
m2 = Model(a='mongodb+srv://user:pass@localhost/app')
624-
assert str(m1.a) == 'mongodb://user:pass@localhost:27017/app'
625-
assert str(m2.a) == 'mongodb+srv://user:pass@localhost/app'
638+
m = Model(dsn=dsn)
639+
assert str(m.dsn) == expected
626640

627641

628642
def test_kafka_dsns():

0 commit comments

Comments
 (0)
0