Skip to content

Commit 8726431

Browse files
Skip UnsupportedFeature's on the command line (#1475)
* Add "name" parameter to UnsupportedFeature exception * Skip UnsupportedFeature's on the command line Resolves failure when Pillow isn't installed (#1474)
1 parent 22017a4 commit 8726431

File tree

4 files changed

+17
-5
lines changed

4 files changed

+17
-5
lines changed

faker/cli.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from pathlib import Path
88
from typing import Any, Dict, List, Optional, TextIO
99

10-
from faker import VERSION, Faker, documentor
10+
from faker import VERSION, Faker, documentor, exceptions
1111
from faker.config import AVAILABLE_LOCALES, DEFAULT_LOCALE, META_PROVIDERS_MODULES
1212

1313
__author__ = 'joke2k'
@@ -86,8 +86,15 @@ def print_doc(provider_or_field=None,
8686

8787
else:
8888
doc = documentor.Documentor(fake)
89+
unsupported = []
8990

90-
formatters = doc.get_formatters(with_args=True, with_defaults=True)
91+
while True:
92+
try:
93+
formatters = doc.get_formatters(with_args=True, with_defaults=True, excludes=unsupported)
94+
except exceptions.UnsupportedFeature as e:
95+
unsupported.append(e.name)
96+
else:
97+
break
9198

9299
for provider, fakers in formatters:
93100

@@ -104,7 +111,7 @@ def print_doc(provider_or_field=None,
104111

105112
for p, fs in d.get_formatters(with_args=True, with_defaults=True,
106113
locale=language,
107-
excludes=base_provider_formatters):
114+
excludes=base_provider_formatters + unsupported):
108115
print_provider(d, p, fs, output=output)
109116

110117

faker/exceptions.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,6 @@ class UniquenessException(BaseFakerException):
1010

1111
class UnsupportedFeature(BaseFakerException):
1212
"""The requested feature is not available on this system."""
13+
def __init__(self, msg, name):
14+
self.name = name
15+
super().__init__(msg)

faker/providers/misc/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ def image(self, size=(256, 256), image_format='png', hue=None, luminosity=None):
305305
import PIL.Image
306306
import PIL.ImageDraw
307307
except ImportError:
308-
raise UnsupportedFeature("`image` requires the `Pillow` python library.")
308+
raise UnsupportedFeature("`image` requires the `Pillow` python library.", "image")
309309

310310
(width, height) = size
311311
image = PIL.Image.new('RGB', size, self.generator.color(hue=hue, luminosity=luminosity))

tests/providers/test_misc.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -326,9 +326,11 @@ def test_image(self, faker):
326326

327327
def test_image_no_pillow(self, faker):
328328
with patch.dict("sys.modules", {"PIL": None}):
329-
with pytest.raises(exceptions.UnsupportedFeature):
329+
with pytest.raises(exceptions.UnsupportedFeature) as excinfo:
330330
faker.image()
331331

332+
assert excinfo.value.name == "image"
333+
332334
def test_dsv_with_invalid_values(self, faker):
333335
with pytest.raises(ValueError):
334336
faker.dsv(num_rows='1')

0 commit comments

Comments
 (0)