4import discord
5from discord.ext import commands
6from discord import app_commands, Member
7from discord.app_commands import AppCommandError, Command, ContextMenu, CommandInvokeError, TransformerError 8from extensions import initial_extensions
9from utils import cfg, db, logger, GIRContext, BanCache, IssueCache, Tasks, RuleCache, init_client_session, scam_cache
10from utils.framework import PermissionsFailure, gatekeeper, find_triggered_filters
4import discord
5from discord.ext import commands
6from discord import app_commands, Member
7from discord.app_commands import AppCommandError, Command, ContextMenu, CommandInvokeError, TransformerError 8from extensions import initial_extensions
9from utils import cfg, db, logger, GIRContext, BanCache, IssueCache, Tasks, RuleCache, init_client_session, scam_cache
10from utils.framework import PermissionsFailure, gatekeeper, find_triggered_filters
9from utils import cfg, db, logger, GIRContext, BanCache, IssueCache, Tasks, RuleCache, init_client_session, scam_cache
10from utils.framework import PermissionsFailure, gatekeeper, find_triggered_filters
11from cogs.commands.context_commands import setup_context_commands
12import cogs.monitors.misc.profanity as profanity_cog 13
14from typing import Union
15from data.services.user_service import user_service
6import discord
7from data.services import guild_service
8from discord.ext import commands
9from utils import cfg10
11
12class FixSocials(commands.Cog):
4import aiohttp
5
6import discord
7from data.services import guild_service 8from discord.ext import commands
9from utils import cfg
10
An object has been imported but is not used anywhere in the file. It should either be used or the import should be removed.
import os
def example():
print("This snippet is not using the `os` import anywhere.")
def example():
print("This looks good now!")
One major reason why this issue can cause confusion is when it's raised for imports that are meant to be exported, for use in other places.
For example, consider this file, mypackage/__init__.py
:
from mypackage.foo import is_foo
from mypackage.bar import bar_function
This is a very common pattern to export common functionality from modules, to
the top level of a package. But there is a major problem with this approach.
Consider this file, mypackage/foo.py
:
import os
def is_foo(item):
return os.path.exists(item)
Since os
is imported inside foo.py
, you can actually do this:
>>> from mypackage.foo import os
Although weird, Python automatically exports all imports in a file. In practice however, it is ill-advised to rely on this behaviour.
If you want to explicitly export an imported item in a file, add it to the
special variable named __all__
:
from mypackage.foo import is_foo
from mypackage.bar import bar_function
__all__ = ['is_foo', 'bar_function'] # Notice that these are strings!
DeepSource won't raise an issue if the imported item is present in __all__
.