Create Controller.walk_attributes(access_mode=) helper#138
Create Controller.walk_attributes(access_mode=) helper#138shihab-dls wants to merge 3 commits intomainfrom
Controller.walk_attributes(access_mode=) helper#138Conversation
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #138 +/- ##
==========================================
+ Coverage 90.58% 90.74% +0.15%
==========================================
Files 41 41
Lines 1933 1955 +22
==========================================
+ Hits 1751 1774 +23
+ Misses 182 181 -1 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
src/fastcs/controller.py
Outdated
There was a problem hiding this comment.
Can we use a TypeVar bound to attributes here?
| def walk_attributes(self, access_mode: type[Attribute]): | |
| def walk_attributes(self, access_mode: type[T_Attribute] = Attribute) -> dict[str, T_Attribute]: |
There was a problem hiding this comment.
What python version do we need for this syntax?
There was a problem hiding this comment.
Tested on 3.11:
class X():
x: int
class X1(X):
x1: int
class X2(X):
x2: int
class X3(X2):
x3: int
T = TypeVar('T', bound=X)
def foo(xs: dict[str, T], x_t: type[T] = X) -> dict[str, T]:
return {k: v for k, v in xs.items() if isinstance(v, x_t)}
xs = {"a": X1(), "b": X2(), "c": X3()}
for k, v in foo(xs, x_t=X3).items():
# These work
print(k, v.x)
print(k, v.x2)
print(k, v.x3)
# attribute "x1" is unknown pyright error
print(k, v.x1)
src/fastcs/controller.py
Outdated
There was a problem hiding this comment.
I think we're looking for cs_methods on the controller, e.g
FastCS/src/fastcs/cs_methods.py
Line 77 in 5aa7a12
FastCS/src/fastcs/cs_methods.py
Line 98 in 5aa7a12
FastCS/src/fastcs/cs_methods.py
Line 125 in 5aa7a12
with
def walk_methods(self, access_mode: type[Method] = Method) -> dict[str, Method]
src/fastcs/controller.py
Outdated
There was a problem hiding this comment.
Be nice for this to be an optional restriction:
| def walk_attributes(self, access_mode: type[Attribute]): | |
| return { | |
| attr: values | |
| for attr, values in self.attributes.items() | |
| if isinstance(values, access_mode) | |
| } | |
| def walk_attributes(self, access_mode: type[Attribute] = Attribute): | |
| return { | |
| name: attribute | |
| for name, attribute in self.attributes.items() | |
| if isinstance(attribute, access_mode) | |
| } |
|
@evalott100 mentioned protocols, so I've added in a protocol for |
|
Closing as we no longer need this functionality. |
Fixes #112
This PR adds a
Controllermethod to return all attributes of a given access mode, whereinAttrWandAttrRare subsets ofAttrRW. A method was also added to return methods of a given access mode (excluding dunders).