Skip to content
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

Clearer error message if async context manager used synchronously #212

Merged
merged 3 commits into from
Jun 15, 2017

Conversation

Carreau
Copy link
Contributor

@Carreau Carreau commented Jun 13, 2017

Otherwise it says it does not have __enter__ which is obvious for
advanced pythonista. Still a tiny bit clearer is likely better.

I guess this might fool static type checkers, but I'm unaware of any
that would flag that.

Copy link
Member

@njsmith njsmith left a comment

Choose a reason for hiding this comment

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

two nitpicks

trio/_util.py Outdated
@@ -135,6 +136,13 @@ def __init__(self, func, args, kwds):
if sys.exc_info()[1] is not value:
raise

def __enter__(self):
raise RuntimeError("The function `{}` create an async context manager, use with `async with` and not `with`.".format(self._func_name))
Copy link
Member

Choose a reason for hiding this comment

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

"use 'async with {func_name}(...)', not 'with {func_name}(...)'".format(name=self._func_name)"

(and 80 column limit!)

trio/_util.py Outdated
def __enter__(self):
raise RuntimeError("The function `{}` create an async context manager, use with `async with` and not `with`.".format(self._func_name))

def __exit__(self):
Copy link
Member

Choose a reason for hiding this comment

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

# pragma: no-cover

@Carreau
Copy link
Contributor Author

Carreau commented Jun 13, 2017

Hum, also unawaited coroutines. I need to fix the test.

@codecov
Copy link

codecov bot commented Jun 13, 2017

Codecov Report

Merging #212 into master will increase coverage by 0.04%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #212      +/-   ##
==========================================
+ Coverage   99.01%   99.06%   +0.04%     
==========================================
  Files          59       63       +4     
  Lines        8012     8407     +395     
  Branches      569      606      +37     
==========================================
+ Hits         7933     8328     +395     
  Misses         62       62              
  Partials       17       17
Impacted Files Coverage Δ
trio/_core/tests/test_run.py 100% <100%> (ø) ⬆️
trio/_util.py 87.64% <100%> (+1.56%) ⬆️
trio/__init__.py 100% <0%> (ø) ⬆️
trio/_file_io.py 100% <0%> (ø)
trio/tests/test_path.py 100% <0%> (ø)
trio/_path.py 100% <0%> (ø)
trio/tests/test_file_io.py 100% <0%> (ø)

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 f279b7d...3f865fb. Read the comment docs.

Otherwise it says it does not have `__enter__` which is obvious for
advanced pythonista. Still a tiny bit clearer is likely better.

I guess this _might_ fool static type checkers, but I'm unaware of any
that would flag that.
@njsmith
Copy link
Member

njsmith commented Jun 13, 2017

If it does create a problem for static type checkers then it should be fixable by adding an explicit annotation to acontextmanager.

Something else we might want to think about is coming up with a more standard/comprehensive way of doing this kind of checking, like a decorator we can put on all our context managers and iterators. But that doesn't need to block this; this case is a bit special because we can give a nice error message.

Copy link
Member

@njsmith njsmith left a comment

Choose a reason for hiding this comment

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

Sorry, missed that this was ready for review.

Two tiny nitpicks but otherwise looks good.

trio/_util.py Outdated
raise RuntimeError("use 'async with {func_name}(...)', not 'with {func_name}(...)'".format(func_name=self._func_name))

def __exit__(self):
assert False, """Never called, but should be defined""" # pragma: no-cover
Copy link
Member

Choose a reason for hiding this comment

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

Should be no cover, not no-cover; it's not actually being recognized right now :-). Also better to put two spaces before the #, and to move it up one line, so:

def __exit__(self):  # pragma: no cover

with pytest.raises(RuntimeError) as excinfo:
on = _core.open_nursery()
with on as nursery:
pass # pragma: no-cover
Copy link
Member

Choose a reason for hiding this comment

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

Also needs an extra space before # and removal of the -

@njsmith
Copy link
Member

njsmith commented Jun 15, 2017

Oo, I figured out how to fix the nitpicks through github. Let's see what the CI thinks...

@njsmith
Copy link
Member

njsmith commented Jun 15, 2017

Thanks!

@Carreau
Copy link
Contributor Author

Carreau commented Jun 15, 2017

Thanks !

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.

2 participants