This repository has been archived by the owner on Dec 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Core: Add reference
#69
Merged
mawildoer
merged 14 commits into
mawildoer/reduced-trait-verbosity
from
mawildoer/magic_pointers
Sep 23, 2024
Merged
Changes from 7 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
c2a2d44
Core: Add megic_pointer to generate convenient references
mawildoer 41eb50f
Library: Move Reference to library and add some polish
mawildoer 35f809d
Library: Add has_reference trait
mawildoer 4fbc245
Get the references passing pre-commit
mawildoer a0df83b
Test: refactor test_basic.py to work as a pytest properly
mawildoer 25e5194
Library: Move reference back to core. Apparently library doesn't like it
mawildoer 70c10d4
Test: Refactor test_basic with clear criteria
mawildoer abf0c0e
Improved basic test; use type_pair
iopapamanoglou 65cb0e3
graphinterface connect tying
iopapamanoglou b612e86
Address incidious underlying properties
mawildoer 548ea85
Add check to ensure properties aren't instantiated as instance attrib…
mawildoer 4a2bc25
Reinstate better errors on field construction
mawildoer 7c559bd
Add util to check debugging
mawildoer b7ba675
pre-commit
mawildoer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,71 @@ | ||
from collections import defaultdict | ||
|
||
from faebryk.core.graphinterface import GraphInterfaceReference | ||
from faebryk.core.link import LinkPointer | ||
from faebryk.core.node import Node, constructed_field | ||
|
||
|
||
class Reference[O: Node](constructed_field): | ||
""" | ||
Create a simple reference to other nodes that are properly encoded in the graph. | ||
""" | ||
|
||
class UnboundError(Exception): | ||
"""Cannot resolve unbound reference""" | ||
|
||
def __init__(self, out_type: type[O] | None = None): | ||
self.gifs: dict[Node, GraphInterfaceReference] = defaultdict( | ||
GraphInterfaceReference | ||
) | ||
self.points_to: dict[Node, O] = {} | ||
|
||
def get(instance: Node) -> O: | ||
if instance not in self.gifs: | ||
raise Reference.UnboundError | ||
|
||
my_gif = self.gifs[instance] | ||
|
||
try: | ||
return my_gif.get_reference() | ||
except GraphInterfaceReference.UnboundError as ex: | ||
raise Reference.UnboundError from ex | ||
|
||
def set(instance: Node, value: O): | ||
if instance in self.points_to: | ||
# TypeError is also raised when attempting to assign | ||
# to an immutable (eg. tuple) | ||
raise TypeError( | ||
f"{self.__class__.__name__} already set and are immutable" | ||
) | ||
|
||
if out_type is not None and not isinstance(value, out_type): | ||
raise TypeError(f"Expected {out_type} got {type(value)}") | ||
|
||
self.points_to[instance] = value | ||
|
||
# if we've already been graph-constructed | ||
# then immediately attach our gif to what we're referring to | ||
# if not, this is done in the construction | ||
if instance._init: | ||
self.gifs[instance].connect(value.self_gif, LinkPointer) | ||
|
||
property.__init__(self, get, set) | ||
|
||
def __construct__(self, obj: Node) -> None: | ||
gif = obj.add(self.gifs[obj]) | ||
|
||
# if what we're referring to is set, then immediately also connect the link | ||
if points_to := self.points_to.get(obj): | ||
gif.connect(points_to.self_gif, LinkPointer) | ||
|
||
# don't attach anything additional to the Node during field setup | ||
return None | ||
|
||
|
||
def reference[O: Node](out_type: type[O] | None = None) -> O | Reference: | ||
""" | ||
Create a simple reference to other nodes properly encoded in the graph. | ||
|
||
This final wrapper is primarily to fudge the typing. | ||
""" | ||
return Reference(out_type=out_type) |
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,15 @@ | ||
from faebryk.core.node import Node | ||
from faebryk.core.reference import Reference | ||
from faebryk.core.trait import Trait | ||
|
||
|
||
class has_reference[T: Node](Trait.decless()): | ||
"""Trait-attached reference""" | ||
|
||
reference: T = Reference() | ||
|
||
def __init__(self, reference: T): | ||
super().__init__() | ||
self.reference = reference | ||
|
||
# TODO: extend this class with support for other trait-types |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this belong in
Node
?