Skip to content

Commit

Permalink
Deprecate ServerState in the main module (#2581)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kludex authored Feb 21, 2025
1 parent 54d9575 commit aaf2016
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 3 deletions.
2 changes: 1 addition & 1 deletion tests/protocols/test_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
from uvicorn.config import WS_PROTOCOLS, Config
from uvicorn.lifespan.off import LifespanOff
from uvicorn.lifespan.on import LifespanOn
from uvicorn.main import ServerState
from uvicorn.protocols.http.h11_impl import H11Protocol
from uvicorn.server import ServerState

try:
from uvicorn.protocols.http.httptools_impl import HttpToolsProtocol
Expand Down
2 changes: 1 addition & 1 deletion tests/test_auto_detection.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@

from uvicorn.config import Config
from uvicorn.loops.auto import auto_loop_setup
from uvicorn.main import ServerState
from uvicorn.protocols.http.auto import AutoHTTPProtocol
from uvicorn.protocols.websockets.auto import AutoWebSocketsProtocol
from uvicorn.server import ServerState

try:
importlib.import_module("uvloop")
Expand Down
11 changes: 11 additions & 0 deletions tests/test_main.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import importlib
import inspect
import socket
from logging import WARNING

import httpx
import pytest

import uvicorn.server
from tests.utils import run_server
from uvicorn import Server
from uvicorn._types import ASGIReceiveCallable, ASGISendCallable, Scope
Expand Down Expand Up @@ -113,3 +115,12 @@ async def test_exit_on_create_server_with_invalid_host() -> None:
server = Server(config=config)
await server.serve()
assert exc_info.value.code == 1


def test_deprecated_server_state_from_main() -> None:
with pytest.deprecated_call(
match="uvicorn.main.ServerState is deprecated, use uvicorn.server.ServerState instead."
):
main = importlib.import_module("uvicorn.main")
server_state_cls = getattr(main, "ServerState")
assert server_state_cls is uvicorn.server.ServerState
15 changes: 14 additions & 1 deletion uvicorn/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import platform
import ssl
import sys
import warnings
from configparser import RawConfigParser
from typing import IO, Any, Callable

Expand All @@ -29,7 +30,7 @@
LoopSetupType,
WSProtocolType,
)
from uvicorn.server import Server, ServerState # noqa: F401 # Used to be defined here.
from uvicorn.server import Server
from uvicorn.supervisors import ChangeReload, Multiprocess

LEVEL_CHOICES = click.Choice(list(LOG_LEVELS.keys()))
Expand Down Expand Up @@ -587,5 +588,17 @@ def run(
sys.exit(STARTUP_FAILURE)


def __getattr__(name: str) -> Any:
if name == "ServerState":
warnings.warn(
"uvicorn.main.ServerState is deprecated, use uvicorn.server.ServerState instead.",
DeprecationWarning,
)
from uvicorn.server import ServerState

return ServerState
raise AttributeError(f"module {__name__} has no attribute {name}")


if __name__ == "__main__":
main() # pragma: no cover

0 comments on commit aaf2016

Please sign in to comment.