[pytest-dev] fixtures and pylint W0621

lahwran lahwran at lahwran.net
Wed Dec 4 16:05:15 CET 2013


perhaps pytest.fixture could return a special object that rejects all
operations, rather than the decorated function object; such that `theobject
== 5` would produce a big flashy error in theobject.__eq__ indicating you
need to actually use the fixture for this to work, maybe even with an
example. Repeat for all magic methods that make sense. this object would
maybe have a _pytest_fixture attribute, or conform to whatever protocol
already exists for detecting fixture objects, such that the only thing you
could do with such an object is check if it's a fixture and get the
original function object from somewhere inside it. Any other operation
would result in a flashy error.


On Tue, Dec 3, 2013 at 11:26 PM, holger krekel <holger at merlinux.eu> wrote:

> On Wed, Dec 04, 2013 at 13:29 +1100, Brianna Laugher wrote:
> > On 3 December 2013 18:39, holger krekel <holger at merlinux.eu> wrote:
> > > Right, it seems that when we introduced @pytest.fixture we decided you
> can
> > > either use the prefix or the marker.  That could be lifted but i wonder
> > > if we should rather go for a different convention because
> pytest_funcarg__
> > > is not a beautiful prefix.  What do you think of pytest stripping the
> "__" prefix?
> > >
> > >     @pytest.fixture
> > >     def __foo2(monkeypatch):
> > >        return monkeypatch
> > >
> > > This fixture would become accessible via the "foo2" name.  Using
> "__foo2"
> > > would yield a lookup error and the error would indicate there is a
> "foo2"
> > > available.  If you don't like it, any other suggestions?
> >
> > Hmm. My main concern here would be that fixture definitions are easily
> > 'findable' in a global search. The fixture decorator is easy to search
> > for, as is 'pytest_funcarg__'. Such an ugly prefix is also far easier
> > to google. Double underscore, not so much. The ugly prefix is also
> > good for backwards compat.
> >
> > So, I don't really mind what happens as long as each fixture has at
> > least one of fixture decorator/ugly prefix.
>
> Well, ok.  Pending further input, i made pytest accept pytest.fixture
> decorated pytest_funcarg__ prefixed functions, see
>
> https://bitbucket.org/hpk42/pytest/commits/aa1f0505a3156b9feca43cd67c5afc95622b9ac5
>
> I am a bit unhappy because apart from the pylint warning it's also a
> real issue for beginners to write something like this::
>
>     @pytest.fixture
>     def somename():
>         return 42
>
>     def test_something():
>         assert somename == 42
>
> The "somename" is accessible (as a global) but is a function object.
> The resulting error message is irritating if you are not proficient in
> python.
> To prevent this irritation we would need to recommend a different
> default way of declaring the fixture and neither pytest_funcarg__NAME nor
> __NAME sound like we would like to suggest this as the main way in the
> docs.
>
> Then again, my general recommendation is to put fixture functions into
> conftest.py files and you then get a clean "NameError" in a test module
> for the above example.  The docs use the "everything contained in test
> module"
> method, though, to make it easier to present getting-started examples.
> Maybe it makes sense to mention this recommended conftest/test module
> split earlier on.
>
> best,
> holger
> _______________________________________________
> Pytest-dev mailing list
> Pytest-dev at python.org
> https://mail.python.org/mailman/listinfo/pytest-dev
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/pytest-dev/attachments/20131204/05617af7/attachment.html>


More information about the Pytest-dev mailing list