Giter Site home page Giter Site logo

Comments (4)

jakkdl avatar jakkdl commented on August 21, 2024 1

I'm not a fan of this parameter, in large part because the end goal is to move RaisesGroup into upstream pytest, and it's hard for me to make a strong case for adding it there. It also brings up a bunch of follow-up questions: why should RaisesGroup have this functionality, but not pytest.raises? Should it be possible to unset .must_reach_this_point? Can one require setting it multiple times? Or what about multiple different .must_reach_this_points?

I could even see pytest newbies creating really broad with RaisesGroup blocks, or even going as far as manually raising ExceptionGroups just to use the must-reach-this-point functionality.

I think making sure a specific line of code runs is a super common problem that comes up in tons of other situations, not just when we expect ExceptionGroups to be raised. So adding custom logic for this one specific case needs a strong reason imo - which I'm not sure this scenario when there are fairly painless ways of achieving it in other ways.

Another option for rewriting it would be to unroll the async with statement:

my_cm = some_async_context_manager()
await my_cm.__aenter__()
...
with RaisesGroup(...):
    await my_cm.__aexit__()

(which would make PT012 very happy), making it very explicit that you're only testing the exit. though ofc it is somewhat clunky.

Yet another option, keep your AssertRaises - but make it a subclass of RaisesGroup, delegating all the exception logic to it and only adding on your .must_reach_this_point(). Something like

class AssertRaises(trio.testing.RaisesGroup):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.has_reached=False

    def must_reach_this_point(self):
        self.has_reached=True

    def __exit__(self, *args):
        assert self.has_reached
        super().__exit__(*args)

from trio.

Zac-HD avatar Zac-HD commented on August 21, 2024

Fair enough, the subclass trick does seem like a better solution than upstreaming everything.

from trio.

A5rocks avatar A5rocks commented on August 21, 2024

Alternatively, I think it would be possible to just have another context manager (not a subclass) and use that! You could probably even then use a pytest fixture and allow it for non-exception group tests. To be specific, something like this (not sure if this works, just copying from the subclass above + edits):

class GetsTo():
    def __init__(self):
        self.has_reached=False

    def must_reach_this_point(self):
        self.has_reached=True

    # some definition of __enter__, I think this is the right definition?:
    def __enter__(self, *args):
        return None

    def __exit__(self, *args):
        assert self.has_reached

from trio.

jakkdl avatar jakkdl commented on August 21, 2024

Hm, I considered that one as well but thought it wouldn't be able to perfectly replicate the behaviour with printing the stacktrace if the point wasn't reached. But thinking about it again now I don't see why it wouldn't work.
Just gotta make sure to place the GetsTo() CM inside the RaisesGroup(), and to always return False (unless the assert fails) in __exit__ so as not to suppress the expected exception[group].

from trio.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.