[pytest-dev] [proposal] deprecating the pytest_namespace hook

Floris Bruynooghe flub at devork.be
Mon Sep 5 07:11:12 EDT 2016


On Fri, Aug 26, 2016 at 03:06:57PM +0200, Ronny Pfannschmidt wrote:
> On 26.08.2016 15:00, Bruno Oliveira wrote:
> > On Fri, Aug 26, 2016 at 9:54 AM Ronny Pfannschmidt
> > <opensource at ronnypfannschmidt.de
> > <mailto:opensource at ronnypfannschmidt.de>> wrote:
> > 
> >     the problem it creates is, that we need to load plugins before
> >     even parsing the config and before even having config object just
> >     in order to literally monkeypatch a module
> > 
> > You mean that just importing "pytest" you automatically load all
> > plugins? Hmmm I agree that's not very nice.

Yes and no.  I agree it is not nice that hooks get executed on import
time.  But we do need to give plugins the option of extending the
pytest namespace.  So maybe we could introduce a `pytest.init()`
method to explicitly do the execution of hooks (and hence loading of
plugins I guess?).  The `pytest.main()` call could then implicitly
call `pytest.init()` as well, which would be a no-op if it was already
initialised.

> >     the second problem it creates is, it makes the pytest namespace
> >     impossible to introspect for external tools
> > 
> > I agree that's annoying, but that's a problem in general on how dynamic
> > pytest is (IDEs don't autocomplete on fixture parameters either), so I
> > would say this is small.

If the external tools would call `pytest.init()` before introspecting
then it would work just fine.  We could even provide a pylint plugin
by default for this in that case.  That would be nice.


> >     also - import works just fine - there is no need for a dynamic
> >     namespace, people can import from concrete/correct locations
> > 
> > I see, but TBH I don't see how we could just deprecate that without
> > providing an alternative solution which won't break user code. Too many
> > plugins and code depend on that, and we would need to have a **really**
> > good reason to break them all in a future pytest version (I wouldn't
> > even dare to introduce such a deprecation until 4.0 for example).

I'm afraid I agree with Bruno here, I'd even go further and suggest we
can never remove the pytest_namespace hook.  It is just used too
widely by plugins and afaik the documentation even actively
encourages the use.  What I think we could do is clean up the order of
when the hook gets called as I vaguely suggest with the
`pytest.init()` proposal above (note that I haven't fully thought this
through yet).  This would still have to go through some deprecation
cycle obviously as I'm sure there will be some cases that will break,
but at least we'll be providing plugin authors with a clear way they
can make sure to not rely on the current funky ordering without
breaking their own public API.


Regards,
Floris


More information about the pytest-dev mailing list