Skip to content

Commit a7b9855

Browse files
committed
Uncouple formatting from Py2StubTransformer
1 parent 405a94f commit a7b9855

File tree

3 files changed

+10
-9
lines changed

3 files changed

+10
-9
lines changed

src/docstub/_cli.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,12 @@
1414
)
1515
from ._cache import FileCache
1616
from ._config import Config
17-
from ._stubs import Py2StubTransformer, walk_source, walk_source_and_targets
17+
from ._stubs import (
18+
Py2StubTransformer,
19+
try_format_stub,
20+
walk_source,
21+
walk_source_and_targets,
22+
)
1823
from ._version import __version__
1924

2025
logger = logging.getLogger(__name__)
@@ -171,6 +176,7 @@ def main(source_dir, out_dir, config_path, verbose):
171176
stub_content = stub_transformer.python_to_stub(
172177
py_content, module_path=source_path
173178
)
179+
stub_content = try_format_stub(stub_content)
174180
except (SystemExit, KeyboardInterrupt):
175181
raise
176182
except Exception as e:

src/docstub/_stubs.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ def current_source(self, value):
264264
if self.types_db is not None:
265265
self.types_db.current_source = value
266266

267-
def python_to_stub(self, source, *, module_path=None, try_format=True):
267+
def python_to_stub(self, source, *, module_path=None):
268268
"""Convert Python source code to stub-file ready code.
269269
270270
Parameters
@@ -274,9 +274,6 @@ def python_to_stub(self, source, *, module_path=None, try_format=True):
274274
The location of the source that is transformed into a stub file.
275275
If given, used to enhance logging & error messages with more
276276
context information.
277-
try_format : bool, optional
278-
Try to format the output, if the appropriate dependencies are
279-
installed.
280277
281278
Returns
282279
-------
@@ -292,8 +289,6 @@ def python_to_stub(self, source, *, module_path=None, try_format=True):
292289
source_tree = cst.metadata.MetadataWrapper(source_tree)
293290
stub_tree = source_tree.visit(self)
294291
stub = stub_tree.code
295-
if try_format is True:
296-
stub = try_format_stub(stub)
297292
return stub
298293
finally:
299294
self._scope_stack = None

tests/test_stubs.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ def test_attributes_no_doctype(self, assign, expected, scope):
173173
src = NESTED_CLASS_ATTRIBUTE_TEMPLATE.format(assign=assign, doctype="")
174174

175175
transformer = Py2StubTransformer()
176-
result = transformer.python_to_stub(src, try_format=False)
176+
result = transformer.python_to_stub(src)
177177

178178
# Find exactly one occurrence of `expected`
179179
pattern = f"^ *({re.escape(expected)})$"
@@ -212,7 +212,7 @@ def test_attributes_with_doctype(self, assign, doctype, expected, scope):
212212
src = NESTED_CLASS_ATTRIBUTE_TEMPLATE.format(assign=assign, doctype=doctype)
213213

214214
transformer = Py2StubTransformer()
215-
result = transformer.python_to_stub(src, try_format=False)
215+
result = transformer.python_to_stub(src)
216216

217217
# Find exactly one occurrence of `expected`
218218
pattern = f"^ *({re.escape(expected)})$"

0 commit comments

Comments
 (0)