Skip to content

Commit

Permalink
🔧 Add strict typing for sphinx_needs.needsfile
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisjsewell committed Aug 28, 2023
1 parent a19f4b0 commit f3725a0
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 21 deletions.
1 change: 1 addition & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ repos:
additional_dependencies:
- sphinx==6
- types-docutils
- types-jsonschema
- types-requests

- repo: https://github.com/python-poetry/poetry
Expand Down
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,6 @@ module = [
'sphinx_needs.functions.common',
'sphinx_needs.functions.functions',
'sphinx_needs.layout',
'sphinx_needs.needsfile',
'sphinx_needs.roles.need_ref',
'sphinx_needs.services.github',
'sphinx_needs.utils',
Expand Down
37 changes: 17 additions & 20 deletions sphinx_needs/needsfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@
from typing import Any, List

from jsonschema import Draft7Validator
from sphinx.config import Config

from sphinx_needs.data import NeedsFilterType
from sphinx_needs.data import NeedsFilterType, NeedsInfoType
from sphinx_needs.logging import get_logger

log = get_logger(__name__)
Expand Down Expand Up @@ -46,7 +47,7 @@ class NeedsList:
"content_node",
}

def __init__(self, config, outdir, confdir) -> None:
def __init__(self, config: Config, outdir: str, confdir: str) -> None:
self.config = config
self.outdir = outdir
self.confdir = confdir
Expand All @@ -60,7 +61,7 @@ def __init__(self, config, outdir, confdir) -> None:
}
self.log = log

def update_or_add_version(self, version) -> None:
def update_or_add_version(self, version: str) -> None:
if version not in self.needs_list["versions"].keys():
self.needs_list["versions"][version] = {
"created": "",
Expand All @@ -75,16 +76,16 @@ def update_or_add_version(self, version) -> None:

self.needs_list["versions"][version]["created"] = datetime.now().isoformat()

def add_need(self, version, need_info) -> None:
def add_need(self, version: str, need_info: NeedsInfoType) -> None:
self.update_or_add_version(version)
writable_needs = {key: need_info[key] for key in need_info if key not in self.JSON_KEY_EXCLUSIONS_NEEDS}
writable_needs = {key: need_info[key] for key in need_info if key not in self.JSON_KEY_EXCLUSIONS_NEEDS} # type: ignore[literal-required]
writable_needs["description"] = need_info["content"]
self.needs_list["versions"][version]["needs"][need_info["id"]] = writable_needs
self.needs_list["versions"][version]["needs_amount"] = len(self.needs_list["versions"][version]["needs"])

def add_filter(self, version, need_filter: NeedsFilterType) -> None:
def add_filter(self, version: str, need_filter: NeedsFilterType) -> None:
self.update_or_add_version(version)
writable_filters = {key: need_filter[key] for key in need_filter if key not in self.JSON_KEY_EXCLUSIONS_FILTERS}
writable_filters = {key: need_filter[key] for key in need_filter if key not in self.JSON_KEY_EXCLUSIONS_FILTERS} # type: ignore[literal-required]
self.needs_list["versions"][version]["filters"][need_filter["export_id"].upper()] = writable_filters
self.needs_list["versions"][version]["filters_amount"] = len(self.needs_list["versions"][version]["filters"])

Expand All @@ -98,13 +99,10 @@ def write_json(self, needs_file: str = "needs.json") -> None:
self.needs_list["current_version"] = self.current_version
self.needs_list["project"] = self.project

needs_json = json.dumps(self.needs_list, indent=4, sort_keys=True)
file = os.path.join(self.outdir, needs_file)
with open(os.path.join(self.outdir, needs_file), "w") as f:
json.dump(self.needs_list, f, indent=4, sort_keys=True)

with open(file, "w") as f:
f.write(needs_json)

def load_json(self, file) -> None:
def load_json(self, file: str) -> None:
if not os.path.isabs(file):
file = os.path.join(self.confdir, file)

Expand All @@ -120,13 +118,12 @@ def load_json(self, file) -> None:
self.log.info(f' {error.message} -> {".".join(error.path)}')

with open(file) as needs_file:
needs_file_content = needs_file.read()
try:
needs_list = json.loads(needs_file_content)
except json.JSONDecodeError:
self.log.warning(f"Could not decode json file {file} [needs]", type="needs")
else:
self.needs_list = needs_list
try:
needs_list = json.load(needs_file)
except json.JSONDecodeError:
self.log.warning(f"Could not decode json file {file} [needs]", type="needs")
else:
self.needs_list = needs_list

self.log.debug(f"needs.json file loaded: {file}")

Expand Down

0 comments on commit f3725a0

Please sign in to comment.