Skip to content

Commit aaf2016

Browse files
authored
Deprecate ServerState in the main module (#2581)
1 parent 54d9575 commit aaf2016

File tree

4 files changed

+27
-3
lines changed

4 files changed

+27
-3
lines changed

tests/protocols/test_http.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
from uvicorn.config import WS_PROTOCOLS, Config
1515
from uvicorn.lifespan.off import LifespanOff
1616
from uvicorn.lifespan.on import LifespanOn
17-
from uvicorn.main import ServerState
1817
from uvicorn.protocols.http.h11_impl import H11Protocol
18+
from uvicorn.server import ServerState
1919

2020
try:
2121
from uvicorn.protocols.http.httptools_impl import HttpToolsProtocol

tests/test_auto_detection.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55

66
from uvicorn.config import Config
77
from uvicorn.loops.auto import auto_loop_setup
8-
from uvicorn.main import ServerState
98
from uvicorn.protocols.http.auto import AutoHTTPProtocol
109
from uvicorn.protocols.websockets.auto import AutoWebSocketsProtocol
10+
from uvicorn.server import ServerState
1111

1212
try:
1313
importlib.import_module("uvloop")

tests/test_main.py

+11
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1+
import importlib
12
import inspect
23
import socket
34
from logging import WARNING
45

56
import httpx
67
import pytest
78

9+
import uvicorn.server
810
from tests.utils import run_server
911
from uvicorn import Server
1012
from uvicorn._types import ASGIReceiveCallable, ASGISendCallable, Scope
@@ -113,3 +115,12 @@ async def test_exit_on_create_server_with_invalid_host() -> None:
113115
server = Server(config=config)
114116
await server.serve()
115117
assert exc_info.value.code == 1
118+
119+
120+
def test_deprecated_server_state_from_main() -> None:
121+
with pytest.deprecated_call(
122+
match="uvicorn.main.ServerState is deprecated, use uvicorn.server.ServerState instead."
123+
):
124+
main = importlib.import_module("uvicorn.main")
125+
server_state_cls = getattr(main, "ServerState")
126+
assert server_state_cls is uvicorn.server.ServerState

uvicorn/main.py

+14-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import platform
77
import ssl
88
import sys
9+
import warnings
910
from configparser import RawConfigParser
1011
from typing import IO, Any, Callable
1112

@@ -29,7 +30,7 @@
2930
LoopSetupType,
3031
WSProtocolType,
3132
)
32-
from uvicorn.server import Server, ServerState # noqa: F401 # Used to be defined here.
33+
from uvicorn.server import Server
3334
from uvicorn.supervisors import ChangeReload, Multiprocess
3435

3536
LEVEL_CHOICES = click.Choice(list(LOG_LEVELS.keys()))
@@ -587,5 +588,17 @@ def run(
587588
sys.exit(STARTUP_FAILURE)
588589

589590

591+
def __getattr__(name: str) -> Any:
592+
if name == "ServerState":
593+
warnings.warn(
594+
"uvicorn.main.ServerState is deprecated, use uvicorn.server.ServerState instead.",
595+
DeprecationWarning,
596+
)
597+
from uvicorn.server import ServerState
598+
599+
return ServerState
600+
raise AttributeError(f"module {__name__} has no attribute {name}")
601+
602+
590603
if __name__ == "__main__":
591604
main() # pragma: no cover

0 commit comments

Comments
 (0)