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

Rename the formatter file since formatter is a standard library #16

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions pydocstring/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@
import parso
from parso.python.tree import BaseNode, search_ancestor

import pydocstring.formatter
from pydocstring import exc
from pydocstring import exc, docstring_formatter

FORMATTER = {
"google": {
Expand Down Expand Up @@ -112,11 +111,11 @@ def generate_docstring(source, position=(1, 0), formatter="google", autocomplete
)

if scope.type == "classdef":
return pydocstring.formatter.class_docstring(scope, FORMATTER[formatter])
return docstring_formatter.class_docstring(scope, FORMATTER[formatter])
elif scope.type == "funcdef":
return pydocstring.formatter.function_docstring(scope, FORMATTER[formatter])
return docstring_formatter.function_docstring(scope, FORMATTER[formatter])
elif scope.type == "file_input":
return pydocstring.formatter.module_docstring(scope, FORMATTER[formatter])
return docstring_formatter.module_docstring(scope, FORMATTER[formatter])

raise exc.FailedToGenerateDocstringError(
"Failed to generate Docstring for: {}".format(scope)
Expand Down
57 changes: 29 additions & 28 deletions pydocstring/formatter.py → pydocstring/docstring_formatter.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,29 @@
"""
Google Docstring Formatter
Docstring Formatter
"""

from typing import Dict
from parso.python.tree import (
Class,
ExprStmt,
Function,
KeywordStatement,
Module,
Name,
PythonNode,
)

from pydocstring.format_utils import (
get_exception_name,
get_param_info,
get_return_info,
safe_determine_type,
)
)


def function_docstring(parso_function, formatter):
def function_docstring(parso_function, format_template: Dict[str, str]) -> str:
"""
Format a google docstring for a function
Format a docstring for a function

Args:
parso_function (Function): The function tree node
format_template (Dict): The specific templates used to construct the docstring

Returns:
str: The formatted docstring
Expand All @@ -35,62 +34,63 @@ def function_docstring(parso_function, formatter):

params = parso_function.get_params()
if params:
docstring += formatter["start_args_block"]
docstring += format_template["start_args_block"]
for param in params:
if param.star_count == 1:
docstring += formatter["param_placeholder_args"].format(
docstring += format_template["param_placeholder_args"].format(
param.name.value, "Variable length argument list."
)
elif param.star_count == 2:
docstring += formatter["param_placeholder_kwargs"].format(
docstring += format_template["param_placeholder_kwargs"].format(
param.name.value, "Arbitrary keyword arguments."
)
else:
docstring += formatter["param_placeholder"].format(
docstring += format_template["param_placeholder"].format(
*get_param_info(param)
)

returns = list(parso_function.iter_return_stmts())
if returns:
docstring += formatter["start_return_block"]
docstring += format_template["start_return_block"]
for ret in returns:
docstring += formatter["return_placeholder"].format(
docstring += format_template["return_placeholder"].format(
*get_return_info(ret, parso_function.annotation)
)
elif parso_function.annotation:
docstring += formatter["start_return_block"]
docstring += formatter["return_annotation_placeholder"].format(
docstring += format_template["start_return_block"]
docstring += format_template["return_annotation_placeholder"].format(
parso_function.annotation.value
)

yields = list(parso_function.iter_yield_exprs())
if yields:
docstring += formatter["start_yield_block"]
docstring += format_template["start_yield_block"]
for yie in yields:
docstring += formatter["yield_placeholder"].format(
docstring += format_template["yield_placeholder"].format(
*get_return_info(yie, parso_function.annotation)
)

raises = list(parso_function.iter_raise_stmts())
if raises:
docstring += formatter["start_raise_block"]
docstring += format_template["start_raise_block"]
for exception in raises:
docstring += formatter["raise_placeholder"].format(
docstring += format_template["raise_placeholder"].format(
get_exception_name(exception)
)

docstring += "\n"
return docstring


def class_docstring(parso_class, formatter):
def class_docstring(parso_class, format_template: Dict[str, str]) -> str:
"""
Format a google docstring for a class
Format a docstring for a class

Only documents attributes, ``__init__`` method args can be documented on the ``__init__`` method

Args:
parso_class (Class): The class tree node
format_template (Dict): The specific templates used to construct the docstring

Returns:
str: The formatted docstring
Expand All @@ -109,26 +109,27 @@ def class_docstring(parso_class, formatter):
attribute_expressions.append(child3)

if attribute_expressions:
docstring += formatter["start_attributes"]
docstring += format_template["start_attributes"]
for attribute in attribute_expressions:
name = attribute.children[0].value
code = attribute.get_rhs().get_code().strip()
attr_type = safe_determine_type(code)
attr_str = formatter["attribute_placeholder"].format(name, attr_type, code)
attr_str = format_template["attribute_placeholder"].format(name, attr_type, code)
docstring += attr_str

docstring += "\n"
return docstring


def module_docstring(parso_module, formatter):
def module_docstring(parso_module, format_template: Dict[str, str]) -> str:
"""
Format a google docstring for a module
Format a docstring for a module

Only documents attributes, ``__init__`` method args can be documented on the ``__init__`` method

Args:
parso_module (Module): The module tree node
format_template (Dict): The specific templates used to construct the docstring

Returns:
str: The formatted docstring
Expand All @@ -145,12 +146,12 @@ def module_docstring(parso_module, formatter):
attribute_expressions.append(child2)

if attribute_expressions:
docstring += formatter["start_attributes"]
docstring += format_template["start_attributes"]
for attribute in attribute_expressions:
name = attribute.children[0].value
code = attribute.get_rhs().get_code().strip()
attr_type = safe_determine_type(code)
attr_str = formatter["attribute_placeholder"].format(name, attr_type, code)
attr_str = format_template["attribute_placeholder"].format(name, attr_type, code)
docstring += attr_str

docstring += "\n"
Expand Down