Skip to content

Commit 72d762f

Browse files
authored
Merge pull request #26 from alanisaac/static-analysis
Update GitHub actions to run static analysis
2 parents 76c0f30 + 6f2aa6d commit 72d762f

File tree

11 files changed

+100
-58
lines changed

11 files changed

+100
-58
lines changed

.github/actions/setup/action.yml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
name: "setup-ocsf-validator"
2+
description: "Sets up the CI environment for the ocsf-validator"
3+
inputs:
4+
python-version:
5+
description: Python version to use (e.g. "3.11")
6+
required: true
7+
runs:
8+
using: "composite"
9+
steps:
10+
- name: Set up Python ${{ inputs.python-version }}
11+
uses: actions/setup-python@v4
12+
with:
13+
python-version: ${{ inputs.python-version }}
14+
- name: Install Poetry
15+
uses: snok/install-poetry@v1
16+
- name: Install dependencies
17+
shell: bash
18+
run: poetry install

.github/workflows/ocsf-validator.yml

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
name: ocsf-validator
2+
3+
on:
4+
push:
5+
branches: [ "main" ]
6+
pull_request:
7+
branches: [ "main" ]
8+
9+
jobs:
10+
pytest:
11+
runs-on: ubuntu-latest
12+
strategy:
13+
matrix:
14+
python-version: ["3.11", "3.12"]
15+
steps:
16+
- uses: actions/checkout@v4
17+
- name: Set up environment
18+
uses: "./.github/actions/setup"
19+
with:
20+
python-version: ${{ matrix.python-version }}
21+
- name: pytest
22+
run: poetry run pytest
23+
black:
24+
runs-on: ubuntu-latest
25+
steps:
26+
- uses: actions/checkout@v4
27+
- name: Set up environment
28+
uses: "./.github/actions/setup"
29+
with:
30+
python-version: "3.12"
31+
- name: black
32+
run: poetry run black --check .
33+
isort:
34+
runs-on: ubuntu-latest
35+
steps:
36+
- uses: actions/checkout@v4
37+
- name: Set up environment
38+
uses: "./.github/actions/setup"
39+
with:
40+
python-version: "3.12"
41+
- name: isort
42+
run: poetry run isort --check .
43+
pyright:
44+
runs-on: ubuntu-latest
45+
steps:
46+
- uses: actions/checkout@v4
47+
- name: Set up environment
48+
uses: "./.github/actions/setup"
49+
with:
50+
python-version: "3.12"
51+
- name: pyright
52+
run: poetry run pyright

.github/workflows/test.yml

Lines changed: 0 additions & 27 deletions
This file was deleted.

ocsf_validator/errors.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
from typing import Iterable, Optional, TypeVar
1+
from __future__ import annotations
22

3-
TCollector = TypeVar("TCollector", bound="Collector")
3+
from typing import Iterable, Optional
44

55

