Skip to content

Commit eee7fba

Browse files
committed
Correct SSL type annotations
#992 https://docs.python.org/3/library/ssl.html#ssl.SSLContext.load_cert_chain - `certfile` is a required positional argument when running `SSLContext.load_cert_chain`, so annotating as `Optional` (which allows `None`) would not be ideal. Path-like objects are acceptable, so after `from pathlib import Path`, the annotation is `certfile: Union[Path, str]`. - `if self.is_ssl and self.ssl_certfile` will help ensure that the `self.ssl_certfile` required positional argument is present.
1 parent 0db3bc2 commit eee7fba

File tree

1 file changed

+4
-5
lines changed

1 file changed

+4
-5
lines changed

uvicorn/config.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@
108108

109109

110110
def create_ssl_context(
111-
certfile: Optional[str],
111+
certfile: Union[Path, str],
112112
keyfile: Optional[str],
113113
password: Optional[str],
114114
ssl_version: int,
@@ -161,7 +161,7 @@ def __init__(
161161
timeout_notify: int = 30,
162162
callback_notify: Callable[..., None] = None,
163163
ssl_keyfile: Optional[str] = None,
164-
ssl_certfile: Optional[str] = None,
164+
ssl_certfile: Optional[Union[Path, str]] = None,
165165
ssl_keyfile_password: Optional[str] = None,
166166
ssl_version: int = SSL_PROTOCOL_VERSION,
167167
ssl_cert_reqs: int = ssl.CERT_NONE,
@@ -286,9 +286,8 @@ def configure_logging(self) -> None:
286286
def load(self) -> None:
287287
assert not self.loaded
288288

289-
self.ssl: Optional[ssl.SSLContext]
290-
if self.is_ssl:
291-
self.ssl = create_ssl_context(
289+
if self.is_ssl and self.ssl_certfile:
290+
self.ssl: Optional[ssl.SSLContext] = create_ssl_context(
292291
keyfile=self.ssl_keyfile,
293292
certfile=self.ssl_certfile,
294293
password=self.ssl_keyfile_password,

0 commit comments

Comments
 (0)