Skip to content

Annotated callbacks#62

Merged
ASEM000 merged 2 commits intomainfrom
AnnotatedCallbacks
May 14, 2023
Merged

Annotated callbacks#62
ASEM000 merged 2 commits intomainfrom
AnnotatedCallbacks

Conversation

@ASEM000
Copy link
Copy Markdown
Owner

@ASEM000 ASEM000 commented May 14, 2023

move callbacks to inside typing.Annotated
instead of field(...callbacks=[...]) use field_name: Annotated[type, *callbacks].
check PEP 593 and https://github.com/annotated-types/annotated-types for motivation.

import jax
import pytreeclass as pytc
# python 3.9 and above
from typing import Annotated
# python 3.8
# from typing_extensions import Annotated

class PositiveInt:
    def __call__(self,value):
        if not isinstance(value, int):
            raise TypeError("Value must be an integer")
        if value <= 0:
            raise ValueError("Value must be positive")
        return value


class Tree(pytc.TreeClass):
    in_features: Annotated[int, PositiveInt()]

tree = Tree(1)
# no error

try:
    tree = Tree(0)
except ValueError as e:
    print(e)
# Error for field=`in_features`:
# Value must be positive

try:
    tree = Tree(1.0)
except TypeError as e:
    print(e)
# Error for field=`in_features`:
# Value must be an integer

@codecov
Copy link
Copy Markdown

codecov bot commented May 14, 2023

Codecov Report

Patch coverage: 100.00% and project coverage change: -0.01 ⚠️

Comparison is base (d399e78) 98.44% compared to head (44db470) 98.44%.

Additional details and impacted files
@@            Coverage Diff             @@
##             main      #62      +/-   ##
==========================================
- Coverage   98.44%   98.44%   -0.01%     
==========================================
  Files          13       13              
  Lines        2378     2373       -5     
==========================================
- Hits         2341     2336       -5     
  Misses         37       37              
Impacted Files Coverage Δ
pytreeclass/__init__.py 100.00% <100.00%> (ø)
pytreeclass/_src/code_build.py 100.00% <100.00%> (ø)
tests/test_treeclass.py 99.07% <100.00%> (-0.02%) ⬇️

☔ View full report in Codecov by Sentry.
📢 Do you have feedback about the report comment? Let us know in this issue.

@ASEM000 ASEM000 merged commit 30a6fba into main May 14, 2023
@ASEM000 ASEM000 deleted the AnnotatedCallbacks branch May 14, 2023 00:55
ASEM000 added a commit that referenced this pull request May 14, 2023
This reverts commit 30a6fba, reversing
changes made to d399e78.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant