Skip to content
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

imports not working #81

Closed
4 tasks done
iDutchy opened this issue Dec 13, 2020 · 7 comments
Closed
4 tasks done

imports not working #81

iDutchy opened this issue Dec 13, 2020 · 7 comments

Comments

@iDutchy
Copy link

iDutchy commented Dec 13, 2020

Summary

I was working on subclassing the jishaku command to make it work with my command system, but for some reason I am missing things in my jishaku but jishaku does still seem to work. I have tried this on both 1.20.0 and 2.0.0 and with both I have the same issue...

Reproduction steps

I'm not sure. I completely reinstalled jishaku, and I even tried loading just by bot.load_extension("jishaku") in stead of the file with my subclass.

Expected results

Having access to anything in jishaku

Actual results

Dont have access to basically everything. ( https://media.discordapp.net/attachments/381963689470984203/787497858994798602/unknown.png )

Checklist

  • I have updated discord.py and jishaku to the latest available versions and have confirmed that this issue is still present
  • I have searched the open issues for duplicates
  • I have shown the entire traceback, if possible
  • I have removed my token from display, if visible

System information

Jishaku 1.20.0 (and tried 2.0.0 as well),
discord.py 1.5.1

@Gorialis Gorialis mentioned this issue Dec 13, 2020
4 tasks
@Gorialis
Copy link
Owner

Given that there aren't any diagnostics in this issue and v1 hasn't changed since the v2 push, I can't really assume anything other than your install has become broken somehow.

The immediate thing I'd expect to fix it is just doing:

pip uninstall jishaku
pip install -U --no-deps --force-reinstall jishaku

If that fails, you will have to run through some diagnostic steps yourself:

  • Are the modules themselves importable? (e.g. does from jishaku.repl.compilation import AsyncCodeExecutor work?) If yes, then it's probably a bad reload and you should try reloading again or restarting your interpreter, otherwise
  • If you look into the jishaku directory in site-packages, can you see the submodules? (you can find out where it's installed if you don't know by doing jsk pyi jishaku) If you can, then likewise again, it's probably a bad reload and you should try reloading again or restarting your interpreter. If not, then, well, that's likely the problem.

Other potential culprits are maybe permissions issues, but regardless, this doesn't seem like anything caused by jishaku.

@iDutchy
Copy link
Author

iDutchy commented Dec 14, 2020

I have done all the above. the from x import y does seem to be working, and everything in the commands seem to be working as well (when I load default jishaku, not my subclass). like the from jishaku.repl.compilation import AsyncCodeExecutor works, but when I try to jishaku.repl.compilation.CORO_CODE = NEW_CORO_CODE, I get the AttributeError: module 'jishaku' has no attribute 'repl' error... I have reinstalled and reloaded many times already now, but nothing seems to be working.

For some reason it wont let me add a screenshot, but in the link below you can see that somehow the from jishaku import repl does work but checking with an hasattr returns False
https://media.discordapp.net/attachments/460568954968997890/787844477766991903/unknown.png

@Gorialis
Copy link
Owner

Do you have import jishaku in your custom extension file (the one you do bot.add_cog(...) in)?

I think the likely explanation for why this happens probably stems from how discord.py resolves extension submodule dependencies.

I'm guessing that because you do something along the lines of from jishaku.cog import ..., discord.py detects that jishaku.cog and its recursive submodule references need to be reloaded, and reloads them, but doesn't reload jishaku itself. Thus, the jishaku module object is left with dead references that get removed, even though the modules do themselves exist post-reload, so you get the confusing scenario where from jishaku import cog works but import jishaku.cog; jishaku.cog does not.

As an interesting aside, before the new reload logic was even added into discord.py, jishaku was mostly responsible for its own reload, and even had a dedicated command for it back in the day. A remnant of this logic is actually still present even in the current v2 code, labelled only vaguely as "panic recovery mode".

If my speculation is right, I'm not really sure what the solution to it would be. If the library was itself adjusted to reload parent modules, you'd get odd situations where stuff would get reloaded when it really shouldn't be (cogs.games reloading cogs, etc). On the other hand, I can't make jishaku.cog or such depend on jishaku because it would result in a circular import.

If you don't already have import jishaku in your extension file, and adding it fixes it, I suppose that's the best solution for now, even if it results in a seemingly redundant import.

Another potential slapstick fix would be to force a reload of the main module at load time:

import importlib

import jishaku

...

def setup(bot: commands.Bot):
    importlib.reload(jishaku)
    bot.add_cog(CustomJishaku(bot=bot))

I don't like either of these as permanent solutions, so I'll probably have to dig into how the submodule resolution works again and see if I can fool discord.py into reloading jishaku subclass extensions properly.

@iDutchy
Copy link
Author

iDutchy commented Dec 15, 2020

So I have been trying out multiple things, and what eventually seemed to be the breaking change was

import jishaku
jishaku.repl.compilation.CORO_CODE = NEW_CORO_CODE
This would not work anymore. Which is weird because it always worked even on 1.20 until someday it magically stopped working

from jishaku import repl
repl.compilation.CORO_CODE = NEW_CORO_CODE
This is the solution that fixed the issue for me.

Personally I dont see how there is any difference, but I'm also not the biggest expert at this as I often just think like "if it works, dont touch it" :)

@iDutchy
Copy link
Author

iDutchy commented Dec 15, 2020

I do have another question about subclassing commands in v2.0.0.. I know its not officially released yet but since I like to keep my things updated I wanted to look into this before the final release. Would you want me to open a separate issue for this? Or is there any place on discord where I can ask for this?

@Gorialis
Copy link
Owner

I'm still scratching my head over this problem... I can't seem to replicate it no matter what I do. I reckon it's definitely something to do with extension management, and maybe whatever behavior caused this is fixed in discord.py, or maybe there's a behavior difference between Python versions.

I'm thinking about writing some unit tests for things like this, as well as just some ones for testing subclass behavior in general, since honestly those need to be done before 2.0 has a realistic chance of releasing... maybe it'll reveal something, who knows.

@Gorialis
Copy link
Owner

Gorialis commented Apr 1, 2022

Since the Feature rewrite redoing how subclasses work, I don't think it would really be possible to reproduce this issue anymore on the latest version. If such a scenario arises, then a new issue can be made about it, but I haven't had anyone run into this problem since so I'm assuming it probably no longer applies.

@Gorialis Gorialis closed this as completed Apr 1, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants