Skip to content

Commit 8bcc64f

Browse files
committed
Modernize the code base
* Use attrs instead of attr * Fully type the code base: mypy and autocompletion tool should work correctly on all the objects and types. Also, deserializing a JSON SARIF file should populate the whole datastructure correctly using `cattrs`.
1 parent f99b8ed commit 8bcc64f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+886
-632
lines changed

README.rst

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,24 @@ To learn more about SARIF and find resources for working with it, you can visit
2222

2323
The source code is available at https://github.com/microsoft/sarif-python-om.
2424

25+
How to use
26+
==========
27+
28+
.. code-block:: python
29+
30+
import cattrs
31+
import json
32+
import sys
33+
34+
from sarif_om import SarifLog
35+
36+
37+
with open(sys.argv[1]) as fp:
38+
data = json.load(fp)
39+
40+
sarif_log = cattrs.structure(data, SarifLog)
41+
42+
2543
Generation
2644
==========
2745

sarif_om/__init__.py

Lines changed: 53 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,55 @@
11
# This file was generated by jschema_to_python version 1.2.3.
22

3-
from sarif_om._sarif_log import SarifLog
4-
from sarif_om._address import Address
5-
from sarif_om._artifact import Artifact
6-
from sarif_om._artifact_change import ArtifactChange
7-
from sarif_om._artifact_content import ArtifactContent
8-
from sarif_om._artifact_location import ArtifactLocation
9-
from sarif_om._attachment import Attachment
10-
from sarif_om._code_flow import CodeFlow
11-
from sarif_om._configuration_override import ConfigurationOverride
12-
from sarif_om._conversion import Conversion
13-
from sarif_om._edge import Edge
14-
from sarif_om._edge_traversal import EdgeTraversal
15-
from sarif_om._exception import Exception
16-
from sarif_om._external_properties import ExternalProperties
17-
from sarif_om._external_property_file_reference import ExternalPropertyFileReference
18-
from sarif_om._external_property_file_references import ExternalPropertyFileReferences
19-
from sarif_om._fix import Fix
20-
from sarif_om._graph import Graph
21-
from sarif_om._graph_traversal import GraphTraversal
22-
from sarif_om._invocation import Invocation
23-
from sarif_om._location import Location
24-
from sarif_om._location_relationship import LocationRelationship
25-
from sarif_om._logical_location import LogicalLocation
26-
from sarif_om._message import Message
27-
from sarif_om._multiformat_message_string import MultiformatMessageString
28-
from sarif_om._node import Node
29-
from sarif_om._notification import Notification
30-
from sarif_om._physical_location import PhysicalLocation
31-
from sarif_om._property_bag import PropertyBag
32-
from sarif_om._rectangle import Rectangle
33-
from sarif_om._region import Region
34-
from sarif_om._replacement import Replacement
35-
from sarif_om._reporting_configuration import ReportingConfiguration
36-
from sarif_om._reporting_descriptor import ReportingDescriptor
37-
from sarif_om._reporting_descriptor_reference import ReportingDescriptorReference
38-
from sarif_om._reporting_descriptor_relationship import ReportingDescriptorRelationship
39-
from sarif_om._result import Result
40-
from sarif_om._result_provenance import ResultProvenance
41-
from sarif_om._run import Run
42-
from sarif_om._run_automation_details import RunAutomationDetails
43-
from sarif_om._special_locations import SpecialLocations
44-
from sarif_om._stack import Stack
45-
from sarif_om._stack_frame import StackFrame
46-
from sarif_om._suppression import Suppression
47-
from sarif_om._thread_flow import ThreadFlow
48-
from sarif_om._thread_flow_location import ThreadFlowLocation
49-
from sarif_om._tool import Tool
50-
from sarif_om._tool_component import ToolComponent
51-
from sarif_om._tool_component_reference import ToolComponentReference
52-
from sarif_om._translation_metadata import TranslationMetadata
53-
from sarif_om._version_control_details import VersionControlDetails
54-
from sarif_om._web_request import WebRequest
55-
from sarif_om._web_response import WebResponse
3+
from ._sarif_log import SarifLog
4+
from ._address import Address
5+
from ._artifact import Artifact
6+
from ._artifact_change import ArtifactChange
7+
from ._artifact_content import ArtifactContent
8+
from ._artifact_location import ArtifactLocation
9+
from ._attachment import Attachment
10+
from ._code_flow import CodeFlow
11+
from ._configuration_override import ConfigurationOverride
12+
from ._conversion import Conversion
13+
from ._edge import Edge
14+
from ._edge_traversal import EdgeTraversal
15+
from ._exception import Exception
16+
from ._external_properties import ExternalProperties
17+
from ._external_property_file_reference import ExternalPropertyFileReference
18+
from ._external_property_file_references import ExternalPropertyFileReferences
19+
from ._fix import Fix
20+
from ._graph import Graph
21+
from ._graph_traversal import GraphTraversal
22+
from ._invocation import Invocation
23+
from ._location import Location
24+
from ._location_relationship import LocationRelationship
25+
from ._logical_location import LogicalLocation
26+
from ._message import Message
27+
from ._multiformat_message_string import MultiformatMessageString
28+
from ._node import Node
29+
from ._notification import Notification
30+
from ._physical_location import PhysicalLocation
31+
from ._property_bag import PropertyBag
32+
from ._rectangle import Rectangle
33+
from ._region import Region
34+
from ._replacement import Replacement
35+
from ._reporting_configuration import ReportingConfiguration
36+
from ._reporting_descriptor import ReportingDescriptor
37+
from ._reporting_descriptor_reference import ReportingDescriptorReference
38+
from ._reporting_descriptor_relationship import ReportingDescriptorRelationship
39+
from ._result import Result
40+
from ._result_provenance import ResultProvenance
41+
from ._run import Run
42+
from ._run_automation_details import RunAutomationDetails
43+
from ._special_locations import SpecialLocations
44+
from ._stack import Stack
45+
from ._stack_frame import StackFrame
46+
from ._suppression import Suppression
47+
from ._thread_flow import ThreadFlow
48+
from ._thread_flow_location import ThreadFlowLocation
49+
from ._tool import Tool
50+
from ._tool_component import ToolComponent
51+
from ._tool_component_reference import ToolComponentReference
52+
from ._translation_metadata import TranslationMetadata
53+
from ._version_control_details import VersionControlDetails
54+
from ._web_request import WebRequest
55+
from ._web_response import WebResponse

sarif_om/_address.py

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
11
# This file was generated by jschema_to_python version 1.2.3.
22

3-
import attr
3+
from ._property_bag import PropertyBag
4+
from attrs import define
5+
from attrs import field
46

57

6-
@attr.s
7-
class Address(object):
8+
@define()
9+
class Address:
810
"""A physical or virtual address, or a range of addresses, in an 'addressable region' (memory or a binary file)."""
911

10-
absolute_address = attr.ib(default=-1, metadata={"schema_property_name": "absoluteAddress"})
11-
fully_qualified_name = attr.ib(default=None, metadata={"schema_property_name": "fullyQualifiedName"})
12-
index = attr.ib(default=-1, metadata={"schema_property_name": "index"})
13-
kind = attr.ib(default=None, metadata={"schema_property_name": "kind"})
14-
length = attr.ib(default=None, metadata={"schema_property_name": "length"})
15-
name = attr.ib(default=None, metadata={"schema_property_name": "name"})
16-
offset_from_parent = attr.ib(default=None, metadata={"schema_property_name": "offsetFromParent"})
17-
parent_index = attr.ib(default=-1, metadata={"schema_property_name": "parentIndex"})
18-
properties = attr.ib(default=None, metadata={"schema_property_name": "properties"})
19-
relative_address = attr.ib(default=None, metadata={"schema_property_name": "relativeAddress"})
12+
absolute_address : int = field(default=-1, metadata={"schema_property_name": "absoluteAddress"})
13+
fully_qualified_name : str = field(default=None, metadata={"schema_property_name": "fullyQualifiedName"})
14+
index : int = field(default=-1, metadata={"schema_property_name": "index"})
15+
kind : str = field(default=None, metadata={"schema_property_name": "kind"})
16+
length : int = field(default=None, metadata={"schema_property_name": "length"})
17+
name : str = field(default=None, metadata={"schema_property_name": "name"})
18+
offset_from_parent : int = field(default=None, metadata={"schema_property_name": "offsetFromParent"})
19+
parent_index : int = field(default=-1, metadata={"schema_property_name": "parentIndex"})
20+
properties : PropertyBag = field(default=None, metadata={"schema_property_name": "properties"})
21+
relative_address : int = field(default=None, metadata={"schema_property_name": "relativeAddress"})

