PyDev:模块的 @DynamicAttrs
在我正在编写的程序中,我创建了一个名为 settings
的模块,它声明了一些常量,但从配置文件加载其他常量,将它们放置在模块命名空间中(例如:π 的值可能是模块的代码,但配置文件中用户的权重)。
这样,在其他模块中,我可以执行以下操作:
from settings import *
一切对我来说都工作正常,但是 - 使用 Aptana Studio / PyDev,代码分析工具会抛出很多未定义的变量错误
,如下所示:
我发现 这里有一个标志可用于防止类文档字符串中的这种行为,但如果我尝试在模块级别使用它,它不会产生任何效果。所以我想知道两件事:
有没有办法有选择地消除这些错误(这意味着我不想完全关闭“将未定义变量标记为错误”选项:在其他模块中它实际上可能是一个错误)?
如果没有,是否有一种替代模式可以在野生导入方面实现我想要的目标,但又不会混淆代码分析工具?
先发制人的说明:我完全清楚不鼓励野生进口的事实。
In the program I am writing, I created a module called settings
that declares a few constants but loads other from a configuration file, placing them in the module namespace (for example: the value of π might be in the code of the module, but the weight of the user in a configuration file).
This is such that in other modules, I can do:
from settings import *
Everything works fine for me but - using Aptana Studio / PyDev, the code analysis tool throws a lot of undefined variable errors
like this:
I found here that there is a flag usable to prevent this behaviour in class docstrings, but it has no effect if I try to use it at module level. So I wonder two things:
Is there a way to selectively get rid of these errors (meaning that I wouldn't want to completely turn off the option "mark as errors the undefined variables": in other modules it could in fact be an error)?
If not, is there an alternative pattern to achieve what I want in terms of wild imports, but without confusing the code analysis tool?
Pre-emptive note: I am perfectly aware of the fact wild imports are discouraged.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
实际上,即使不是疯狂导入,您也可能会遇到相同的错误(即:导入设置/settings.MY_VARIABLE 仍会显示错误,因为代码分析找不到它)。
除了引用它的每个地方的
@UndefinedVariable
之外(CTRL+1 将显示该选项),我认为对于您来说这是一个更好的模式模块将是:这样,代码分析(以及阅读您的模块的任何人)就会知道需要哪些变量。
否则,如果您以前不知道它们,我认为更好的方法是拥有一个方法(即:
get_settings('MY_VARIABLE')
)。与实际问题无关。我真的建议不要在这里使用野生导入(甚至不导入常量...即:
from settings import MY_VARIABLE
)。设置模块的更好方法始终是使用:(
因为否则,如果任何地方决定要更改 MY_VARIABLE,则任何将引用放入其自己的命名空间的地方可能永远不会获得更改的变量)。
更安全的方法是使用方法
get_setting('var')
,因为它可以让您更好地延迟加载您的首选项(即:不在导入时加载,但在导入时加载)第一次打电话)。Actually you'd probably have the same error even if it wasn't a wild import (i.e.: import settings / settings.MY_VARIABLE would still show an error because the code-analysis can't find it).
Aside from the
@UndefinedVariable
in each place that references it (CTRL+1 will show that option), I think that a better pattern for your module would be:That way, the code-analysis (and anyone reading your module), would know which variables are expected.
Otherwise, if you don't know them before, I think a better approach would be having a method (i.e.:
get_settings('MY_VARIABLE')
).Unrelated to the actual problem. I'd really advise against using a wild import here (nor even importing the constant... i.e.:
from settings import MY_VARIABLE
).A better approach for a settings module is always using:
(because otherwise, if any place decides it wants to change the MY_VARIABLE, any place that has put the reference in its own namespace will probably never get the changed variable).
An even safer approach would be having a method
get_setting('var')
, as it would allow you to a better lazy-loading of your preferences (i.e.: don't load on import, but when it's called the 1st time).您可以在错误上使用 Ctrl-1 并选择
@UndefineVariable
或在包含您想要忽略的错误的行上键入#@UndefineVariable
。您可以尝试通过转到 Window > 来添加要由 PyDev 解释器扫描的模块。首选项,然后 PyDev >解释器——Python。在“库”选项卡下,单击
新建文件夹
并浏览到包含设置
的目录,然后单击应用
。希望 Pydev 能够找到您的包并识别大量导入的变量。You can use Ctrl-1 on an error and choose
@UndefinedVariable
or type#@UndefinedVariable
on a line that has an error you want to ignore.You can try to add your module to be scanned by the PyDev interpreter by going to Window > Preferences, then PyDev > Interpreter - Python. Under the Libraries tab, click
New Folder
and browse to the directory that containssettings
, then clickApply
. Hopefully, Pydev will find your package and recognize the wildly-imported variables.