Skip to content
Closed
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
4 changes: 4 additions & 0 deletions docs/Coding-Conventions.md
Original file line number Diff line number Diff line change
Expand Up @@ -861,6 +861,10 @@ def go_shopping():

ℹ️ You can document a package by documenting the module docstring of the package directory's `__init__.py`

### Which docstring format should I follow?

We recommend (and internally use) the [Google docstring format](https://google.github.io/styleguide/pyguide.html#383-functions-and-methods) but you can choose any format so long as you are consistent.

### [D.1.2] ✔️ **DO** List exported modules and subpackages in a package's docstring

> 🐍 This rule stems from [PEP 257](https://www.python.org/dev/peps/pep-0257/#multi-line-docstrings)
Expand Down
2 changes: 1 addition & 1 deletion ni_python_styleguide/_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class AllowConfigGroup(click.Group):
"""click.Group subclass which allows for a config option to load options from."""

def __init__(self, *args, **kwargs):
"""Construct the click.Group with the config option."""
"""Constructs the click.Group with the config option."""

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Hmm.. I like the styling of the Google docstrings, but I've tended to prefer imperative method summaries, to harmonize with the rest of the Python community. But I can live with descriptive instead, if we have to go whole-hog on one standard or the other. One advantage of descriptive-style is that it would match our C# docstring conventions, so that's good, at least.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

well shit, I prefer the other style.. Must we adopt this as part of Google docstring style?
I suppose if we subscribe to google docstring, we should just follow that and keep it simple.. but.. bah!

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yeah, it's gonna be all-or-nothing. 😄 Anything in the middle would just be confusing...

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

bummer :( ..anyway not a big deal.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Well, it's kind of a big deal. Now our conventions violate PEP-0257. If this difference was highlighted pre-voting I would have changed from ambivalent to Sphinx.

@neilvana neilvana Jan 21, 2021 •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I wish the Python community had a readable, more consistent, standard. I like the overall readability of Google Style because it looks like it was written for a human and not a computer. The Sphinx style seems written for a parser not a human. I prefer Google Style for the same reason I prefer yaml or toml over XML.

But why did Google decide to deviate from PEP-257 in this way? --Neil wrings his hands-- Did they want to match other languages at Google? Were they just trying to be difficult? I don't think I even noticed this deviation when reading the style the first time (so good catch) and have generally preferred imperative mood since it usually is shorter and matches convention in Git commits.

(It's minor, but I'm also personally not a fan of mixing the imperative style description which can be seen as "first-party" with the "third-party" "Args", "Returns", "Raises", etc...)

I just interpret those as plurals nouns rather than descriptive verbs since a function has multiple arguments, can return multiple values, and can raise more than one thing. It's a little of a stretch but makes me feel better :-)

My personal preference would be to adopt Google Style and PEP-257 and prefer PEP-257 where they conflict (i.e. use imperative mood).

As a side curiosity do we have a mood standard among any other languages at NI?

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

NI's C# code uses declarative style, though it's not explicitly called out in the style guide (though there are examples there that are declarative).

@thejcannon thejcannon Jan 22, 2021 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Unsurprisingly, Google's developer documentation style guide states:

When you're writing reference documentation for a method, phrase the main method description in terms of what the method does (gets, lists, creates, searches), rather than what the developer would use it to do (get, list, create, search).

I've also read a handful of posts from various people online (language-agnostic) basically saying "client documentation uses descriptive. Developer documentation (I.e. comments) uses imperative". I think this makes the most sense as it fits with the context of what's being described and to whom.

Ultimately, I think I'm inclined to leave it as "We follow Google's guide." which would mean advocating for descriptive style across the company. However also calling out, much like which guide to follow that being consistent is ultimately the important thing. The tooling currently doesn't enforce any verb-mood, and I'll switch it back to simply enforce all conventions (minus verb mood).

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Sounds good to me. I also buy the developer versus user argument. I've never heard that before but it makes sense.

@alejandro5042 alejandro5042 Jan 22, 2021 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Agreed, I love their explanation. Talk in terms of what the method can do for the user (gets the thing), instead of what the program will do if you use it (get the thing).

kwargs["params"].append(
click.Option(
["--config"],
Expand Down
7 changes: 7 additions & 0 deletions ni_python_styleguide/config.ini
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,13 @@ ignore =
F812

# flake8-docstrings
# Ignoring the requirement on imperative mood, as not all conventions agree on this
# and ultimately is up to the developer. If, in the future, the mood was enforced based on
# convention choice we can consider removing this supression as that would make documentation
# consistent within a standard (but maybe not a codebase)
# Developers are welcome to turn back on this error in their specific projects.
# The same goes for the "descriptive"-mood error, if one was to be added to pydocstyle.
D401
# Ignoring things enforced by black
D200 # One-line docstring should fit on one line with quotes
D206 # Docstring should be indented with spaces, not tabs
Expand Down
6 changes: 3 additions & 3 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@


def pytest_collection_modifyitems(items):
"""Ignore deprecation warnings in all tests."""
"""Ignores deprecation warnings in all tests."""
for item in items:
item.add_marker(pytest.mark.filterwarnings("ignore::DeprecationWarning"))


@pytest.fixture
def styleguide(monkeypatch, cli_runner):
"""Fixture which runs the styleguide when executed.
"""Runs the styleguide when executed.

Args passed to the function are equivalent to CLI args,
and are automatically stringified.
Expand All @@ -40,7 +40,7 @@ def runner(*args):

@pytest.fixture
def chdir():
"""Fixture which changes the current working directory when executed."""
"""Changes the current working directory when executed."""
cwd = os.getcwd()
yield os.chdir
os.chdir(cwd)
4 changes: 2 additions & 2 deletions tests/test_cli/test_bare.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@


def test_no_args_prints_help(styleguide):
"""Test that running the styleguide without any arguments prints the help info."""
"""Tests that running the styleguide without any arguments prints the help info."""
result = styleguide()

assert result
assert result.output.startswith("Usage:")


def test_help_prints_help(styleguide):
"""Test that running the styleguide with "--help" prints the help info."""
"""Tests that running the styleguide with "--help" prints the help info."""
result = styleguide("--help")

assert result
Expand Down
32 changes: 16 additions & 16 deletions tests/test_cli/test_lint.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

@pytest.fixture
def styleguide_lint(styleguide, tmp_path, chdir):
"""Fixture which will run the styleguide with the "lint" subcommand.
"""Runs the styleguide with the "lint" subcommand.

Both `base_args` and `lint_args` must be iterables which will be transformed into strings
and passed on the cmd line in the following order: `<cmd> <base_args> lint <lint_args>.
Expand All @@ -29,7 +29,7 @@ def runner(base_args=[], lint_args=[]):

@pytest.fixture
def config_write(tmp_path):
"""Fixture which will write the specified data to a config file for the tool."""
"""Writes the specified data to a config file for the tool."""

def writer(**config_data):
(tmp_path / "pyproject.toml").write_text(
Expand All @@ -48,7 +48,7 @@ def writer(**config_data):
]
)
def styleguide_lint_with_options(request, styleguide_lint, config_write, tmp_path):
"""Fixture which will run the styleguide with the "lint" subcommand, given the provided options.
"""Runs the styleguide with the "lint" subcommand, given the provided options.

The options will be tested on the command line as well as in the config.
`base_args` and `lint_args` are similar to the `styleguide_lint` fixture, with the exception of
Expand All @@ -74,15 +74,15 @@ def runner(base_args={}, lint_args={}):


def test_lint__help_arg_prints_help(styleguide):
"""Test running the "lint" subcommand with "--help" prints help info."""
"""Tests running the "lint" subcommand with "--help" prints help info."""
result = styleguide("lint", "--help")

assert result, result.output
assert result.output.startswith("Usage:")


def test_lint__ignores_local_flake8_config(styleguide_lint, tmp_path):
"""Test that running lint ignores local flake8 config.
"""Tests that running lint ignores local flake8 config.

We test this by creating a local config with a smaller (80) max line length than our styleguide
enforces (100) and asserting the command passes with a line of length between 80 and 100.
Expand All @@ -103,7 +103,7 @@ def test_lint__ignores_local_flake8_config(styleguide_lint, tmp_path):


def test_lint__with_one_file_arg(styleguide_lint, tmp_path):
"""Test that we can specify the file on the cmd-line."""
"""Tests that we can specify the file on the cmd-line."""
(tmp_path / "spam.py").write_text(TOO_LONG_LINE)
result = styleguide_lint(lint_args=[tmp_path / "spam.py"])

Expand All @@ -112,7 +112,7 @@ def test_lint__with_one_file_arg(styleguide_lint, tmp_path):


def test_lint__with_one_multiple_file_args(styleguide_lint, tmp_path):
"""Test that we can specify multiple files on the cmd-line."""
"""Tests that we can specify multiple files on the cmd-line."""
(tmp_path / "spam1.py").write_text(TOO_LONG_LINE)
(tmp_path / "spam2.py").write_text(TOO_LONG_LINE)
result = styleguide_lint(lint_args=[tmp_path / "spam1.py", tmp_path / "spam2.py"])
Expand All @@ -121,7 +121,7 @@ def test_lint__with_one_multiple_file_args(styleguide_lint, tmp_path):


def test_lint__specifying_file_only_lints_file(styleguide_lint, tmp_path):
"""Test that when we specify file(s) on the cmd line other files are not linted."""
"""Tests that when we specify file(s) on the cmd line other files are not linted."""
(tmp_path / "spam1.py").write_text(TOO_LONG_LINE)
(tmp_path / "_spam2.py").write_text("") # empty
result = styleguide_lint(lint_args=[tmp_path / "_spam2.py"])
Expand All @@ -130,7 +130,7 @@ def test_lint__specifying_file_only_lints_file(styleguide_lint, tmp_path):


def test_lint__no_args_lints_dir(styleguide_lint, tmp_path):
"""Test that when we specify no file(s) on the cmd line we lint the directory."""
"""Tests that when we specify no file(s) on the cmd line we lint the directory."""
(tmp_path / "spam.py").write_text(TOO_LONG_LINE)

result = styleguide_lint()
Expand All @@ -140,7 +140,7 @@ def test_lint__no_args_lints_dir(styleguide_lint, tmp_path):


def test_lint__no_args_lints_subdirs(styleguide_lint, tmp_path):
"""Test that when we specify no file(s) on the cmd line we lint subdirs."""
"""Tests that when we specify no file(s) on the cmd line we lint subdirs."""
(tmp_path / "subdir").mkdir()
(tmp_path / "subdir" / "spam.py").write_text(TOO_LONG_LINE)

Expand All @@ -151,7 +151,7 @@ def test_lint__no_args_lints_subdirs(styleguide_lint, tmp_path):


def test_lint__lignores_venv_by_default(styleguide_lint, tmp_path):
"""Test that we exclude ".venv" dir by default."""
"""Tests that we exclude ".venv" dir by default."""
(tmp_path / ".venv").mkdir()
(tmp_path / ".venv" / "spam.py").write_text(TOO_LONG_LINE)

Expand All @@ -161,7 +161,7 @@ def test_lint__lignores_venv_by_default(styleguide_lint, tmp_path):


def test_lint__exclude__excludes_file(styleguide_lint_with_options, tmp_path):
"""Test that exclude option excludes a file."""
"""Tests that exclude option excludes a file."""
(tmp_path / "spam.py").write_text(TOO_LONG_LINE)

result = styleguide_lint_with_options(base_args=dict(exclude="spam.py"))
Expand All @@ -180,7 +180,7 @@ def test_lint__exclude__excludes_dir(styleguide_lint_with_options, tmp_path):


def test_lint__exclude__overrides_default(styleguide_lint_with_options, tmp_path):
"""Test that specifying exclude option overrides the default exclude."""
"""Tests that specifying exclude option overrides the default exclude."""
(tmp_path / ".venv").mkdir()
(tmp_path / ".venv" / "spam.py").write_text(TOO_LONG_LINE)

Expand All @@ -191,7 +191,7 @@ def test_lint__exclude__overrides_default(styleguide_lint_with_options, tmp_path


def test_lint__exclude__cmd_line_overrides_config(styleguide_lint, tmp_path, config_write):
"""Test that specifying exclude option on the cmd-line overrides the exclude in config."""
"""Tests that specifying exclude option on the cmd-line overrides the exclude in config."""
(tmp_path / "dir1").mkdir()
(tmp_path / "dir1" / "spam.py").write_text(TOO_LONG_LINE)
(tmp_path / "dir2").mkdir()
Expand All @@ -208,7 +208,7 @@ def test_lint__exclude__cmd_line_overrides_config(styleguide_lint, tmp_path, con
def test_lint__extend_exclude__extends_exclude(
styleguide_lint_with_options, tmp_path, config_write
):
"""Test that extend-exclude adds to exclude."""
"""Tests that extend-exclude adds to exclude."""
(tmp_path / "dir1").mkdir()
(tmp_path / "dir1" / "spam.py").write_text(TOO_LONG_LINE)
(tmp_path / "dir2").mkdir()
Expand All @@ -231,7 +231,7 @@ def test_lint__extend_exclude__extends_exclude(
],
)
def test_lint__verbosity_args_allowed(styleguide_lint, verbosity_args):
"""Test that using verbosity flags doesnt have any fatal side-effects."""
"""Tests that using verbosity flags doesnt have any fatal side-effects."""
result = styleguide_lint(base_args=[verbosity_args])

assert result, result.output
20 changes: 10 additions & 10 deletions tests/test_convention_doc/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,19 @@

@pytest.fixture
def sections():
"""Fixture with each convention section."""
"""All convention sections."""
return doctypes.SECTIONS


@pytest.fixture
def subsections():
"""Fixture with each convention subsection."""
"""All convention subsections."""
return doctypes.SUBSECTIONS


@pytest.fixture
def rules():
"""Fixture with each convention rule."""
"""All convention rules."""
return doctypes.RULES


Expand All @@ -28,7 +28,7 @@ def rules():
params=[pytest.param(section, id=section.identifier) for section in doctypes.SECTIONS],
)
def section(request):
"""Parametrized fixture of each convention section."""
"""Each convention section."""
return request.param


Expand All @@ -39,7 +39,7 @@ def section(request):
],
)
def subsection(request):
"""Parametrized fixture of each convention subsection."""
"""Each convention subsection."""
return request.param


Expand All @@ -48,7 +48,7 @@ def subsection(request):
params=[pytest.param(rule, id=rule.identifier) for rule in doctypes.RULES],
)
def rule(request):
"""Parametrized fixture of each convention rule."""
"""Each convention rule."""
return request.param


Expand All @@ -61,7 +61,7 @@ def rule(request):
],
)
def codeblock(request):
"""Parametrized fixture of each convention codeblock."""
"""Each convention codeblock."""
return request.param


Expand All @@ -75,7 +75,7 @@ def codeblock(request):
],
)
def bad_codeblock(request):
"""Parametrized fixture of each convention codeblock marked "Bad"."""
"""Each convention codeblock marked "Bad"."""
return request.param


Expand All @@ -89,7 +89,7 @@ def bad_codeblock(request):
],
)
def good_codeblock(request):
"""Parametrized fixture of each convention codeblock marked "Good"."""
"""Each convention codeblock marked "Good"."""
return request.param


Expand All @@ -103,5 +103,5 @@ def good_codeblock(request):
],
)
def best_codeblock(request):
"""Parametrized fixture of each convention codeblock marked "Best"."""
"""Each convention codeblock marked "Best"."""
return request.param
Loading