sarif_om/_artifact.py

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,27 @@
11
# This file was generated by jschema_to_python version 1.2.3.
22

3-
import attr
3+
from ._artifact_content import ArtifactContent
4+
from ._artifact_location import ArtifactLocation
5+
from ._message import Message
6+
from ._property_bag import PropertyBag
7+
from attrs import define
8+
from attrs import field
49

510

6-
@attr.s
7-
class Artifact(object):
11+
@define()
12+
class Artifact:
813
"""A single artifact. In some cases, this artifact might be nested within another artifact."""
914

10-
contents = attr.ib(default=None, metadata={"schema_property_name": "contents"})
11-
description = attr.ib(default=None, metadata={"schema_property_name": "description"})
12-
encoding = attr.ib(default=None, metadata={"schema_property_name": "encoding"})
13-
hashes = attr.ib(default=None, metadata={"schema_property_name": "hashes"})
14-
last_modified_time_utc = attr.ib(default=None, metadata={"schema_property_name": "lastModifiedTimeUtc"})
15-
length = attr.ib(default=-1, metadata={"schema_property_name": "length"})
16-
location = attr.ib(default=None, metadata={"schema_property_name": "location"})
17-
mime_type = attr.ib(default=None, metadata={"schema_property_name": "mimeType"})
18-
offset = attr.ib(default=None, metadata={"schema_property_name": "offset"})
19-
parent_index = attr.ib(default=-1, metadata={"schema_property_name": "parentIndex"})
20-
properties = attr.ib(default=None, metadata={"schema_property_name": "properties"})
21-
roles = attr.ib(default=None, metadata={"schema_property_name": "roles"})
22-
source_language = attr.ib(default=None, metadata={"schema_property_name": "sourceLanguage"})
15+
contents : ArtifactContent = field(default=None, metadata={"schema_property_name": "contents"})
16+
description : Message = field(default=None, metadata={"schema_property_name": "description"})
17+
encoding : str = field(default=None, metadata={"schema_property_name": "encoding"})
18+
hashes : dict[str, str] = field(default=None, metadata={"schema_property_name": "hashes"})
19+
last_modified_time_utc : str = field(default=None, metadata={"schema_property_name": "lastModifiedTimeUtc"})
20+
length : int = field(default=-1, metadata={"schema_property_name": "length"})
21+
location : ArtifactLocation = field(default=None, metadata={"schema_property_name": "location"})
22+
mime_type : str = field(default=None, metadata={"schema_property_name": "mimeType"})
23+
offset : int = field(default=None, metadata={"schema_property_name": "offset"})
24+
parent_index : int = field(default=-1, metadata={"schema_property_name": "parentIndex"})
25+
properties : PropertyBag = field(default=None, metadata={"schema_property_name": "properties"})
26+
roles : list[str] = field(factory=list, metadata={"schema_property_name": "roles"})
27+
source_language : str = field(default=None, metadata={"schema_property_name": "sourceLanguage"})

sarif_om/_artifact_change.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
# This file was generated by jschema_to_python version 1.2.3.
22

3-
import attr
3+
from ._artifact_location import ArtifactLocation
4+
from ._property_bag import PropertyBag
5+
from ._replacement import Replacement
6+
from attrs import define
7+
from attrs import field
48

59

