-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Is there a way to access the pytest global namespace variables in any module without importing pytest to avoid circular dependency? #2346
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
Comments
Not sure, can you provide a specific example of what you are trying to accomplish? It is hard to think of a situation where you need things from the pytest namespace without having imported pytest itself. |
@nicoddemus : I just updated the body of my first comment. Please let me know, if that makes sense. Thanks. |
The usual way to deal with import dependencies (apart breaking the dependency itself) is to delay your imports as late as possible. You could import pytest in your custom class method locally: def my_custom_method(self):
import pytest
if pytest.script_name:
... If you can provide a reproducible example with your exact problem we might be able to help more precisely. |
Please note that we are working in removing the namespace hook in future |
oh! then what would be the alternative method to access variables in the python global namespace ? |
The def pytest_namespace():
return {'my_plugin_var': MyVar(value=4)} And then users can access it with Unfortunately this hook introduces all kinds of initialization issues (#2280), so we plan to eventually (read: years and years down the road) remove it. Plugin authors can after all just execute |
ok, makes sense. |
I have implemented the pytest_namespace() hook with some key value pairs.
one such pair is the script_name or module_name whose value i update using config.option.file_or_dir
I need to access this variable in one of my other module. The way i know to access this is "pytest.script_name". But for this, I need to do "import pytest" which is creating a circular dependency.
Is there any other way of access these global namespace variables or more specifically, a way to access this config.option.file_or_dir value in my custom class method (Note, this custom class is not a class in any plugin). It could be for example my logger module.
Any help is appreciated. Thanks.
The text was updated successfully, but these errors were encountered: