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

full typing with mypy #16

Merged
merged 13 commits into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 23 additions & 13 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,33 @@ on:
release:
types: published
schedule:
- cron: 0 0 1 * * # Run once every month
- cron: 0 0 1 * * # Run once every month
jobs:
run-pytest:
strategy:
fail-fast: false
matrix:
py: ['3.8', '3.9', '3.10', '3.11', '3.12']
os: ['ubuntu-latest', 'windows-latest', 'macos-latest']
py: [ '3.9', '3.10', '3.11', '3.12' ]
os: [ 'ubuntu-latest', 'windows-latest', 'macos-latest' ]
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: ${{ matrix.py }}
- name: "Install requirements"
run: |
python -m pip install -r requirements-dev.txt
- name: "Run pytest"
run: |
pytest
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: ${{ matrix.py }}
- name: "Install requirements"
run: |
python -m pip install -r requirements-dev.txt
- name: "Run pytest"
run: |
pytest

check-typing:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: "3.9"
- run: pip install mypy
- run: mypy --install-types --non-interactive
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
/build/
/.eggs/
/venv/
/.venv
*.egg-info
*.py[co]
2 changes: 2 additions & 0 deletions cmake_file_api/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from .cmake import CMakeProject
from .errors import CMakeException
from .kinds.kind import ObjectKind

__all__ = ["CMakeProject", "CMakeException", "ObjectKind"]
11 changes: 6 additions & 5 deletions cmake_file_api/cmake.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
from pathlib import Path
import subprocess
from typing import List, Optional, Union
from typing import Optional, Union

from .reply.api import REPLY_API
from .reply.v1.api import CMakeFileApiV1

PathLike = Union[Path, str]


class CMakeProject(object):
class CMakeProject:
__slots__ = ("_source_path", "_build_path", "_api_version", "_cmake")

def __init__(self, build_path: PathLike, source_path: Optional[PathLike]=None, api_version: Optional[int]=None, cmake: Optional[str]=None):
Expand Down Expand Up @@ -57,14 +58,14 @@ def _cache_lookup(name: str, cache: PathLike) -> str:
_, value = line.split("=", 1)
return value

def configure(self, args: Optional[List[str]]=None, quiet=False):
def configure(self, args: Optional[list[str]]=None, quiet: bool = False) -> None:
if self._source_path is None:
raise ValueError("Cannot configure with no source path")
stdout = subprocess.DEVNULL if quiet else None
args = [str(self._cmake), str(self._source_path)] + (args if args else [])
subprocess.check_call(args, cwd=str(self._build_path), stdout=stdout)

def reconfigure(self, quiet=False):
def reconfigure(self, quiet: bool = False) -> None:
stdout = subprocess.DEVNULL if quiet else None
args = [str(self._cmake)]
if self._source_path:
Expand All @@ -74,5 +75,5 @@ def reconfigure(self, quiet=False):
subprocess.check_call(args, cwd=str(self._build_path), stdout=stdout)

@property
def cmake_file_api(self):
def cmake_file_api(self) -> CMakeFileApiV1:
return REPLY_API[self._api_version](self._build_path)
13 changes: 12 additions & 1 deletion cmake_file_api/kinds/api.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
from pathlib import Path
from typing import Protocol
from typing_extensions import Self

from .kind import ObjectKind
from .cache.api import CACHE_API
from .cmakeFiles.api import CMAKEFILES_API
Expand All @@ -6,7 +10,14 @@
from .toolchains.api import TOOLCHAINS_API


