Skip to content

Commit 41f8fb0

Browse files
authored
Use default= kwarg for models (PEP 681 compliance) (#93)
* Use `default=` kwarg for models * Update fragments * Move inline AST change to fragment * Add note regarding Field AST node
1 parent d6c62b2 commit 41f8fb0

File tree

10 files changed

+865
-676
lines changed

10 files changed

+865
-676
lines changed

codegen/ast/fragments/main/generalinfo.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@
77

88
class GeneralInfo(BaseModel):
99
with_chartmuseum: Optional[bool] = Field(
10-
None,
10+
default=None,
1111
description="DEPRECATED: Harbor instance is deployed with nested chartmuseum.",
1212
)

codegen/ast/fragments/main/projectmetadata.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
class ProjectMetadata(BaseModel):
1414
retention_id: Optional[Union[str, int]] = Field(
15-
None, description="The ID of the tag retention policy for the project"
15+
default=None, description="The ID of the tag retention policy for the project"
1616
)
1717

1818
@field_validator("*", mode="before")

codegen/ast/fragments/main/registryproviders.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@
1010

1111
class RegistryProviders(RootModel):
1212
root: Dict[str, RegistryProviderInfo] = Field(
13-
{},
13+
default={},
1414
description="The registry providers. Each key is the name of the registry provider.",
1515
)

codegen/ast/fragments/main/replicationfilter.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@
99

1010
class ReplicationFilter(BaseModel):
1111
value: Union[str, Dict[str, Any], None] = Field(
12-
None, description="The value of replication policy filter."
12+
default=None, description="The value of replication policy filter."
1313
)

codegen/ast/fragments/main/vulnerabilitysummary.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,27 @@
1010
class VulnerabilitySummary(BaseModel):
1111
# Summary dict keys added as fields
1212
critical: int = Field(
13-
0,
13+
default=0,
1414
alias="Critical",
1515
description="The number of critical vulnerabilities detected.",
1616
)
1717
high: int = Field(
18-
0, alias="High", description="The number of critical vulnerabilities detected."
18+
default=0,
19+
alias="High",
20+
description="The number of critical vulnerabilities detected.",
1921
)
2022
medium: int = Field(
21-
0,
23+
default=0,
2224
alias="Medium",
2325
description="The number of critical vulnerabilities detected.",
2426
)
2527
low: int = Field(
26-
0, alias="Low", description="The number of critical vulnerabilities detected."
28+
default=0,
29+
alias="Low",
30+
description="The number of critical vulnerabilities detected.",
2731
)
2832
unknown: int = Field(
29-
0,
33+
default=0,
3034
alias="Unknown",
3135
description="The number of critical vulnerabilities detected.",
3236
)

codegen/ast/fragments/scanner/vulnerabilityitem.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ class VulnerabilityItem(BaseModel):
1818
None,
1919
description="The list of links to the upstream databases with the full description of the vulnerability.\n",
2020
)
21+
severity: Severity = Field(
22+
Severity.unknown, description="The severity of the vulnerability."
23+
)
2124

2225
@field_validator("severity", mode="before")
2326
@classmethod

codegen/ast/parser.py

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ def modify(self, classdef: ast.ClassDef) -> ast.ClassDef:
118118
Unset = object() # Sentinel value so we can pass None as a default
119119

120120

121+
# NOTE: Unused right now. Field additions should be added using fragments.
121122
class Field(Modifier):
122123
def __init__(
123124
self,
@@ -184,20 +185,7 @@ def construct_field(self) -> ast.AnnAssign:
184185
# Changes to existing definitions (change annotation, etc.) or additions
185186
# that are too minor to create new fragments for.
186187
models: dict[str, dict[str, list[Modifier]]] = {
187-
FragmentDir.scanner: {
188-
"VulnerabilityItem": [
189-
Field(
190-
"severity",
191-
"Severity",
192-
default=ast.Attribute(
193-
value=ast.Name(id="Severity", ctx=ast.Load()),
194-
attr="unknown",
195-
ctx=ast.Load(),
196-
),
197-
description="The severity of the vulnerability.",
198-
),
199-
],
200-
},
188+
FragmentDir.scanner: {},
201189
FragmentDir.main: {
202190
"ExtraAttrs": [
203191
Annotation(

0 commit comments

Comments
 (0)