Skip to content

Commit

Permalink
Add typing to metadata.py
Browse files Browse the repository at this point in the history
  • Loading branch information
danielhollas committed Aug 7, 2023
1 parent e139446 commit 608437e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 16 deletions.
31 changes: 18 additions & 13 deletions aiidalab/metadata.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
"""App metadata specification"""
from __future__ import annotations

from configparser import ConfigParser
from configparser import ConfigParser, SectionProxy
from dataclasses import dataclass, field
from pathlib import Path
from typing import Any, Generator

__all__ = [
"Metadata",
]


def _map_development_state(classifiers):
def _map_development_state(classifiers: str | list[str]) -> str:
"Map standard trove classifiers (PEP 301) to aiidalab development states."
if "Development Status :: 1 - Planning" in classifiers:
return "registered"
Expand All @@ -28,24 +30,27 @@ def _map_development_state(classifiers):
return "registered"


def _parse_config_dict(dict_):
def _parse_config_dict(dict_: str) -> Generator[tuple[str, str], None, None]:
"Parse a config dict string for key-values pairs."
for line in dict_.splitlines():
if line:
key, value = line.split("=")
yield key.strip(), value.strip()


def _parse_setup_cfg(setup_cfg):
def _parse_setup_cfg(
setup_cfg: str,
) -> Generator[tuple[str, str | list[str]], None, None]:
"Parse a setup.cfg configuration file string for metadata."
cfg = ConfigParser()
cfg.read_string(setup_cfg)

try:
metadata_pep426 = cfg["metadata"]
except KeyError:
metadata_pep426 = dict()
aiidalab = cfg["aiidalab"] if "aiidalab" in cfg else dict()
metadata_pep426: SectionProxy | dict[Any, Any] = (
cfg["metadata"] if "metadata" in cfg else {}
)
aiidalab: SectionProxy | dict[Any, Any] = (
cfg["aiidalab"] if "aiidalab" in cfg else {}
)

yield "title", aiidalab.get("title", metadata_pep426.get("name"))
yield "version", aiidalab.get("version", metadata_pep426.get("version"))
Expand All @@ -62,12 +67,12 @@ def _parse_setup_cfg(setup_cfg):
"logo", project_urls.get("Logo") or project_urls.get("logo")
)
yield "state", aiidalab.get(
"state", _map_development_state(metadata_pep426.get("classifiers", []))
"state", _map_development_state(metadata_pep426.get("classifiers", ""))
)

# Allow passing single category and convert to list
# and allow parse line separated string as list
categories = aiidalab.get("categories", [])
categories = aiidalab.get("categories", "")
if isinstance(categories, str):
categories = [c for c in categories.split("\n") if c]
yield "categories", categories
Expand All @@ -90,7 +95,7 @@ class Metadata:
_search_dirs = (".aiidalab", "./")

@staticmethod
def _parse(path):
def _parse(path: Path) -> dict[str, Any]:
try:
return {
key: value
Expand All @@ -103,7 +108,7 @@ def _parse(path):
return {}

@classmethod
def parse(cls, root):
def parse(cls, root: Path) -> Metadata:
"""Parse the app metadata from a setup.cfg within the app repository.
This function will parse metadata fields from a possible "aiidalab"
Expand Down
3 changes: 0 additions & 3 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,6 @@ disallow_untyped_calls = False
disallow_incomplete_defs = True
warn_return_any = True

[mypy-aiidalab.metadata.*]
ignore_errors = True

[mypy-aiidalab.__main__.*]
ignore_errors = True

Expand Down

0 comments on commit 608437e

Please sign in to comment.