OBJECT_KINDS_API = {
class CMakeApiType(Protocol):
KIND: ObjectKind

@classmethod
def from_path(cls, path: Path, reply_path: Path) -> Self:
...

OBJECT_KINDS_API: dict[ObjectKind, dict[int, CMakeApiType]] = {
ObjectKind.CACHE: CACHE_API,
ObjectKind.CMAKEFILES: CMAKEFILES_API,
ObjectKind.CONFIGURELOG: CONFIGURELOG_API,
Expand Down
2 changes: 2 additions & 0 deletions cmake_file_api/kinds/cache/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
from .api import CACHE_API

__all__ = ["CACHE_API"]
7 changes: 6 additions & 1 deletion cmake_file_api/kinds/cache/api.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
from __future__ import annotations
import typing

from .v2 import CacheV2

if typing.TYPE_CHECKING:
from ..api import CMakeApiType

CACHE_API = {
CACHE_API: dict[int, CMakeApiType] = {
2: CacheV2,
}
20 changes: 10 additions & 10 deletions cmake_file_api/kinds/cache/v2.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from enum import Enum
import json
from pathlib import Path
from typing import Dict, List
from typing import Any

from cmake_file_api.kinds.common import VersionMajorMinor
from cmake_file_api.kinds.kind import ObjectKind
Expand All @@ -17,15 +17,15 @@ class CacheEntryType(Enum):
TYPE_UNINITIALIZED = "UNINITIALIZED"


class CacheEntryProperty(object):
class CacheEntryProperty:
__slots__ = ("name", "value")

def __init__(self, name: str, value: str):
self.name = name
self.value = value

@classmethod
def from_dict(cls, dikt: Dict) -> "CacheEntryProperty":
def from_dict(cls, dikt: dict[str, Any]) -> "CacheEntryProperty":
name = dikt["name"]
value = dikt["value"]
return cls(name, value)
Expand All @@ -38,17 +38,17 @@ def __repr__(self) -> str:
)


class CacheEntry(object):
class CacheEntry:
__slots__ = ("name", "value", "type", "properties")

def __init__(self, name: str, value: str, type: CacheEntryType, properties: List[CacheEntryProperty]):
def __init__(self, name: str, value: str, type: CacheEntryType, properties: list[CacheEntryProperty]):
self.name = name
self.value = value
self.type = type
self.properties = properties

@classmethod
def from_dict(cls, dikt: Dict) -> "CacheEntry":
def from_dict(cls, dikt: dict[str, Any]) -> "CacheEntry":
name = dikt["name"]
value = dikt["value"]
type = CacheEntryType(dikt["type"])
Expand All @@ -65,25 +65,25 @@ def __repr__(self) -> str:
)


class CacheV2(object):
class CacheV2:
KIND = ObjectKind.CACHE

__slots__ = ("version", "entries")

def __init__(self, version: VersionMajorMinor, entries: List[CacheEntry]):
def __init__(self, version: VersionMajorMinor, entries: list[CacheEntry]):
self.version = version
self.entries = entries

@classmethod
def from_dict(cls, dikt: Dict, reply_path) -> "CacheModelV2":
def from_dict(cls, dikt: dict[str, Any], reply_path: Path) -> "CacheV2":
if dikt["kind"] != cls.KIND.value:
raise ValueError
version = VersionMajorMinor.from_dict(dikt["version"])
entries = list(CacheEntry.from_dict(ce) for ce in dikt["entries"])
return cls(version, entries)

@classmethod
def from_path(cls, path: Path, reply_path: Path) -> "CacheModelV2":
def from_path(cls, path: Path, reply_path: Path) -> "CacheV2":
dikt = json.load(path.open())
return cls.from_dict(dikt, reply_path)

Expand Down
7 changes: 6 additions & 1 deletion cmake_file_api/kinds/cmakeFiles/api.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
from __future__ import annotations
import typing

from .v1 import CMakeFilesV1

if typing.TYPE_CHECKING:
from ..api import CMakeApiType

CMAKEFILES_API = {
CMAKEFILES_API: dict[int, CMakeApiType] = {
1: CMakeFilesV1,
}
14 changes: 7 additions & 7 deletions cmake_file_api/kinds/cmakeFiles/v1.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import json
from pathlib import Path
from typing import Dict, List, Optional
from typing import Any, Optional

from cmake_file_api.kinds.common import CMakeSourceBuildPaths, VersionMajorMinor
from cmake_file_api.kinds.kind import ObjectKind


class CMakeFilesInput(object):
class CMakeFilesInput:
__slots__ = ("path", "isGenerator", "isExternal", "isCMake")

def __init__(self, path: Path, isGenerator: Optional[bool], isExternal: Optional[bool], isCMake: Optional[bool]):
Expand All @@ -16,7 +16,7 @@ def __init__(self, path: Path, isGenerator: Optional[bool], isExternal: Optional
self.isCMake = isCMake

@classmethod
def from_dict(cls, dikt: Dict) -> "CMakeFileInput":
def from_dict(cls, dikt: dict[str, Any]) -> "CMakeFilesInput":
path = Path(dikt["path"])
isGenerator = dikt.get("isGenerator")
isExternal = dikt.get("isExternal")
Expand All @@ -33,25 +33,25 @@ def __repr__(self) -> str:
)


class CMakeFilesV1(object):
class CMakeFilesV1:
KIND = ObjectKind.CMAKEFILES

__slots__ = ("version", "paths", "inputs")

def __init__(self, version: VersionMajorMinor, paths: CMakeSourceBuildPaths, inputs: List[CMakeFilesInput]):
def __init__(self, version: VersionMajorMinor, paths: CMakeSourceBuildPaths, inputs: list[CMakeFilesInput]):
self.version = version
self.paths = paths
self.inputs = inputs

@classmethod
def from_dict(cls, dikt: Dict, reply_path: Path) -> "CmakeFilesV2":
def from_dict(cls, dikt: dict[str, Any], reply_path: Path) -> "CMakeFilesV1":
version = VersionMajorMinor.from_dict(dikt["version"])
paths = CMakeSourceBuildPaths.from_dict(dikt["paths"])
inputs = list(CMakeFilesInput.from_dict(cmi) for cmi in dikt["inputs"])
return cls(version, paths, inputs)

@classmethod
def from_path(cls, path: Path, reply_path: Path) -> "CmakeFilesV2":
def from_path(cls, path: Path, reply_path: Path) -> "CMakeFilesV1":
dikt = json.load(path.open())
return cls.from_dict(dikt, reply_path)

Expand Down
8 changes: 7 additions & 1 deletion cmake_file_api/kinds/codemodel/api.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
from __future__ import annotations
import typing

from .v2 import CodemodelV2


CODEMODEL_API = {
if typing.TYPE_CHECKING:
from ..api import CMakeApiType

CODEMODEL_API: dict[int, CMakeApiType] = {
2: CodemodelV2,
}
Loading