-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
__getattr__
is not IDE friendly
#3
Comments
Not happy with the dynamic way of doing this, makes it hard to debug and trace as well. but couldn't find a better way to add other datatypes this easily. Don't know how to make it more efficient. Can you explain how type checking helps? |
I'm thinking too. If we were assured that we only have those four strategies( from typing import TYPE_CHECKING
class PyInMemStore:
def __init__():
...
self.strategies = [
StringStrategy(),
ListStrategy(),
SetStrategy(),
SortedSetStrategy(),
]
def __getattr__(self, name: str):
# body
pass
if TYPE_CHECKING:
def lpush(self, store: Dict[str, Any], key: str, value: Any) -> int: ...
def rpop(self, store: Dict[str, Any], key: str) -> Any: ... And we'll get IDE support. But what if someone wants to create and add his/her own strategy and append it to Another option is to remove class PyInMemStore:
def __init__():
...
self.string: StringStrategy = StringStrategy()
self.list: ListStrategy = ListStrategy()
self.set: SetStrategy = SetStrategy()
self.sorted: SortedSetStrategy = SortedSetStrategy() users need to know the specific strategy before accessing its method which is less flexible but everything works fine and we also have type hints defined in the definitions. We could also simplify the interface by defining those methods in the class PyInMemStore:
def __init__():
...
self.string: StringStrategy = StringStrategy()
self.list: ListStrategy = ListStrategy()
self.set: SetStrategy = SetStrategy()
self.sorted: SortedSetStrategy = SortedSetStrategy()
def lpush(self, store: Dict[str, Any], key: str, value: Any) -> int:
return self.list.lpush(store, key, value) They are all have cons and pros. What's your opinion? |
I think the best approach is your first solution, That way we can keep the code dynamic and easy to add other datatypes with their own unique features. And I'm still a newbie for type checking but could you please look at this numpy sample: They have a '.pyi' file next to the original file, which performs type checking. |
@amirsoroush
All the functions are static and right now its not more that 10-15 of them, why would it be slow? |
Sure it does. They're called "stub files". Mostly they're being used when we have extension modules written in C (or others, which don't have annotations) or for old modules written in Python 2. It's your call but I'm more into keeping the type hints along with the source code. There are other modules that use this trick like SQLAlchemy, Pytantic, etc. |
Suppose you want to use
Not a huge concern but it may make a difference in tight loops. |
Ok looks good, can you add some sample to core file and maybe on one of the datatypes? |
We could save the related strategy (if existed) to the key and value then there is no need to iterate over strategies. |
pyinmem/pyinmem/core.py
Line 47 in 6678b27
Although we simplified the implementation by delegating the unknown attributes to
strategies
, IDEs are not going to help us to detect problems or code completion.Do you have any plan to address this issue? like adding
if TYPE_CHECKING:
block or something?Another thing to note is that we're iterating over strategies two times, once in
__getattr__
and again in the innermethod
. So the option is defining those methods in thePyInMemStore
class itself.The text was updated successfully, but these errors were encountered: