Skip to content

full typing with mypy #16

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

Merged
merged 13 commits into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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]
9 changes: 5 additions & 4 deletions cmake_file_api/cmake.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
from typing import List, 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)
16 changes: 8 additions & 8 deletions cmake_file_api/kinds/cache/v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -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) -> "CacheEntryProperty":
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's now possible to remove the unused typing imports?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

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) -> "CacheEntry":
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, does the dikt need a more specialized typing annotation?
e.g. dict[str, object]?

I see you've changed it into dict[str, Any] in other locations.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, needs to be changed everywhere to comply. Long-term it would be great to replace the Any as well, perhaps

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When/if that becomes possible, you'd effectively replace a lot of work these classes do.

name = dikt["name"]
value = dikt["value"]
type = CacheEntryType(dikt["type"])
Expand All @@ -65,17 +65,17 @@ 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) -> "CacheV2":
def from_dict(cls, dikt: dict, reply_path) -> "CacheV2":
if dikt["kind"] != cls.KIND.value:
raise ValueError
version = VersionMajorMinor.from_dict(dikt["version"])
Expand Down
12 changes: 6 additions & 6 deletions cmake_file_api/kinds/cmakeFiles/v1.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
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) -> "CMakeFilesInput":
def from_dict(cls, dikt: dict) -> "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, 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
Loading