66
class Collector:
@@ -11,7 +11,7 @@ class Collector:
1111
notice the collector until `throw` is `False`.
1212
"""
1313

14-
default: TCollector # type: ignore
14+
default: Collector
1515
"""Simple singleton used whenever an Optional[Collector] parameter is None."""
1616

1717
def __init__(self, throw: bool = True):
@@ -52,16 +52,13 @@ class ValidationError(Exception):
5252
...
5353

5454

55-
class InvalidBasePathError(ValidationError):
56-
...
55+
class InvalidBasePathError(ValidationError): ...
5756

5857

59-
class InvalidMetaSchemaError(ValidationError):
60-
...
58+
class InvalidMetaSchemaError(ValidationError): ...
6159

6260

63-
class InvalidMetaSchemaFileError(ValidationError):
64-
...
61+
class InvalidMetaSchemaFileError(ValidationError): ...
6562

6663

6764
class UnusedAttributeError(ValidationError):

ocsf_validator/matchers.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ def match(self, value: str):
3636
if matcher.match(value):
3737
return True
3838

39+
return False
40+
3941
def add(self, matcher: Matcher):
4042
self._matchers.append(matcher)
4143

@@ -123,12 +125,14 @@ def __init__(self):
123125
def get_type(self):
124126
return OcsfCategories
125127

128+
126129
class ExcludeMatcher(Matcher):
127130
"""
128131
A matcher that produces the opposite result of the matcher it's given.
129132
"""
133+
130134
def __init__(self, matcher: Matcher):
131135
self.matcher = matcher
132136

133137
def match(self, value: str) -> bool:
134-
return not self.matcher.match(value)
138+
return not self.matcher.match(value)

ocsf_validator/processor.py

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@
22
from typing import Any, Callable, Optional
33

44
from ocsf_validator.errors import *
5-
from ocsf_validator.matchers import (
6-
CategoriesMatcher,
7-
ExcludeMatcher
8-
)
5+
from ocsf_validator.matchers import CategoriesMatcher, ExcludeMatcher
96
from ocsf_validator.reader import Reader
107
from ocsf_validator.type_mapping import TypeMapping
118
from ocsf_validator.types import (
@@ -259,7 +256,7 @@ class ProfilesParser(MergeParser):
259256
def applies_to(self, t: type) -> bool:
260257
if hasattr(t, "__required_keys__") or hasattr(t, "__optional_keys"):
261258
return (
262-
PROFILES_KEY in t.__required_keys__
259+
PROFILES_KEY in t.__required_keys__ # type: ignore
263260
or PROFILES_KEY in t.__optional_keys__ # type: ignore
264261
)
265262
else:
@@ -289,7 +286,7 @@ class AttributesParser(MergeParser):
289286
def applies_to(self, t: type) -> bool:
290287
if hasattr(t, "__required_keys__") or hasattr(t, "__optional_keys"):
291288
return (
292-
ATTRIBUTES_KEY in t.__required_keys__
289+
ATTRIBUTES_KEY in t.__required_keys__ # type: ignore
293290
or ATTRIBUTES_KEY in t.__optional_keys__ # type: ignore
294291
)
295292
else:
@@ -468,9 +465,7 @@ def process_includes(
468465

469466
# categories cannot be extended with dependencies, and it causes problems
470467
# if we try to include dictionary attributes in categories
471-
matcher = ExcludeMatcher(
472-
CategoriesMatcher()
473-
)
468+
matcher = ExcludeMatcher(CategoriesMatcher())
474469

475470
for path in reader.match(matcher):
476471
for directive, parser in parsers.items():

ocsf_validator/validators.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,7 @@
4747
leaf_type,
4848
)
4949

50-
51-
METASCHEMA_MATCHERS = {
50+
METASCHEMA_MATCHERS = {
5251
"event.schema.json": EventMatcher(),
5352
"include.schema.json": IncludeMatcher(),
5453
"object.schema.json": ObjectMatcher(),
@@ -269,10 +268,11 @@ def validate(reader: Reader, file: str):
269268

270269
def _default_get_registry(reader: Reader, base_uri: str) -> referencing.Registry:
271270
registry: referencing.Registry = referencing.Registry()
272-
for schema_file_path in reader.metaschema_path.glob("*.schema.json"):
271+
272+
for schema_file_path in reader.metaschema_path.glob("*.schema.json"): # type: ignore
273273
with open(schema_file_path, "r") as file:
274274
schema = json.load(file)
275-
resource = referencing.Resource.from_contents(schema)
275+
resource = referencing.Resource.from_contents(schema) # type: ignore
276276
registry = registry.with_resource(
277277
base_uri + schema_file_path.name, resource=resource
278278
)

pyproject.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,6 @@ pyright = "^1.1.327"
2424
[build-system]
2525
requires = ["poetry-core"]
2626
build-backend = "poetry.core.masonry.api"
27+
28+
[tool.isort]
29+
profile = "black"

tests/test_dependencies.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,24 @@
1+
from typing import Any
2+
13
import pytest
24

35
from ocsf_validator.errors import *
46
from ocsf_validator.processor import *
57
from ocsf_validator.reader import DictReader, Reader
68

79

8-
def attributes(attrs=[]):
10+
def attributes(attrs: list = []) -> dict[str, Any]:
911
d = {}
1012
for a in attrs:
1113
d[a] = {"name": a}
1214
return {"attributes": d}
1315

1416

15-
def obj(name="object", attrs=[]):
17+
def obj(name: str = "object", attrs: list = []) -> dict[str, Any]:
1618
return {"name": name, "caption": ""} | attributes(attrs)
1719

1820

19-
def event(name="event", attrs=[]):
21+
def event(name: str = "event", attrs: list = []) -> dict[str, Any]:
2022
return {"name": name, "caption": ""} | attributes(attrs)
2123

2224

tests/test_matchers.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,7 @@ def test_extension_matcher():
4343

4444

4545
def test_exclude_matcher():
46-
m = ExcludeMatcher(
47-
ExtensionMatcher()
48-
)
46+
m = ExcludeMatcher(ExtensionMatcher())
4947

5048
assert m.match("/extensions/ext1/extension.json") is False
5149
assert m.match("/extension.json") is True

0 commit comments

Comments
 (0)