Skip to content

Prevent an Outcome to be unwrapped twice. - #11

Merged
RazerM merged 1 commit into
python-trio:masterfrom
smurfix:master
Apr 18, 2018
Merged

RazerM merged 1 commit into
python-trio:masterfrom
smurfix:master

Conversation

@m-u-xyz

@m-u-xyz m-u-xyz commented Apr 16, 2018

Copy link
Copy Markdown
Contributor

Fix #7.

@codecov

codecov Bot commented Apr 16, 2018 •

Copy link
Copy Markdown

Codecov Report

Merging #11 into master will not change coverage.
The diff coverage is 100%.

Impacted file tree graph

@@          Coverage Diff          @@
##           master    #11   +/-   ##
=====================================
  Coverage     100%   100%           
=====================================
  Files           8      8           
  Lines         196    226   +30     
  Branches       10     12    +2     
=====================================
+ Hits          196    226   +30
Impacted Files Coverage Δ
src/outcome/_sync.py 100% <100%> (ø) ⬆️
tests/test_sync.py 100% <100%> (ø) ⬆️
src/outcome/_util.py 100% <100%> (ø) ⬆️
src/outcome/_async.py 100% <100%> (ø) ⬆️
tests/test_async.py 100% <100%> (ø) ⬆️
src/outcome/__init__.py 100% <100%> (ø) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 9df5122...3e4e94e. Read the comment docs.

@RazerM RazerM left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When I half-implemented this to try it out, I had

@attr.s(repr=False, init=False, slots=True)
class Outcome(ABC):
    _unwrapped = attr.ib(default=False, cmp=False, init=False)

    def _set_unwrapped(self):
        if self._unwrapped:
            raise AlreadyUnwrapped
        object.__setattr__(self, '_unwrapped', True)

which means

  • Value and Error can keep @attr.s(frozen=True, repr=False).
  • We don't need to define __init__ on Value and Error

Comment thread src/outcome/_async.py Outdated
Error as ErrorBase, Outcome as OutcomeBase, Value as ValueBase
)

from ._util import OutcomeUsedError

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't feel that strongly about it, but I would call it AlreadyUsedError.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good enough ;-)

Comment thread tests/test_sync.py
assert repr(v) == "Value(1)"

with pytest.raises(OutcomeUsedError):
v.unwrap()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We'll need tests that check .send() and .asend() raise OutcomeUsedError too.

@m-u-xyz

m-u-xyz commented Apr 17, 2018

Copy link
Copy Markdown
Contributor Author

Ah. I didn't know that you could split frozen and not-frozen simply by subclassing.

New commit prepared.

@RazerM RazerM left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry I should have been clearer with my earlier comments. It's looking good, just a couple more minor changes.

Comment thread src/outcome/_sync.py Outdated


@attr.s(frozen=True, repr=False)
@attr.s(repr=False, hash=True)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be @attr.s(frozen=True, repr=False, slots=True). That is, the same as before this commit but with slots added since that was also added to Outcome.

Comment thread src/outcome/_sync.py Outdated


@attr.s(frozen=True, repr=False)
@attr.s(repr=False, hash=True)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here

Comment thread tests/test_sync.py
assert e.error is exc
with pytest.raises(RuntimeError):
e.unwrap()
with pytest.raises(AlreadyUsedError):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We're still missing a test that .send() raises AlreadyUnusedError

@m-u-xyz

m-u-xyz commented Apr 17, 2018

Copy link
Copy Markdown
Contributor Author

Updated w/ requested changes.

@RazerM

RazerM commented Apr 17, 2018

Copy link
Copy Markdown
Member

Before I merge, do we need the newsfragment? I pushed some earlier version to PyPI to reserve the name, but as 0.1.0a0, so the next release will be the first release 🤔

@njsmith

njsmith commented Apr 18, 2018

Copy link
Copy Markdown
Member

Docs are rarely bad, but it doesn't sound super important either.

BTW, has anyone double-checked that these semantics won't break trio? (I mean I think they're probably a good idea, but that doesn't mean I haven't accidentally been naughty in the past :-).)

@m-u-xyz

m-u-xyz commented Apr 18, 2018

Copy link
Copy Markdown
Contributor Author

It's a change with respect to trio.hazmat.Result, thus IMHO it makes sense to keep it.

@RazerM

RazerM commented Apr 18, 2018

Copy link
Copy Markdown
Member

Good point with respect to Result

@RazerM
RazerM merged commit f916e3d into python-trio:master Apr 18, 2018
@m-u-xyz

m-u-xyz commented Apr 18, 2018

Copy link
Copy Markdown
Contributor Author

Oops … yes, Trio does fail with this in place.

python-trio/trio#505

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants