-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* create custom dataclass decorator * fix tests * add typing extension * ignore overloads from covarage
- Loading branch information
Showing
7 changed files
with
326 additions
and
235 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
Large diffs are not rendered by default.
Oops, something went wrong.
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 |
---|---|---|
|
@@ -5,12 +5,13 @@ description = "" | |
authors = ["Cristian Garcia <[email protected]>"] | ||
license = "MIT" | ||
readme = "README.md" | ||
packages = [{include = "simple_pytree"}] | ||
packages = [{ include = "simple_pytree" }] | ||
|
||
[tool.poetry.dependencies] | ||
python = ">=3.8,<3.12" | ||
jax = "*" | ||
jaxlib = "*" | ||
typing-extensions = "*" | ||
|
||
|
||
[tool.poetry.group.dev.dependencies] | ||
|
@@ -24,3 +25,6 @@ flax = "*" | |
[build-system] | ||
requires = ["poetry-core"] | ||
build-backend = "poetry.core.masonry.api" | ||
|
||
[tool.coverage.report] | ||
exclude_lines = ["@tp.overload"] |
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,5 +1,6 @@ | ||
__version__ = "0.1.7" | ||
|
||
from .pytree import Pytree, field, static_field | ||
from .dataclass import dataclass, field, static_field | ||
from .pytree import Pytree, PytreeMeta | ||
|
||
__all__ = ["Pytree", "field", "static_field"] | ||
__all__ = ["Pytree", "PytreeMeta", "dataclass", "field", "static_field"] |
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,103 @@ | ||
import dataclasses | ||
import typing as tp | ||
|
||
import typing_extensions as tpe | ||
|
||
A = tp.TypeVar("A") | ||
|
||
|
||
def field( | ||
*, | ||
default: tp.Any = dataclasses.MISSING, | ||
pytree_node: bool = True, | ||
default_factory: tp.Any = dataclasses.MISSING, | ||
init: bool = True, | ||
repr: bool = True, | ||
hash: tp.Optional[bool] = None, | ||
compare: bool = True, | ||
metadata: tp.Optional[tp.Mapping[str, tp.Any]] = None, | ||
): | ||
if metadata is None: | ||
metadata = {} | ||
else: | ||
metadata = dict(metadata) | ||
|
||
if "pytree_node" in metadata: | ||
raise ValueError("'pytree_node' found in metadata") | ||
|
||
metadata["pytree_node"] = pytree_node | ||
|
||
return dataclasses.field( # type: ignore | ||
default=default, | ||
default_factory=default_factory, | ||
init=init, | ||
repr=repr, | ||
hash=hash, | ||
compare=compare, | ||
metadata=metadata, | ||
) | ||
|
||
|
||
def static_field( | ||
*, | ||
default: tp.Any = dataclasses.MISSING, | ||
default_factory: tp.Any = dataclasses.MISSING, | ||
init: bool = True, | ||
repr: bool = True, | ||
hash: tp.Optional[bool] = None, | ||
compare: bool = True, | ||
metadata: tp.Optional[tp.Mapping[str, tp.Any]] = None, | ||
): | ||
return field( | ||
default=default, | ||
pytree_node=False, | ||
default_factory=default_factory, | ||
init=init, | ||
repr=repr, | ||
hash=hash, | ||
compare=compare, | ||
metadata=metadata, | ||
) | ||
|
||
|
||
@tp.overload | ||
def dataclass(cls: tp.Type[A]) -> tp.Type[A]: | ||
... | ||
|
||
|
||
@tp.overload | ||
def dataclass( | ||
*, | ||
init: bool = True, | ||
repr: bool = True, | ||
eq: bool = True, | ||
order: bool = False, | ||
unsafe_hash: bool = False, | ||
frozen: bool = False, | ||
) -> tp.Callable[[tp.Type[A]], tp.Type[A]]: | ||
... | ||
|
||
|
||
@tpe.dataclass_transform(field_specifiers=(field, static_field, dataclasses.field)) | ||
def dataclass( | ||
cls: tp.Optional[tp.Type[A]] = None, | ||
init: bool = True, | ||
repr: bool = True, | ||
eq: bool = True, | ||
order: bool = False, | ||
unsafe_hash: bool = False, | ||
frozen: bool = False, | ||
) -> tp.Union[tp.Type[A], tp.Callable[[tp.Type[A]], tp.Type[A]]]: | ||
decorator = dataclasses.dataclass( | ||
init=init, | ||
repr=repr, | ||
eq=eq, | ||
order=order, | ||
unsafe_hash=unsafe_hash, | ||
frozen=frozen, | ||
) | ||
|
||
if cls is None: | ||
return decorator | ||
|
||
return decorator(cls) |
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