Skip to content

Commit

Permalink
update type annotation
Browse files Browse the repository at this point in the history
  • Loading branch information
StardustDL committed Jan 10, 2024
1 parent 3af8b8e commit 6dd88a7
Show file tree
Hide file tree
Showing 20 changed files with 302 additions and 337 deletions.
24 changes: 16 additions & 8 deletions core/aexpy/diffing/differs/checkers.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import dataclasses
from dataclasses import dataclass, field
from typing import Any, Callable, TypeVar, Type, cast
import functools
from typing import Any, Callable, TypeVar, Type, cast, TypeGuard

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

from aexpy.models import ApiDescription, DiffEntry

T_Checker = Callable[
type T_Checker = Callable[
[ApiEntry | None, ApiEntry | None, ApiDescription, ApiDescription], list[DiffEntry]
]

Expand Down Expand Up @@ -90,13 +91,20 @@ def diffcons(
) -> DiffConstraint:
"""Create a DiffConstraint on a function."""

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


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

def decorator(constraint: DiffConstraint) -> DiffConstraint:
return constraint.fortype(type, optional)
def wrapper(checker: Callable[[TEntry, TEntry, ApiDescription, ApiDescription], list[DiffEntry]]):
return DiffConstraint(checker.__name__, cast(T_Checker, checker)).fortype(type, False)
return wrapper

def typedConsOp[TEntry: ApiEntry](type: Type[TEntry]):
"""Limit the diff constraint to a type of ApiEntry."""

return decorator
def wrapper(checker: Callable[[TEntry, TEntry, ApiDescription, ApiDescription], list[DiffEntry]]):
return DiffConstraint(checker.__name__, cast(T_Checker, checker)).fortype(type, True)
return wrapper


38 changes: 11 additions & 27 deletions core/aexpy/diffing/differs/contraints/__init__.py
Original file line number Diff line number Diff line change
@@ -1,31 +1,15 @@
import functools
import itertools
from itertools import zip_longest
from typing import Callable, OrderedDict
from aexpy.models import ApiDescription

from aexpy.models.description import (
ApiEntry,
AttributeEntry,
ClassEntry,
CollectionEntry,
FunctionEntry,
ItemEntry,
ModuleEntry,
Parameter,
ParameterKind,
SpecialEntry,
SpecialKind,
)
from aexpy.models.description import ApiEntry
from aexpy.models.difference import DiffEntry


def add(
a: "ApiEntry | None",
b: "ApiEntry | None",
old: "ApiDescription",
new: "ApiDescription",
):
a: ApiEntry | None,
b: ApiEntry | None,
old: ApiDescription,
new: ApiDescription,
) -> list[DiffEntry]:
if a is None and b is not None:
return [
DiffEntry(
Expand All @@ -36,11 +20,11 @@ def add(


def remove(
a: "ApiEntry | None",
b: "ApiEntry | None",
old: "ApiDescription",
new: "ApiDescription",
):
a: ApiEntry | None,
b: ApiEntry | None,
old: ApiDescription,
new: ApiDescription,
) -> list[DiffEntry]:
if a is not None and b is None:
if a.parent in old.entries and a.parent not in new.entries:
# only report if parent exisits
Expand Down
11 changes: 4 additions & 7 deletions core/aexpy/diffing/differs/contraints/aliases.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@
from aexpy.models.description import CollectionEntry
from aexpy.models.difference import DiffEntry

from ..checkers import DiffConstraint, DiffConstraintCollection, diffcons, fortype
from ..checkers import DiffConstraintCollection, typedCons

AliasConstraints = DiffConstraintCollection()


@AliasConstraints.cons
@fortype(CollectionEntry)
@diffcons
@typedCons(CollectionEntry)
def AddAlias(
a: CollectionEntry, b: CollectionEntry, old: "ApiDescription", new: "ApiDescription"
):
Expand All @@ -25,8 +24,7 @@ def AddAlias(


@AliasConstraints.cons
@fortype(CollectionEntry)
@diffcons
@typedCons(CollectionEntry)
def RemoveAlias(
a: CollectionEntry, b: CollectionEntry, old: "ApiDescription", new: "ApiDescription"
):
Expand All @@ -42,8 +40,7 @@ def RemoveAlias(


@AliasConstraints.cons
@fortype(CollectionEntry)
@diffcons
@typedCons(CollectionEntry)
def ChangeAlias(
a: CollectionEntry, b: CollectionEntry, old: "ApiDescription", new: "ApiDescription"
):
Expand Down
27 changes: 11 additions & 16 deletions core/aexpy/diffing/differs/contraints/classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from aexpy.models.difference import DiffEntry
from aexpy.utils import getObjectId

from ..checkers import DiffConstraint, DiffConstraintCollection, diffcons, fortype
from ..checkers import DiffConstraint, DiffConstraintCollection, diffcons, typedCons
from . import add, remove

ClassConstraints = DiffConstraintCollection()
Expand All @@ -16,10 +16,9 @@


@ClassConstraints.cons
@fortype(ClassEntry)
@diffcons
@typedCons(ClassEntry)
def AddBaseClass(
a: ClassEntry, b: ClassEntry, old: "ApiDescription", new: "ApiDescription"
a: ClassEntry, b: ClassEntry, old: ApiDescription, new: ApiDescription
):
sa = set(a.bases)
sb = set(b.bases)
Expand All @@ -33,10 +32,9 @@ def AddBaseClass(


@ClassConstraints.cons
@fortype(ClassEntry)
@diffcons
@typedCons(ClassEntry)
def RemoveBaseClass(
a: ClassEntry, b: ClassEntry, old: "ApiDescription", new: "ApiDescription"
a: ClassEntry, b: ClassEntry, old: ApiDescription, new: ApiDescription
):
sa = set(a.bases)
sb = set(b.bases)
Expand All @@ -50,10 +48,9 @@ def RemoveBaseClass(


@ClassConstraints.cons
@fortype(ClassEntry)
@diffcons
@typedCons(ClassEntry)
def ImplementAbstractBaseClass(
a: ClassEntry, b: ClassEntry, old: "ApiDescription", new: "ApiDescription"
a: ClassEntry, b: ClassEntry, old: ApiDescription, new: ApiDescription
):
sa = set(a.abcs)
sb = set(b.abcs)
Expand All @@ -69,10 +66,9 @@ def ImplementAbstractBaseClass(


@ClassConstraints.cons
@fortype(ClassEntry)
@diffcons
@typedCons(ClassEntry)
def DeimplementAbstractBaseClass(
a: ClassEntry, b: ClassEntry, old: "ApiDescription", new: "ApiDescription"
a: ClassEntry, b: ClassEntry, old: ApiDescription, new: ApiDescription
):
sa = set(a.abcs)
sb = set(b.abcs)
Expand All @@ -88,10 +84,9 @@ def DeimplementAbstractBaseClass(


@ClassConstraints.cons
@fortype(ClassEntry)
@diffcons
@typedCons(ClassEntry)
def ChangeMethodResolutionOrder(
a: ClassEntry, b: ClassEntry, old: "ApiDescription", new: "ApiDescription"
a: ClassEntry, b: ClassEntry, old: ApiDescription, new: ApiDescription
):
sa = a.mro
sb = a.mro
Expand Down
8 changes: 3 additions & 5 deletions core/aexpy/diffing/differs/contraints/externals.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@
from aexpy.models.description import ApiEntry, SpecialEntry, SpecialKind
from aexpy.models.difference import DiffEntry

from ..checkers import DiffConstraint, DiffConstraintCollection, diffcons, fortype
from ..checkers import DiffConstraint, DiffConstraintCollection, typedConsOp

ExternalConstraints = DiffConstraintCollection()


@ExternalConstraints.cons
@fortype(SpecialEntry, True)
@diffcons
@typedConsOp(SpecialEntry)
def AddExternal(
a: SpecialEntry | None,
b: SpecialEntry | None,
Expand All @@ -23,8 +22,7 @@ def AddExternal(


@ExternalConstraints.cons
@fortype(SpecialEntry, True)
@diffcons
@typedConsOp(SpecialEntry)
def RemoveExternal(
a: SpecialEntry | None,
b: SpecialEntry | None,
Expand Down
12 changes: 5 additions & 7 deletions core/aexpy/diffing/differs/contraints/parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
)
from aexpy.models.difference import DiffEntry

from ..checkers import DiffConstraint, DiffConstraintCollection, diffcons, fortype
from ..checkers import DiffConstraint, DiffConstraintCollection, diffcons, typedCons

ParameterConstraints = DiffConstraintCollection()

Expand Down Expand Up @@ -68,13 +68,12 @@ def inner() -> "Iterator[tuple[Parameter | None, Parameter | None]]":


def changeParameter(
checker: "Callable[[Parameter | None, Parameter | None, FunctionEntry, FunctionEntry], list[DiffEntry]]",
checker: Callable[[Parameter | None, Parameter | None, FunctionEntry, FunctionEntry], list[DiffEntry]],
):
@fortype(FunctionEntry)
@diffcons
@typedCons(FunctionEntry)
@functools.wraps(checker)
def wrapper(
a: FunctionEntry, b: FunctionEntry, old: "ApiDescription", new: "ApiDescription"
a: FunctionEntry, b: FunctionEntry, old: ApiDescription, new: ApiDescription
):
results: "list[tuple[Parameter | None, Parameter | None, list[DiffEntry]]]" = []
for x, y in matchParameters(a, b):
Expand Down Expand Up @@ -162,8 +161,7 @@ def ChangeParameterDefault(


@ParameterConstraints.cons
@fortype(FunctionEntry)
@diffcons
@typedCons(FunctionEntry)
def MoveParameter(
a: FunctionEntry, b: FunctionEntry, old: "ApiDescription", new: "ApiDescription"
):
Expand Down
8 changes: 3 additions & 5 deletions core/aexpy/diffing/differs/contraints/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@
from aexpy.models.difference import DiffEntry
from aexpy.models.typing import AnyType

from ..checkers import DiffConstraint, DiffConstraintCollection, diffcons, fortype
from ..checkers import DiffConstraint, DiffConstraintCollection, diffcons, typedCons
from .parameters import changeParameter

TypeConstraints = DiffConstraintCollection()


@TypeConstraints.cons
@fortype(AttributeEntry)
@diffcons
@typedCons(AttributeEntry)
def ChangeAttributeType(
a: AttributeEntry, b: AttributeEntry, old: "ApiDescription", new: "ApiDescription"
):
Expand All @@ -30,8 +29,7 @@ def ChangeAttributeType(


@TypeConstraints.cons
@fortype(FunctionEntry)
@diffcons
@typedCons(FunctionEntry)
def ChangeReturnType(
a: FunctionEntry, b: FunctionEntry, old: "ApiDescription", new: "ApiDescription"
):
Expand Down
2 changes: 1 addition & 1 deletion core/aexpy/extracting/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@


class Extractor(Producer):
def extract(self, dist: "Distribution", product: "ApiDescription"):
def extract(self, dist: Distribution, product: ApiDescription):
"""Extract an API description from a distribution."""
pass
26 changes: 14 additions & 12 deletions core/aexpy/extracting/default.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
class DefaultExtractor(Extractor):
"""Basic extractor that uses dynamic inspect."""

def base(self, dist: "Distribution", product: "ApiDescription"):
def base(self, dist: Distribution, product: ApiDescription):
assert dist
subres = subprocess.run(
[getPythonExe(), "-m", "aexpy.extracting.main.base"],
Expand All @@ -39,9 +39,9 @@ def base(self, dist: "Distribution", product: "ApiDescription"):

def attributes(
self,
dist: "Distribution",
product: "ApiDescription",
server: "PackageMypyServer | None",
dist: Distribution,
product: ApiDescription,
server: PackageMypyServer | None,
):
from .enriching import attributes

Expand All @@ -53,7 +53,7 @@ def attributes(
else:
attributes.InstanceAttributeAstEnricher(self.logger).enrich(product)

def enrichCallgraph(self, product: "ApiDescription", cg: "Callgraph"):
def enrichCallgraph(self, product: ApiDescription, cg: Callgraph):
callees: "dict[str, set[str]]" = {}

for caller in cg.items.values():
Expand All @@ -73,9 +73,9 @@ def enrichCallgraph(self, product: "ApiDescription", cg: "Callgraph"):

def kwargs(
self,
dist: "Distribution",
product: "ApiDescription",
server: "PackageMypyServer | None",
dist: Distribution,
product: ApiDescription,
server: PackageMypyServer | None,
):
from .enriching import kwargs

Expand All @@ -96,9 +96,9 @@ def kwargs(

def types(
self,
dist: "Distribution",
product: "ApiDescription",
server: "PackageMypyServer | None",
dist: Distribution,
product: ApiDescription,
server: PackageMypyServer | None,
):
from .enriching import types

Expand All @@ -107,9 +107,11 @@ def types(
if server:
types.TypeEnricher(server, self.logger).enrich(product)

def extract(self, dist: "Distribution", product: "ApiDescription"):
def extract(self, dist: Distribution, product: ApiDescription):
self.base(dist, product)

assert dist.rootPath

try:
server = PackageMypyServer(dist.rootPath, dist.src, self.logger)
server.prepare()
Expand Down
Loading

0 comments on commit 6dd88a7

Please sign in to comment.