Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: replace frequenty used dict literals with data classes #644

Open
willmurphyscode opened this issue Jul 30, 2024 · 1 comment
Open

Comments

@willmurphyscode
Copy link
Contributor

willmurphyscode commented Jul 30, 2024

For example:

vulnerability_element = {
"Vulnerability": {
"Severity": None,
"NamespaceName": None,
"FixedIn": [],
"Link": None,
"Description": "",
"Metadata": {},
"Name": None,
"CVSS": [],
},
}
gets copy.deepcopy-ed around a lot, as a template for a dict literal. The code would be cleaner and have more useful type hinting if this were replaced with a data class.

Examples of copying

v = copy.deepcopy(vulnerability_element)
ns_name = config.ns_format.format(re.search(config.platform_version_pattern, platform_element.text).group(1))
v["Vulnerability"]["NamespaceName"] = ns_name
v["Vulnerability"]["Severity"] = severity or ""
v["Vulnerability"]["Metadata"] = (
{"Issued": issued, "Updated": updated, "RefId": ref_id} if updated else {"Issued": issued, "RefId": ref_id}
)
v["Vulnerability"]["Name"] = name
v["Vulnerability"]["Link"] = link
v["Vulnerability"]["Description"] = description

and

vuln_dict[vid] = copy.deepcopy(vulnerability.vulnerability_element)
vuln_record = vuln_dict[vid]
reference_links = vulnerability.build_reference_links(vid)
# populate the static information about the new vuln record
vuln_record["Vulnerability"]["Name"] = str(vid)
vuln_record["Vulnerability"]["NamespaceName"] = self.namespace + ":" + str(release)

@willmurphyscode
Copy link
Contributor Author

We want to make sure that whatever solution we use is not vulnerable to crashing because a field is added, so maybe skipping straight to data classes is the wrong approach. I'll research some additional approaches.

@willmurphyscode willmurphyscode removed their assignment Sep 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Stalled
Development

No branches or pull requests

1 participant