Skip to content

Commit

Permalink
update type annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
StardustDL committed Jan 10, 2024
1 parent dfc2822 commit 3af8b8e
Show file tree
Hide file tree
Showing 44 changed files with 507 additions and 960 deletions.
21 changes: 1 addition & 20 deletions core/aexpy/__init__.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,3 @@
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

from datetime import datetime
import logging
import os
Expand All @@ -23,9 +6,7 @@
__version__ = "0.2.0"


LOGGING_FORMAT = (
"%(levelname)s %(asctime)s %(name)s [%(pathname)s:%(lineno)d:%(funcName)s]\n%(message)s\n"
)
LOGGING_FORMAT = "%(levelname)s %(asctime)s %(name)s [%(pathname)s:%(lineno)d:%(funcName)s]\n%(message)s\n"
LOGGING_DATEFMT = "%Y-%m-%d,%H:%M:%S"


Expand Down
29 changes: 9 additions & 20 deletions core/aexpy/__main__.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,3 @@
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

import code
import logging
import pathlib
Expand Down Expand Up @@ -139,7 +122,9 @@ def preprocess(
view: bool,
):
"""Generate a release definition."""
assert view or (rootpath and modules), "Please give the input file or use the view mode."
assert view or (
rootpath and modules
), "Please give the input file or use the view mode."

mode = ProduceMode.Read if view else ProduceMode.Write

Expand Down Expand Up @@ -248,12 +233,16 @@ def diff(old: pathlib.Path, new: pathlib.Path, output: pathlib.Path, view: bool)
mode = ProduceMode.Read if view else ProduceMode.Write

oldData = (
services.extract(FileProduceCache("", old), getUnknownDistribution(), ProduceMode.Read)
services.extract(
FileProduceCache("", old), getUnknownDistribution(), ProduceMode.Read
)
if not view
else ApiDescription(distribution=getUnknownDistribution())
)
newData = (
services.extract(FileProduceCache("", new), getUnknownDistribution(), ProduceMode.Read)
services.extract(
FileProduceCache("", new), getUnknownDistribution(), ProduceMode.Read
)
if not view
else ApiDescription(distribution=getUnknownDistribution())
)
Expand Down
31 changes: 7 additions & 24 deletions core/aexpy/caching/__init__.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,3 @@
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

from abc import ABC, abstractmethod
from pathlib import Path

Expand All @@ -23,16 +6,16 @@


class ProduceCache(ABC):
def __init__(self, id: "str") -> None:
def __init__(self, id: str) -> None:
super().__init__()
self.id = id

@abstractmethod
def save(self, product: "Product", log: str) -> None:
def save(self, product: Product, log: str) -> None:
pass

@abstractmethod
def data(self) -> "Product":
def data(self) -> Product:
pass

@abstractmethod
Expand All @@ -41,19 +24,19 @@ def log(self) -> str:


class FileProduceCache(ProduceCache):
def __init__(self, id: "str", cacheFile: "Path") -> None:
def __init__(self, id: str, cacheFile: Path):
super().__init__(id)
self.cacheFile = cacheFile
# self.logFile = self.cacheFile.with_suffix(".log")

def save(self, product: "Product", log: "str") -> None:
def save(self, product: Product, log: str):
ensureDirectory(self.cacheFile.parent)
self.cacheFile.write_text(product.dumps())
# self.logFile.write_text(log)

def data(self) -> "str":
def data(self):
return self.cacheFile.read_text()

def log(self) -> str:
def log(self):
# return self.logFile.read_text()
pass
19 changes: 1 addition & 18 deletions core/aexpy/diffing/__init__.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,8 @@
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

from ..models import ApiDescription, ApiDifference
from ..producers import Producer


class Differ(Producer):
def diff(self, old: "ApiDescription", new: "ApiDescription", product: "ApiDifference"):
def diff(self, old: ApiDescription, new: ApiDescription, product: ApiDifference):
"""Diff two versions of the API and return the differences."""
pass
19 changes: 1 addition & 18 deletions core/aexpy/diffing/default.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,3 @@
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

from logging import Logger
from pathlib import Path
from uuid import uuid1
Expand All @@ -29,7 +12,7 @@


class DefaultDiffer(Differ):
def diff(self, old: "ApiDescription", new: "ApiDescription", product: "ApiDifference"):
def diff(self, old: ApiDescription, new: ApiDescription, product: ApiDifference):
from .differs.default import DefaultDiffer