6-
@attr.s
7-
class ArtifactChange(object):
10+
@define()
11+
class ArtifactChange:
812
"""A change to a single artifact."""
913

10-
artifact_location = attr.ib(metadata={"schema_property_name": "artifactLocation"})
11-
replacements = attr.ib(metadata={"schema_property_name": "replacements"})
12-
properties = attr.ib(default=None, metadata={"schema_property_name": "properties"})
14+
artifact_location : ArtifactLocation = field(metadata={"schema_property_name": "artifactLocation"})
15+
replacements : list[Replacement] = field(metadata={"schema_property_name": "replacements"})
16+
properties : PropertyBag = field(default=None, metadata={"schema_property_name": "properties"})

sarif_om/_artifact_content.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
# This file was generated by jschema_to_python version 1.2.3.
22

3-
import attr
3+
from ._multiformat_message_string import MultiformatMessageString
4+
from ._property_bag import PropertyBag
5+
from attrs import define
6+
from attrs import field
47

58

6-
@attr.s
7-
class ArtifactContent(object):
9+
@define()
10+
class ArtifactContent:
811
"""Represents the contents of an artifact."""
912

10-
binary = attr.ib(default=None, metadata={"schema_property_name": "binary"})
11-
properties = attr.ib(default=None, metadata={"schema_property_name": "properties"})
12-
rendered = attr.ib(default=None, metadata={"schema_property_name": "rendered"})
13-
text = attr.ib(default=None, metadata={"schema_property_name": "text"})
13+
binary : str = field(default=None, metadata={"schema_property_name": "binary"})
14+
properties : PropertyBag = field(default=None, metadata={"schema_property_name": "properties"})
15+
rendered : MultiformatMessageString = field(default=None, metadata={"schema_property_name": "rendered"})
16+
text : str = field(default=None, metadata={"schema_property_name": "text"})

sarif_om/_artifact_location.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
# This file was generated by jschema_to_python version 1.2.3.
22

3-
import attr
3+
from ._message import Message
4+
from ._property_bag import PropertyBag
5+
from attrs import define
6+
from attrs import field
47

58

6-
@attr.s
7-
class ArtifactLocation(object):
9+
@define()
10+
class ArtifactLocation:
811
"""Specifies the location of an artifact."""
912

10-
description = attr.ib(default=None, metadata={"schema_property_name": "description"})
11-
index = attr.ib(default=-1, metadata={"schema_property_name": "index"})
12-
properties = attr.ib(default=None, metadata={"schema_property_name": "properties"})
13-
uri = attr.ib(default=None, metadata={"schema_property_name": "uri"})
14-
uri_base_id = attr.ib(default=None, metadata={"schema_property_name": "uriBaseId"})
13+
description : Message = field(default=None, metadata={"schema_property_name": "description"})
14+
index : int = field(default=-1, metadata={"schema_property_name": "index"})
15+
properties : PropertyBag = field(default=None, metadata={"schema_property_name": "properties"})
16+
uri : str = field(default=None, metadata={"schema_property_name": "uri"})
17+
uri_base_id : str = field(default=None, metadata={"schema_property_name": "uriBaseId"})

sarif_om/_attachment.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
11
# This file was generated by jschema_to_python version 1.2.3.
22

3-
import attr
3+
from ._artifact_location import ArtifactLocation
4+
from ._message import Message
5+
from ._property_bag import PropertyBag
6+
from ._rectangle import Rectangle
7+
from ._region import Region
8+
from attrs import define
9+
from attrs import field
410

511

6-
@attr.s
7-
class Attachment(object):
12+
@define()
13+
class Attachment:
814
"""An artifact relevant to a result."""
915

