Skip to content

Commit 9851f74

Browse files
authored
Fix doctests in dependencygraph.py (nltk#3048)
* Fix doctests in nltk/parse/dependencygraph.py * Move pytest import inside setup_dot() function * Generic fixture to skip doctest when a required binary is not available * Import generic fixture in dependencygraph.py and hunpos.py * Return False when binary is unavailable * Implement suggestions by @tomaahsen * Replace redundant text fixtures by check_binary * Broaden fixture filename * Delete old fixture file
1 parent 2e9cf65 commit 9851f74

File tree

8 files changed

+48
-59
lines changed

8 files changed

+48
-59
lines changed

nltk/parse/dependencygraph.py

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
from itertools import chain
2121
from pprint import pformat
2222

23+
from nltk.internals import find_binary
2324
from nltk.tree import Tree
2425

2526
#################################################################
@@ -185,7 +186,8 @@ def to_dot(self):
185186

186187
def _repr_svg_(self):
187188
"""Show SVG representation of the transducer (IPython magic).
188-
189+
>>> from nltk.test.setup_fixt import check_binary
190+
>>> check_binary('dot')
189191
>>> dg = DependencyGraph(
190192
... 'John N 2\\n'
191193
... 'loves V 0\\n'
@@ -459,7 +461,7 @@ def contains_cycle(self):
459461
>>> cyclic_dg.root = top
460462
461463
>>> cyclic_dg.contains_cycle()
462-
[3, 1, 2, 4]
464+
[1, 2, 4, 3]
463465
464466
"""
465467
distances = {}
@@ -550,30 +552,36 @@ def dot2img(dot_string, t="svg"):
550552
Create image representation fom dot_string, using the 'dot' program
551553
from the Graphviz package.
552554
553-
Use the 't' argument to specify the image file format, for ex.
554-
'png' or 'jpeg' (Running 'dot -T:' lists all available formats).
555+
Use the 't' argument to specify the image file format, for ex. 'jpeg', 'eps',
556+
'json', 'png' or 'webp' (Running 'dot -T:' lists all available formats).
555557
556-
sys.stdout is used instead of subprocess.PIPE, to avoid decoding errors
558+
Note that the "capture_output" option of subprocess.run() is only available
559+
with text formats (like svg), but not with binary image formats (like png).
557560
"""
558-
from sys import stderr, stdout
559561

560562
try:
561-
proc = subprocess.run(
562-
["dot", "-T%s" % t],
563-
input=dot_string,
564-
stdout=stdout,
565-
stderr=stderr,
566-
text=True,
567-
)
563+
find_binary("dot")
564+
try:
565+
if t in ["dot", "dot_json", "json", "svg"]:
566+
proc = subprocess.run(
567+
["dot", "-T%s" % t],
568+
capture_output=True,
569+
input=dot_string,
570+
text=True,
571+
)
572+
else:
573+
proc = subprocess.run(
574+
["dot", "-T%s" % t],
575+
input=bytes(dot_string, encoding="utf8"),
576+
)
577+
return proc.stdout
578+
except:
579+
raise Exception(
580+
"Cannot create image representation by running dot from string: {}"
581+
"".format(dot_string)
582+
)
568583
except OSError as e:
569584
raise Exception("Cannot find the dot binary from Graphviz package") from e
570-
out, err = proc.stdout, proc.stderr
571-
if err:
572-
raise Exception(
573-
"Cannot create image representation by running dot from string: {}"
574-
"".format(dot_string)
575-
)
576-
return out
577585

578586

579587
class DependencyGraphError(Exception):

nltk/tag/hunpos.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,12 @@ class HunposTagger(TaggerI):
3030
- (optionally) the path to the hunpos-tag binary
3131
- (optionally) the encoding of the training data (default: ISO-8859-1)
3232
33-
Example:
33+
Check whether the required "hunpos-tag" binary is available:
34+
35+
>>> from nltk.test.setup_fixt import check_binary
36+
>>> check_binary('hunpos-tag')
3437
38+
Example:
3539
>>> from nltk.tag import HunposTagger
3640
>>> ht = HunposTagger('en_wsj.model')
3741
>>> ht.tag('What is the airspeed of an unladen swallow ?'.split())

nltk/test/discourse_fixt.py

Lines changed: 0 additions & 12 deletions
This file was deleted.

nltk/test/inference.doctest

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
Logical Inference and Model Building
66
====================================
77

8-
>>> from nltk.test.inference_fixt import setup_module
9-
>>> setup_module()
8+
>>> from nltk.test.setup_fixt import check_binary
9+
>>> check_binary('mace4')
1010

1111
>>> from nltk import *
1212
>>> from nltk.sem.drt import DrtParser

nltk/test/inference_fixt.py

Lines changed: 0 additions & 10 deletions
This file was deleted.

nltk/test/nonmonotonic.doctest

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
Nonmonotonic Reasoning
66
======================
77

8-
>>> from nltk.test.nonmonotonic_fixt import setup_module
9-
>>> setup_module()
8+
>>> from nltk.test.setup_fixt import check_binary
9+
>>> check_binary('mace4')
1010

1111
>>> from nltk import *
1212
>>> from nltk.inference.nonmonotonic import *

nltk/test/nonmonotonic_fixt.py

Lines changed: 0 additions & 12 deletions
This file was deleted.

nltk/test/setup_fixt.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Skip doctest when a required binary is not available
2+
from nltk.internals import find_binary
3+
4+
5+
def check_binary(binary):
6+
import pytest
7+
8+
try:
9+
find_binary(binary)
10+
except LookupError:
11+
pytest.skip(f"Skipping test because the {binary} binary was not found")

0 commit comments

Comments
 (0)