-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Organize
nnbench.types.types
into different files
such that the file is less long. The files aim to group related types into files with fitting names. Also refactored imports around the projects and in tests accordingly.
- Loading branch information
Showing
7 changed files
with
187 additions
and
175 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
from .types import Benchmark, BenchmarkRecord, Memo, Parameters, State | ||
from .benchmark import Benchmark, BenchmarkRecord, Parameters, State | ||
from .memo import Memo |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
"""Type interface for the function interface""" | ||
|
||
from __future__ import annotations | ||
|
||
import inspect | ||
from dataclasses import dataclass | ||
from typing import Any, Callable, TypeVar | ||
|
||
T = TypeVar("T") | ||
Variable = tuple[str, type, Any] | ||
|
||
|
||
@dataclass(frozen=True) | ||
class Interface: | ||
""" | ||
Data model representing a function's interface. An instance of this class | ||
is created using the `from_callable` class method. | ||
Parameters: | ||
---------- | ||
names : tuple[str, ...] | ||
Names of the function parameters. | ||
types : tuple[type, ...] | ||
Types of the function parameters. | ||
defaults : tuple | ||
A tuple of the function parameters' default values. | ||
variables : tuple[Variable, ...] | ||
A tuple of tuples, where each inner tuple contains the parameter name and type. | ||
returntype: type | ||
The function's return type annotation, or NoneType if left untyped. | ||
""" | ||
|
||
names: tuple[str, ...] | ||
types: tuple[type, ...] | ||
defaults: tuple | ||
variables: tuple[Variable, ...] | ||
returntype: type | ||
|
||
@classmethod | ||
def from_callable(cls, fn: Callable, defaults: dict[str, Any]) -> Interface: | ||
""" | ||
Creates an interface instance from the given callable. | ||
""" | ||
# Set `follow_wrapped=False` to get the partially filled interfaces. | ||
# Otherwise we get missing value errors for parameters supplied in benchmark decorators. | ||
sig = inspect.signature(fn, follow_wrapped=False) | ||
ret = sig.return_annotation | ||
_defaults = {k: defaults.get(k, v.default) for k, v in sig.parameters.items()} | ||
# defaults are the signature parameters, then the partial parametrization. | ||
return cls( | ||
tuple(sig.parameters.keys()), | ||
tuple(p.annotation for p in sig.parameters.values()), | ||
tuple(_defaults.values()), | ||
tuple((k, v.annotation, _defaults[k]) for k, v in sig.parameters.items()), | ||
type(ret) if ret is None else ret, | ||
) |
Oops, something went wrong.