10-
artifact_location = attr.ib(metadata={"schema_property_name": "artifactLocation"})
11-
description = attr.ib(default=None, metadata={"schema_property_name": "description"})
12-
properties = attr.ib(default=None, metadata={"schema_property_name": "properties"})
13-
rectangles = attr.ib(default=None, metadata={"schema_property_name": "rectangles"})
14-
regions = attr.ib(default=None, metadata={"schema_property_name": "regions"})
16+
artifact_location : ArtifactLocation = field(metadata={"schema_property_name": "artifactLocation"})
17+
description : Message = field(default=None, metadata={"schema_property_name": "description"})
18+
properties : PropertyBag = field(default=None, metadata={"schema_property_name": "properties"})
19+
rectangles : list[Rectangle] = field(factory=list, metadata={"schema_property_name": "rectangles"})
20+
regions : list[Region] = field(factory=list, metadata={"schema_property_name": "regions"})

sarif_om/_code_flow.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
# This file was generated by jschema_to_python version 1.2.3.
22

3-
import attr
3+
from ._message import Message
4+
from ._property_bag import PropertyBag
5+
from ._thread_flow import ThreadFlow
6+
from attrs import define
7+
from attrs import field
48

59

6-
@attr.s
7-
class CodeFlow(object):
10+
@define()
11+
class CodeFlow:
812
"""A set of threadFlows which together describe a pattern of code execution relevant to detecting a result."""
913

10-
thread_flows = attr.ib(metadata={"schema_property_name": "threadFlows"})
11-
message = attr.ib(default=None, metadata={"schema_property_name": "message"})
12-
properties = attr.ib(default=None, metadata={"schema_property_name": "properties"})
14+
thread_flows : list[ThreadFlow] = field(metadata={"schema_property_name": "threadFlows"})
15+
message : Message = field(default=None, metadata={"schema_property_name": "message"})
16+
properties : PropertyBag = field(default=None, metadata={"schema_property_name": "properties"})

sarif_om/_configuration_override.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
# This file was generated by jschema_to_python version 1.2.3.
22

3-
import attr
3+
from ._property_bag import PropertyBag
4+
from ._reporting_configuration import ReportingConfiguration
5+
from ._reporting_descriptor_reference import ReportingDescriptorReference
6+
from attrs import define
7+
from attrs import field
48

59

6-
@attr.s
7-
class ConfigurationOverride(object):
10+
@define()
11+
class ConfigurationOverride:
812
"""Information about how a specific rule or notification was reconfigured at runtime."""
913

10-
configuration = attr.ib(metadata={"schema_property_name": "configuration"})
11-
descriptor = attr.ib(metadata={"schema_property_name": "descriptor"})
12-
properties = attr.ib(default=None, metadata={"schema_property_name": "properties"})
14+
configuration : ReportingConfiguration = field(metadata={"schema_property_name": "configuration"})
15+
descriptor : ReportingDescriptorReference = field(metadata={"schema_property_name": "descriptor"})
16+
properties : PropertyBag = field(default=None, metadata={"schema_property_name": "properties"})

sarif_om/_conversion.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
# This file was generated by jschema_to_python version 1.2.3.
22

3-
import attr
3+
from ._artifact_location import ArtifactLocation
4+
from ._invocation import Invocation
5+
from ._property_bag import PropertyBag
6+
from ._tool import Tool
7+
from attrs import define
8+
from attrs import field
49

510

6-
@attr.s
7-
class Conversion(object):
11+
@define()
12+
class Conversion:
813
"""Describes how a converter transformed the output of a static analysis tool from the analysis tool's native output format into the SARIF format."""
914

10-
tool = attr.ib(metadata={"schema_property_name": "tool"})
11-
analysis_tool_log_files = attr.ib(default=None, metadata={"schema_property_name": "analysisToolLogFiles"})
12-
invocation = attr.ib(default=None, metadata={"schema_property_name": "invocation"})
13-
properties = attr.ib(default=None, metadata={"schema_property_name": "properties"})
15+
tool : Tool = field(metadata={"schema_property_name": "tool"})
16+
analysis_tool_log_files : list[ArtifactLocation] = field(factory=list, metadata={"schema_property_name": "analysisToolLogFiles"})
17+
invocation : Invocation = field(default=None, metadata={"schema_property_name": "invocation"})
18+
properties : PropertyBag = field(default=None, metadata={"schema_property_name": "properties"})

0 commit comments

Comments
 (0)