DefaultDiffer(self.logger).diff(old, new, product)
Expand Down
16 changes: 0 additions & 16 deletions core/aexpy/diffing/differs/__init__.py
Original file line number Diff line number Diff line change
@@ -1,16 +0,0 @@
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
87 changes: 36 additions & 51 deletions core/aexpy/diffing/differs/checkers.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,15 @@
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

import dataclasses
from dataclasses import dataclass, field
from typing import Any, Callable, TypeVar
from typing import Any, Callable, TypeVar, Type, cast

from aexpy.models.description import ApiEntry
from aexpy.models.difference import BreakingRank

from aexpy.models import ApiDescription, DiffEntry

T_ApiEntry = TypeVar("T_ApiEntry", bound=ApiEntry)
T_Checker = Callable[
[ApiEntry | None, ApiEntry | None, ApiDescription, ApiDescription], list[DiffEntry]
]


class DiffConstraint:
Expand All @@ -36,82 +21,82 @@ class DiffConstraint:

def __init__(
self,
kind: "str" = "",
checker: "Callable[[T_ApiEntry | None, T_ApiEntry | None, ApiDescription, ApiDescription], list[DiffEntry]] | None" = None,
kind: str = "",
checker: T_Checker | None = None,
) -> None:
if checker is None:

def tchecker(a: Any, b: Any, old: Any, new: Any):
return []

checker = tchecker
self.checker: "Callable[[T_ApiEntry | None, T_ApiEntry | None, ApiDescription, ApiDescription], list[DiffEntry]]" = (
checker
self.checker = (
checker if checker else cast(T_Checker, lambda a, b, old, new: [])
)
self.kind = kind

def askind(self, kind: "str"):
def askind(self, kind: str):
"""Set kind."""

self.kind = kind
return self

def fortype(self, type, optional: bool = False):
def fortype(self, type: Type, optional: bool = False):
"""Limit to a type of ApiEntry."""

oldchecker = self.checker

def checker(a, b, **kwargs):
def checker(a, b, old, new) -> list[DiffEntry]:
if optional:
if not isinstance(a, type):
a = None
if not isinstance(b, type):
b = None
if a or b:
return oldchecker(a, b, **kwargs) # type: ignore
return []
return oldchecker(a, b, old, new)
else:
if isinstance(a, type) and isinstance(b, type):
return oldchecker(a, b, **kwargs) # type: ignore
else:
return []
return oldchecker(a, b, old, new)
return []

self.checker = checker # type: ignore
self.checker = cast(T_Checker, checker)
return self

def __call__(self, old, new, oldCollection, newCollection) -> "list[DiffEntry]":
result = self.checker(old, new, old=oldCollection, new=newCollection) # type: ignore
if result:
return [
dataclasses.replace(entry, kind=self.kind, old=old, new=new) for entry in result
def __call__(
self,
old: ApiEntry | None,
new: ApiEntry | None,
oldCollection: ApiDescription,
newCollection: ApiDescription,
) -> list[DiffEntry]:
result = self.checker(old, new, oldCollection, newCollection)
return (
[
dataclasses.replace(entry, kind=self.kind, old=old, new=new)
for entry in result
]
else:
return []
if result
else []
)


@dataclass
class DiffConstraintCollection:
"""Collection of DiffConstraint."""

constraints: "list[DiffConstraint]" = field(default_factory=list)
constraints: list[DiffConstraint] = field(default_factory=list)

def cons(self, constraint: "DiffConstraint"):
def cons(self, constraint: DiffConstraint):
self.constraints.append(constraint)
return constraint


def diffcons(
checker: "Callable[[T_ApiEntry, T_ApiEntry, ApiDescription, ApiDescription], list[DiffEntry]]",
) -> "DiffConstraint":
checker: T_Checker,
) -> DiffConstraint:
"""Create a DiffConstraint on a function."""

return DiffConstraint(checker.__name__, checker) # type: ignore


def fortype(type, optional: "bool" = False):
def fortype(type, optional: bool = False):
"""Limit the diff constraint to a type of ApiEntry."""

def decorator(constraint: "DiffConstraint") -> "DiffConstraint":
def decorator(constraint: DiffConstraint) -> DiffConstraint:
return constraint.fortype(type, optional)

return decorator
Loading

0 comments on commit 3af8b8e

Please sign in to comment.