Skip to content

Commit 43874b1

Browse files
authored
Check core tests with pyright (#4418)
* Type mocked API clients in backend tests * Use model_validate for shorthand in core tests * Narrow configuration types and drop fakes in core tests * Type compatibility excludes as keyed by field names * Fix remaining pyright errors in core tests * Check core tests with pyright * Address review comments on core test typing * Ignore POSIX-only SIGKILL in tunnel tests on Windows
1 parent e103c83 commit 43874b1

33 files changed

Lines changed: 544 additions & 461 deletions

‎pyproject.toml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ include = [
112112
"src/dstack/_internal/core/backends",
113113
"src/dstack/_internal/cli/services/configurators",
114114
"src/dstack/_internal/cli/commands",
115+
"src/tests/_internal/core",
115116
"src/tests/_internal/server/background/pipeline_tasks",
116117
]
117118
exclude = [

‎src/dstack/_internal/core/compatibility/backends.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
from dstack._internal.core.backends.gcp.models import GCPBackendConfigWithCreds
22
from dstack._internal.core.backends.hotaisle.models import HotAisleBackendConfigWithCreds
33
from dstack._internal.core.backends.models import AnyBackendConfigWithCreds
4-
from dstack._internal.core.models.common import IncludeExcludeDictType
4+
from dstack._internal.core.models.common import IncludeExcludeStrDictType
55

66

7-
def get_backend_config_excludes(config: AnyBackendConfigWithCreds) -> IncludeExcludeDictType:
7+
def get_backend_config_excludes(config: AnyBackendConfigWithCreds) -> IncludeExcludeStrDictType:
88
"""
99
Returns `config` exclude mapping to exclude certain fields from the create/update backend
1010
request. Use this method to exclude new fields when they are not set to keep
1111
clients backward-compatibility with older servers.
1212
"""
13-
excludes: IncludeExcludeDictType = {}
13+
excludes: IncludeExcludeStrDictType = {}
1414
if isinstance(config, GCPBackendConfigWithCreds) and config.tpu is None:
1515
excludes["tpu"] = True
1616
if isinstance(config, HotAisleBackendConfigWithCreds) and config.bare_metal is None:

‎src/dstack/_internal/core/compatibility/events.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
from dstack._internal.core.models.common import IncludeExcludeDictType
1+
from dstack._internal.core.models.common import IncludeExcludeStrDictType
22
from dstack._internal.server.schemas.events import ListEventsRequest
33

44

5-
def get_list_events_excludes(request: ListEventsRequest) -> IncludeExcludeDictType:
6-
list_events_excludes: IncludeExcludeDictType = {}
5+
def get_list_events_excludes(request: ListEventsRequest) -> IncludeExcludeStrDictType:
6+
list_events_excludes: IncludeExcludeStrDictType = {}
77
if request.target_presets is None:
88
list_events_excludes["target_presets"] = True
99
if request.target_gateway_replicas is None:
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
from dstack._internal.core.models.common import IncludeExcludeDictType
1+
from dstack._internal.core.models.common import IncludeExcludeStrDictType
22
from dstack._internal.server.schemas.exports import CreateExportRequest, UpdateExportRequest
33

44

5-
def get_create_export_excludes(request: CreateExportRequest) -> IncludeExcludeDictType:
6-
excludes: IncludeExcludeDictType = {}
5+
def get_create_export_excludes(request: CreateExportRequest) -> IncludeExcludeStrDictType:
6+
excludes: IncludeExcludeStrDictType = {}
77
return excludes
88

99

10-
def get_update_export_excludes(request: UpdateExportRequest) -> IncludeExcludeDictType:
11-
excludes: IncludeExcludeDictType = {}
10+
def get_update_export_excludes(request: UpdateExportRequest) -> IncludeExcludeStrDictType:
11+
excludes: IncludeExcludeStrDictType = {}
1212
return excludes

‎src/dstack/_internal/core/compatibility/fleets.py‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
11
from typing import Optional
22

33
from dstack._internal.core.compatibility.common import get_profile_excludes
4-
from dstack._internal.core.models.common import IncludeExcludeDictType
4+
from dstack._internal.core.models.common import IncludeExcludeStrDictType
55
from dstack._internal.core.models.fleets import ApplyFleetPlanInput, FleetSpec
66

77

8-
def get_get_plan_excludes(fleet_spec: FleetSpec) -> IncludeExcludeDictType:
9-
get_plan_excludes: IncludeExcludeDictType = {}
8+
def get_get_plan_excludes(fleet_spec: FleetSpec) -> IncludeExcludeStrDictType:
9+
get_plan_excludes: IncludeExcludeStrDictType = {}
1010
spec_excludes = get_fleet_spec_excludes(fleet_spec)
1111
if spec_excludes:
1212
get_plan_excludes["spec"] = spec_excludes
1313
return get_plan_excludes
1414

1515

16-
def get_apply_plan_excludes(plan_input: ApplyFleetPlanInput) -> IncludeExcludeDictType:
17-
apply_plan_excludes: IncludeExcludeDictType = {}
16+
def get_apply_plan_excludes(plan_input: ApplyFleetPlanInput) -> IncludeExcludeStrDictType:
17+
apply_plan_excludes: IncludeExcludeStrDictType = {}
1818
spec_excludes = get_fleet_spec_excludes(plan_input.spec)
1919
if spec_excludes:
2020
apply_plan_excludes["spec"] = spec_excludes
2121
current_resource = plan_input.current_resource
2222
if current_resource is not None:
23-
current_resource_excludes: IncludeExcludeDictType = {}
23+
current_resource_excludes: IncludeExcludeStrDictType = {}
2424
current_resource_spec_excludes = get_fleet_spec_excludes(current_resource.spec)
2525
if current_resource_spec_excludes:
2626
current_resource_excludes["spec"] = current_resource_spec_excludes
@@ -33,13 +33,13 @@ def get_apply_plan_excludes(plan_input: ApplyFleetPlanInput) -> IncludeExcludeDi
3333
return {"plan": apply_plan_excludes}
3434

3535

36-
def get_fleet_spec_excludes(fleet_spec: FleetSpec) -> Optional[IncludeExcludeDictType]:
36+
def get_fleet_spec_excludes(fleet_spec: FleetSpec) -> Optional[IncludeExcludeStrDictType]:
3737
"""
3838
Returns `fleet_spec` exclude mapping to exclude certain fields from the request.
3939
Use this method to exclude new fields when they are not set to keep
4040
clients backward-compatibility with older servers.
4141
"""
42-
spec_excludes: IncludeExcludeDictType = {}
42+
spec_excludes: IncludeExcludeStrDictType = {}
4343
profile_excludes = get_profile_excludes(fleet_spec.profile)
4444

4545
spec_excludes["autocreated"] = True

‎src/dstack/_internal/core/compatibility/gateways.py‎

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from dstack._internal.core.models.common import IncludeExcludeDictType
1+
from dstack._internal.core.models.common import IncludeExcludeStrDictType
22
from dstack._internal.core.models.gateways import (
33
ApplyGatewayPlanInput,
44
GatewayConfiguration,
@@ -10,12 +10,12 @@
1010
)
1111

1212

13-
def get_get_plan_excludes(body: GetGatewayPlanRequest) -> IncludeExcludeDictType:
13+
def get_get_plan_excludes(body: GetGatewayPlanRequest) -> IncludeExcludeStrDictType:
1414
return {"spec": get_gateway_spec_excludes(body.spec)}
1515

1616

17-
def get_apply_plan_excludes(plan_input: ApplyGatewayPlanInput) -> IncludeExcludeDictType:
18-
apply_plan_excludes: IncludeExcludeDictType = {
17+
def get_apply_plan_excludes(plan_input: ApplyGatewayPlanInput) -> IncludeExcludeStrDictType:
18+
apply_plan_excludes: IncludeExcludeStrDictType = {
1919
"spec": get_gateway_spec_excludes(plan_input.spec)
2020
}
2121
if plan_input.current_resource is not None:
@@ -31,39 +31,41 @@ def get_apply_plan_excludes(plan_input: ApplyGatewayPlanInput) -> IncludeExclude
3131
return {"plan": apply_plan_excludes}
3232

3333

34-
def get_gateway_spec_excludes(gateway_spec: GatewaySpec) -> IncludeExcludeDictType:
34+
def get_gateway_spec_excludes(gateway_spec: GatewaySpec) -> IncludeExcludeStrDictType:
3535
"""
3636
Returns `gateway_spec` exclude mapping to exclude certain fields from the request.
3737
Use this method to exclude new fields when they are not set to keep
3838
clients backward-compatibility with older servers.
3939
"""
40-
spec_excludes: IncludeExcludeDictType = {}
40+
spec_excludes: IncludeExcludeStrDictType = {}
4141
spec_excludes["configuration"] = _get_gateway_configuration_excludes(
4242
gateway_spec.configuration
4343
)
4444
return spec_excludes
4545

4646

47-
def get_create_gateway_excludes(configuration: GatewayConfiguration) -> IncludeExcludeDictType:
47+
def get_create_gateway_excludes(configuration: GatewayConfiguration) -> IncludeExcludeStrDictType:
4848
"""
4949
Returns an exclude mapping to exclude certain fields from the create gateway request.
5050
Use this method to exclude new fields when they are not set to keep
5151
clients backward-compatibility with older servers.
5252
"""
53-
create_gateway_excludes: IncludeExcludeDictType = {}
53+
create_gateway_excludes: IncludeExcludeStrDictType = {}
5454
create_gateway_excludes["configuration"] = _get_gateway_configuration_excludes(configuration)
5555
return create_gateway_excludes
5656

5757

58-
def get_set_default_gateway_excludes(request: SetDefaultGatewayRequest) -> IncludeExcludeDictType:
59-
excludes: IncludeExcludeDictType = {}
58+
def get_set_default_gateway_excludes(
59+
request: SetDefaultGatewayRequest,
60+
) -> IncludeExcludeStrDictType:
61+
excludes: IncludeExcludeStrDictType = {}
6062
return excludes
6163

6264

6365
def _get_gateway_configuration_excludes(
6466
configuration: GatewayConfiguration,
65-
) -> IncludeExcludeDictType:
66-
configuration_excludes: IncludeExcludeDictType = {}
67+
) -> IncludeExcludeStrDictType:
68+
configuration_excludes: IncludeExcludeStrDictType = {}
6769

6870
if configuration.default is None:
6971
configuration_excludes["default"] = True

‎src/dstack/_internal/core/compatibility/gpus.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
from typing import Optional
22

33
from dstack._internal.core.compatibility.runs import get_run_spec_excludes
4-
from dstack._internal.core.models.common import IncludeExcludeDictType
4+
from dstack._internal.core.models.common import IncludeExcludeStrDictType
55
from dstack._internal.server.schemas.gpus import ListGpusRequest
66

77

8-
def get_list_gpus_excludes(request: ListGpusRequest) -> Optional[IncludeExcludeDictType]:
9-
list_gpus_excludes: IncludeExcludeDictType = {} if request.reverse else {"reverse": True}
8+
def get_list_gpus_excludes(request: ListGpusRequest) -> Optional[IncludeExcludeStrDictType]:
9+
list_gpus_excludes: IncludeExcludeStrDictType = {} if request.reverse else {"reverse": True}
1010
run_spec_excludes = get_run_spec_excludes(request.run_spec)
1111
if run_spec_excludes is not None:
1212
list_gpus_excludes["run_spec"] = run_spec_excludes
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
from typing import Optional
22

3-
from dstack._internal.core.models.common import IncludeExcludeDictType
3+
from dstack._internal.core.models.common import IncludeExcludeStrDictType
44
from dstack._internal.server.schemas.logs import PollLogsRequest
55

66

7-
def get_poll_logs_excludes(request: PollLogsRequest) -> Optional[IncludeExcludeDictType]:
7+
def get_poll_logs_excludes(request: PollLogsRequest) -> Optional[IncludeExcludeStrDictType]:
88
"""
99
Returns exclude mapping to exclude certain fields from the request.
1010
Use this method to exclude new fields when they are not set to keep
1111
clients backward-compatibility with older servers.
1212
"""
13-
excludes: IncludeExcludeDictType = {}
13+
excludes: IncludeExcludeStrDictType = {}
1414
return excludes if excludes else None

‎src/dstack/_internal/core/compatibility/runs.py‎

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
from dstack._internal.core.compatibility.common import get_profile_excludes
44
from dstack._internal.core.models.common import (
5-
IncludeExcludeDictType,
65
IncludeExcludeSetType,
6+
IncludeExcludeStrDictType,
77
)
88
from dstack._internal.core.models.configurations import (
99
ServiceConfiguration,
@@ -24,19 +24,19 @@ def get_list_runs_excludes(list_runs_request: ListRunsRequest) -> IncludeExclude
2424
return excludes
2525

2626

27-
def get_apply_plan_excludes(plan: ApplyRunPlanInput) -> Optional[IncludeExcludeDictType]:
27+
def get_apply_plan_excludes(plan: ApplyRunPlanInput) -> Optional[IncludeExcludeStrDictType]:
2828
"""
2929
Returns `plan` exclude mapping to exclude certain fields from the request.
3030
Use this method to exclude new fields when they are not set to keep
3131
clients backward-compatibility with older servers.
3232
"""
33-
apply_plan_excludes: IncludeExcludeDictType = {}
33+
apply_plan_excludes: IncludeExcludeStrDictType = {}
3434
run_spec_excludes = get_run_spec_excludes(plan.run_spec)
3535
if run_spec_excludes is not None:
3636
apply_plan_excludes["run_spec"] = run_spec_excludes
3737
current_resource = plan.current_resource
3838
if current_resource is not None:
39-
current_resource_excludes: IncludeExcludeDictType = {}
39+
current_resource_excludes: IncludeExcludeStrDictType = {}
4040
apply_plan_excludes["current_resource"] = current_resource_excludes
4141
current_resource_excludes["run_spec"] = get_run_spec_excludes(current_resource.run_spec)
4242
current_resource_excludes["jobs"] = {
@@ -62,12 +62,12 @@ def get_apply_plan_excludes(plan: ApplyRunPlanInput) -> Optional[IncludeExcludeD
6262
return {"plan": apply_plan_excludes}
6363

6464

65-
def get_get_plan_excludes(request: GetRunPlanRequest) -> Optional[IncludeExcludeDictType]:
65+
def get_get_plan_excludes(request: GetRunPlanRequest) -> Optional[IncludeExcludeStrDictType]:
6666
"""
6767
Excludes new fields when they are not set to keep
6868
clients backward-compatibility with older servers.
6969
"""
70-
get_plan_excludes: IncludeExcludeDictType = (
70+
get_plan_excludes: IncludeExcludeStrDictType = (
7171
{} if request.reverse_offers else {"reverse_offers": True}
7272
)
7373
run_spec_excludes = get_run_spec_excludes(request.run_spec)
@@ -76,14 +76,14 @@ def get_get_plan_excludes(request: GetRunPlanRequest) -> Optional[IncludeExclude
7676
return get_plan_excludes
7777

7878

79-
def get_run_spec_excludes(run_spec: RunSpec) -> IncludeExcludeDictType:
79+
def get_run_spec_excludes(run_spec: RunSpec) -> IncludeExcludeStrDictType:
8080
"""
8181
Returns `run_spec` exclude mapping to exclude certain fields from the request.
8282
Use this method to exclude new fields when they are not set to keep
8383
clients backward-compatibility with older servers.
8484
"""
85-
spec_excludes: IncludeExcludeDictType = {}
86-
configuration_excludes: IncludeExcludeDictType = {}
85+
spec_excludes: IncludeExcludeStrDictType = {}
86+
configuration_excludes: IncludeExcludeStrDictType = {}
8787
profile_excludes = get_profile_excludes(run_spec.profile)
8888
for field in get_profile_excludes(run_spec.configuration):
8989
configuration_excludes[field] = True
@@ -106,13 +106,13 @@ def get_run_spec_excludes(run_spec: RunSpec) -> IncludeExcludeDictType:
106106
return spec_excludes
107107

108108

109-
def get_job_spec_excludes(job_specs: list[JobSpec]) -> IncludeExcludeDictType:
109+
def get_job_spec_excludes(job_specs: list[JobSpec]) -> IncludeExcludeStrDictType:
110110
"""
111111
Returns `job_spec` exclude mapping to exclude certain fields from the request.
112112
Use this method to exclude new fields when they are not set to keep
113113
clients backward-compatibility with older servers.
114114
"""
115-
spec_excludes: IncludeExcludeDictType = {}
115+
spec_excludes: IncludeExcludeStrDictType = {}
116116
if all(s.node_group_index == 0 for s in job_specs):
117117
spec_excludes["node_group_index"] = True
118118
if all(s.node_group_name == DEFAULT_REPLICA_GROUP_NAME for s in job_specs):
@@ -122,8 +122,8 @@ def get_job_spec_excludes(job_specs: list[JobSpec]) -> IncludeExcludeDictType:
122122
return spec_excludes
123123

124124

125-
def get_job_submission_excludes(job_submissions: list[JobSubmission]) -> IncludeExcludeDictType:
126-
submission_excludes: IncludeExcludeDictType = {}
125+
def get_job_submission_excludes(job_submissions: list[JobSubmission]) -> IncludeExcludeStrDictType:
126+
submission_excludes: IncludeExcludeStrDictType = {}
127127
# `Resources.description` is deprecated and never set since 0.21. Not sending it lets 0.22
128128
# drop the field without breaking 0.21 clients.
129129
submission_excludes["job_provisioning_data"] = {
Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,34 @@
1-
from dstack._internal.core.models.common import IncludeExcludeDictType
1+
from dstack._internal.core.models.common import IncludeExcludeStrDictType
22
from dstack._internal.core.models.volumes import (
33
AnyVolumeConfiguration,
44
VolumeSpec,
55
)
66

77

8-
def get_volume_spec_excludes(volume_spec: VolumeSpec) -> IncludeExcludeDictType:
8+
def get_volume_spec_excludes(volume_spec: VolumeSpec) -> IncludeExcludeStrDictType:
99
"""
1010
Returns `volume_spec` exclude mapping to exclude certain fields from the request.
1111
Use this method to exclude new fields when they are not set to keep
1212
clients backward-compatibility with older servers.
1313
"""
14-
spec_excludes: IncludeExcludeDictType = {}
14+
spec_excludes: IncludeExcludeStrDictType = {}
1515
spec_excludes["configuration"] = _get_volume_configuration_excludes(volume_spec.configuration)
1616
return spec_excludes
1717

1818

19-
def get_create_volume_excludes(configuration: AnyVolumeConfiguration) -> IncludeExcludeDictType:
19+
def get_create_volume_excludes(configuration: AnyVolumeConfiguration) -> IncludeExcludeStrDictType:
2020
"""
2121
Returns an exclude mapping to exclude certain fields from the create volume request.
2222
Use this method to exclude new fields when they are not set to keep
2323
clients backward-compatibility with older servers.
2424
"""
25-
create_volume_excludes: IncludeExcludeDictType = {}
25+
create_volume_excludes: IncludeExcludeStrDictType = {}
2626
create_volume_excludes["configuration"] = _get_volume_configuration_excludes(configuration)
2727
return create_volume_excludes
2828

2929

3030
def _get_volume_configuration_excludes(
3131
configuration: AnyVolumeConfiguration,
32-
) -> IncludeExcludeDictType:
33-
configuration_excludes: IncludeExcludeDictType = {}
32+
) -> IncludeExcludeStrDictType:
33+
configuration_excludes: IncludeExcludeStrDictType = {}
3434
return configuration_excludes

0 commit comments

Comments
 (0)