Skip to content

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

Closed
shreyashah opened this issue Apr 4, 2017 · 7 comments
Labels
type: question general question, might be closed after 2 weeks of inactivity

Comments

@shreyashah
Copy link

shreyashah commented Apr 4, 2017

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.

@nicoddemus
Copy link
Member

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.

@shreyashah
Copy link
Author

@nicoddemus : I just updated the body of my first comment. Please let me know, if that makes sense. Thanks.

@nicoddemus nicoddemus added the type: question general question, might be closed after 2 weeks of inactivity label Apr 4, 2017
@nicoddemus
Copy link
Member

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.

@RonnyPfannschmidt
Copy link
Member

Please note that we are working in removing the namespace hook in future

@shreyashah
Copy link
Author

oh! then what would be the alternative method to access variables in the python global namespace ?

@nicoddemus
Copy link
Member

The pytest_namespace() hook is about plugins publishing new variables in the pytest namespace.

def pytest_namespace():
    return {'my_plugin_var': MyVar(value=4)}

And then users can access it with pytest.my_plugin_var.

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 pytest.my_plugin_var = MyVar(value=4) whenever appropriate for their particular case (most common probably will be during pytest_configure hook).

@shreyashah
Copy link
Author

ok, makes sense.
thanks for the prompt reply.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: question general question, might be closed after 2 weeks of inactivity
Projects
None yet
Development

No branches or pull requests

3 participants