Skip to content

Commit

Permalink
Small update
Browse files Browse the repository at this point in the history
  • Loading branch information
halbow committed Apr 21, 2024
1 parent 15767cc commit 21e97ff
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 5 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,7 @@ p.add()

# Limitation

This project is more of a gimmick-y pattern and experimentation around rust-like struct in Python !

- Mypy and type checkers will not be aware of the method declared on your struct as they are added at runtime.
- If you define methods on the same struct from different locations, you could end up having clash
- If you define methods on the same struct from different locations, you could end up having clash
5 changes: 4 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ name = "struuuctify"
authors = [
{ name="halbow" },
]
version = "0.0.1"
version = "0.0.2"
requires-python = ">=3.11"
readme = "README.md"
classifiers = [
Expand Down Expand Up @@ -41,6 +41,9 @@ template = '''
version = "{version}"
'''

[tool.hatch.build.targets.wheel]
packages = ["struuuctify"]

[tool.ruff]
line-length = 120
target-version = 'py312'
Expand Down
6 changes: 3 additions & 3 deletions src/struuuctify/_struuuctify.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from dataclasses import dataclass
from functools import partial
import inspect
from typing import Callable, Protocol, Any, TypeVar, dataclass_transform
from typing import Callable, Protocol, Any, TypeVar, dataclass_transform, final


class _Method(Protocol):
Expand Down Expand Up @@ -29,13 +29,13 @@ def struct(cls: type[T]) -> type[T]:
setattr(cls, "__struuuctify__", {})

setattr(cls, "__getattr__", _struct_get_attr)
return dataclass(cls)
return final(dataclass(cls))


def impl(func: _Method) -> Callable[..., Any]:
cls = func.__annotations__.get("self")
if cls is None or not inspect.isclass(cls):
raise TypeError("self attribute should be a struct")
raise TypeError("self attribute should be a struct class")

cls.__struuuctify__[func.__name__] = func
return func

0 comments on commit 21e97ff

Please sign in to comment.