shine-jayakumar / insta-likecom-bot

Automates likes and comments on an instagram account or tag
MIT License
136 stars 29 forks source link

Fixed #118 issue #121

Closed SamcraftSam closed 5 months ago

SamcraftSam commented 5 months ago

Recently I got this error: Traceback (most recent call last): File "/home/alex/GitRepos/insta-likecom-bot-server/ilcbot.py", line 14, in <module> from modules.insta import Insta File "/home/alex/GitRepos/insta-likecom-bot-server/modules/__init__.py", line 5, in <module> from . import insta File "/home/alex/GitRepos/insta-likecom-bot-server/modules/insta.py", line 47, in <module> from modules.locators import ( File "/home/alex/GitRepos/insta-likecom-bot-server/modules/locators.py", line 53, in <module> @dataclass ^^^^^^^^^ File "/usr/lib/python3.11/dataclasses.py", line 1230, in dataclass return wrap(cls) ^^^^^^^^^ File "/usr/lib/python3.11/dataclasses.py", line 1220, in wrap return _process_class(cls, init, repr, eq, order, unsafe_hash, ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/lib/python3.11/dataclasses.py", line 958, in _process_class cls_fields.append(_get_field(cls, name, type, kw_only)) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/lib/python3.11/dataclasses.py", line 815, in _get_field raise ValueError(f'mutable default {type(f.default)} for field ' ValueError: mutable default <class 'modules.locators.SaveLoginLocators'> for field save_login is not allowed: use default_factory

I have fixed it by adding field(default_factory=) where needed.

shine-jayakumar commented 5 months ago

@SamcraftSam thanks for the changes. I forgot about other classes apart from LoginLocators. Can you replace default_factorywith defaultex. field(default=TheClassName()). It doesn't work when default_factory is used.

SamcraftSam commented 5 months ago

Yes, sure. But on my system default_factory works without exceptions. On the other hand, if I try to launch it with default parameter, ValueError: mutable default <class 'modules.locators.SaveLoginLocators'> for field save_login is not allowed: use default_factory error occurs. I am using Python 3.11.8.

shine-jayakumar commented 5 months ago

Yes, sure. But on my system default_factory works without exceptions. On the other hand, if I try to launch it with default parameter, ValueError: mutable default <class 'modules.locators.SaveLoginLocators'> for field save_login is not allowed: use default_factory error occurs. I am using Python 3.11.8.

I am using v.3.10 but when I run with field(default=...) on v.3.11, I get the same error message you're getting.

This is because of an update in dataclass module (https://docs.python.org/3/whatsnew/3.11.html#dataclasses). Now it's checking if a value assigned to it is hashable or not, which was not the case in version < 3.11. Hence, it allowed field(default=...) or even save_login = SaveLoginLoc()

So, field(default=SaveLoginLoc()) works on version < 3.11 but field(default_factory=SaveLoginLoc())doesn't work on v < 3.11.

And, field(default_factory=SaveLoginLoc()) or field(default_factory=lambda: SaveLoginLoc()) works on v. 3.11, but if you were to do something like LoginLoc.save_login.save, it still gives an error:

    print(LoginLocators.save_login.save)
          ^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: type object 'LoginLocators' has no attribute 'save_login'

We could use__post_init__() but for that we'd have to create instances of our dataclasses before we access their variables, otherwise __post__init__() wouldn't be called Ex:

@dataclass
class Cred:
    username: str = 'user123'
    password: str = 'password'

@dataclass
class LoginInfo:
     user_id: int = 123
     cred: Cred = None

     def __post_init__(self):
        self.cred = Cred()

login_info = LoginInfo()
print(login_info.cred.password) # this will work on both versions

print(LoginInfo.cred.password) # this will not work
shine-jayakumar commented 5 months ago

Thank you for your contribution. We had to make further changes to incorporate the new default initializer (default_factory). Also some other changes to update the story related workflow.