This repository has been archived by the owner on Apr 26, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Clean-up type hints in server config #10915
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Clean-up configuration helper classes for the `ServerConfig` class. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,7 +19,7 @@ | |
import os.path | ||
import re | ||
from textwrap import indent | ||
from typing import Any, Dict, Iterable, List, Optional, Set, Tuple | ||
from typing import Any, Dict, Iterable, List, Optional, Set, Tuple, Union | ||
|
||
import attr | ||
import yaml | ||
|
@@ -184,49 +184,74 @@ def generate_ip_set( | |
|
||
@attr.s(frozen=True) | ||
class HttpResourceConfig: | ||
names = attr.ib( | ||
type=List[str], | ||
names: List[str] = attr.ib( | ||
factory=list, | ||
validator=attr.validators.deep_iterable(attr.validators.in_(KNOWN_RESOURCES)), # type: ignore | ||
) | ||
compress = attr.ib( | ||
type=bool, | ||
compress: bool = attr.ib( | ||
default=False, | ||
validator=attr.validators.optional(attr.validators.instance_of(bool)), # type: ignore[arg-type] | ||
Comment on lines
-187
to
193
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this a style preference or a functional change? (Perhaps mypy et al only recognise the annotation?) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a style preference, mypy pulls it out either way. If |
||
) | ||
|
||
|
||
@attr.s(frozen=True) | ||
@attr.s(slots=True, frozen=True, auto_attribs=True) | ||
class HttpListenerConfig: | ||
"""Object describing the http-specific parts of the config of a listener""" | ||
|
||
x_forwarded = attr.ib(type=bool, default=False) | ||
resources = attr.ib(type=List[HttpResourceConfig], factory=list) | ||
additional_resources = attr.ib(type=Dict[str, dict], factory=dict) | ||
tag = attr.ib(type=str, default=None) | ||
x_forwarded: bool = False | ||
resources: List[HttpResourceConfig] = attr.ib(factory=list) | ||
additional_resources: Dict[str, dict] = attr.ib(factory=dict) | ||
tag: Optional[str] = None | ||
|
||
|
||
@attr.s(frozen=True) | ||
@attr.s(slots=True, frozen=True, auto_attribs=True) | ||
class ListenerConfig: | ||
"""Object describing the configuration of a single listener.""" | ||
|
||
port = attr.ib(type=int, validator=attr.validators.instance_of(int)) | ||
bind_addresses = attr.ib(type=List[str]) | ||
type = attr.ib(type=str, validator=attr.validators.in_(KNOWN_LISTENER_TYPES)) | ||
tls = attr.ib(type=bool, default=False) | ||
port: int = attr.ib(validator=attr.validators.instance_of(int)) | ||
bind_addresses: List[str] | ||
type: str = attr.ib(validator=attr.validators.in_(KNOWN_LISTENER_TYPES)) | ||
tls: bool = False | ||
|
||
# http_options is only populated if type=http | ||
http_options = attr.ib(type=Optional[HttpListenerConfig], default=None) | ||
http_options: Optional[HttpListenerConfig] = None | ||
|
||
|
||
@attr.s(frozen=True) | ||
@attr.s(slots=True, frozen=True, auto_attribs=True) | ||
class ManholeConfig: | ||
"""Object describing the configuration of the manhole""" | ||
|
||
username = attr.ib(type=str, validator=attr.validators.instance_of(str)) | ||
password = attr.ib(type=str, validator=attr.validators.instance_of(str)) | ||
priv_key = attr.ib(type=Optional[Key]) | ||
pub_key = attr.ib(type=Optional[Key]) | ||
username: str = attr.ib(validator=attr.validators.instance_of(str)) | ||
password: str = attr.ib(validator=attr.validators.instance_of(str)) | ||
priv_key: Optional[Key] | ||
pub_key: Optional[Key] | ||
|
||
|
||
@attr.s(slots=True, frozen=True, auto_attribs=True) | ||
class RetentionConfig: | ||
"""Object describing the configuration of the manhole""" | ||
|
||
interval: int | ||
shortest_max_lifetime: Optional[int] | ||
longest_max_lifetime: Optional[int] | ||
|
||
|
||
@attr.s(frozen=True) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This doesn't use |
||
class LimitRemoteRoomsConfig: | ||
enabled: bool = attr.ib(validator=attr.validators.instance_of(bool), default=False) | ||
complexity: Union[float, int] = attr.ib( | ||
validator=attr.validators.instance_of( | ||
(float, int) # type: ignore[arg-type] # noqa | ||
), | ||
default=1.0, | ||
) | ||
complexity_error: str = attr.ib( | ||
validator=attr.validators.instance_of(str), | ||
default=ROOM_COMPLEXITY_TOO_GREAT, | ||
) | ||
admins_can_join: bool = attr.ib( | ||
validator=attr.validators.instance_of(bool), default=False | ||
) | ||
|
||
|
||
class ServerConfig(Config): | ||
|
@@ -519,7 +544,7 @@ def read_config(self, config, **kwargs): | |
" greater than 'allowed_lifetime_max'" | ||
) | ||
|
||
self.retention_purge_jobs: List[Dict[str, Optional[int]]] = [] | ||
self.retention_purge_jobs: List[RetentionConfig] = [] | ||
for purge_job_config in retention_config.get("purge_jobs", []): | ||
interval_config = purge_job_config.get("interval") | ||
|
||
|
@@ -553,20 +578,12 @@ def read_config(self, config, **kwargs): | |
) | ||
|
||
self.retention_purge_jobs.append( | ||
{ | ||
"interval": interval, | ||
"shortest_max_lifetime": shortest_max_lifetime, | ||
"longest_max_lifetime": longest_max_lifetime, | ||
} | ||
RetentionConfig(interval, shortest_max_lifetime, longest_max_lifetime) | ||
) | ||
|
||
if not self.retention_purge_jobs: | ||
self.retention_purge_jobs = [ | ||
{ | ||
"interval": self.parse_duration("1d"), | ||
"shortest_max_lifetime": None, | ||
"longest_max_lifetime": None, | ||
} | ||
RetentionConfig(self.parse_duration("1d"), None, None) | ||
] | ||
|
||
self.listeners = [parse_listener_def(x) for x in config.get("listeners", [])] | ||
|
@@ -591,25 +608,6 @@ def read_config(self, config, **kwargs): | |
self.gc_thresholds = read_gc_thresholds(config.get("gc_thresholds", None)) | ||
self.gc_seconds = self.read_gc_intervals(config.get("gc_min_interval", None)) | ||
|
||
@attr.s | ||
class LimitRemoteRoomsConfig: | ||
enabled = attr.ib( | ||
validator=attr.validators.instance_of(bool), default=False | ||
) | ||
complexity = attr.ib( | ||
validator=attr.validators.instance_of( | ||
(float, int) # type: ignore[arg-type] # noqa | ||
), | ||
default=1.0, | ||
) | ||
complexity_error = attr.ib( | ||
validator=attr.validators.instance_of(str), | ||
default=ROOM_COMPLEXITY_TOO_GREAT, | ||
) | ||
admins_can_join = attr.ib( | ||
validator=attr.validators.instance_of(bool), default=False | ||
) | ||
|
||
self.limit_remote_rooms = LimitRemoteRoomsConfig( | ||
**(config.get("limit_remote_rooms") or {}) | ||
) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This doesn't use
slots
since we splat a dictionary directly into it from the user (e.g.HttpResourceConfig(**config)
).