Skip to content

Commit

Permalink
feat: enforce settings on .pylintrc (#310)
Browse files Browse the repository at this point in the history
* style: enforce settings on .pylintrc
* test: .pylintrc is applied with the default style
* docs: .pylintrc style example
  • Loading branch information
andreoliwa committed Mar 9, 2021
1 parent 44d6cf4 commit 6aee663
Show file tree
Hide file tree
Showing 8 changed files with 152 additions and 13 deletions.
2 changes: 1 addition & 1 deletion .pylintrc
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ disable=bad-continuation,bad-whitespace,
bad-functions=map,filter

# Good variable names which should always be accepted, separated by a comma
good-names=i,j,k,e,ex,Run,_,id,rv,cf
good-names = i,j,k,e,ex,Run,_,id,rv

# Regular expression matching correct constant names
const-rgx=(([A-Z_][A-Z0-9_]*)|([a-z_][a-z0-9_]*)|(__.*__)|register|urlpatterns)$
Expand Down
56 changes: 54 additions & 2 deletions docs/examples.rst
Original file line number Diff line number Diff line change
Expand Up @@ -362,8 +362,60 @@ Contents of `styles/pylint.toml <https://github.com/andreoliwa/nitpick/blob/v0.2

.. code-block:: toml
["pyproject.toml".tool.poetry.dev-dependencies]
pylint = "*"
["pyproject.toml".tool.poetry.dependencies]
pylint = {version = "*", optional = true}
["pyproject.toml".tool.poetry.extras]
lint = ["pylint"]
[".pylintrc".MASTER]
# Use multiple processes to speed up Pylint.
jobs = 1
[".pylintrc".REPORTS]
# Set the output format. Available formats are text, parseable, colorized, msvs (visual studio) and html.
# You can also give a reporter class, eg mypackage.mymodule.MyReporterClass.
output-format = "colorized"
[".pylintrc"."MESSAGES CONTROL"]
# TODO: deal with character separated INI options in https://github.com/andreoliwa/nitpick/issues/271
# The "nitpick.files" section doesn't work out of the box for .pylintrc:
# [nitpick.files.".pylintrc"]
# comma_separated_values = ["MESSAGES CONTROL.disable"]
# This syntax will be deprecated anyway, so I won't make it work now
# Configurations for the black formatter
#disable = "bad-continuation,bad-whitespace,fixme,cyclic-import"
[".pylintrc".BASIC]
# List of builtins function names that should not be used, separated by a comma
bad-functions = "map,filter"
# Good variable names which should always be accepted, separated by a comma
good-names = "i,j,k,e,ex,Run,_,id,rv"
[".pylintrc".FORMAT]
# Maximum number of characters on a single line.
max-line-length = 120
# Maximum number of lines in a module
max-module-lines = 1000
# TODO: deal with empty options (strings with spaces and quotes); maybe it's a ConfigParser/ConfigUpdater thing
# String used as indentation unit. This is usually " " (4 spaces) or "\t" (1 tab).
#indent-string = " "
# Number of spaces of indent required inside a hanging or continued line.
indent-after-paren = 4
[".pylintrc".SIMILARITIES]
# Minimum lines number of a similarity.
min-similarity-lines = 4
# Ignore comments when computing similarities.
ignore-comments = "yes"
# Ignore docstrings when computing similarities.
ignore-docstrings = "yes"
# Ignore imports when computing similarities.
ignore-imports = "no"
[".pylintrc".VARIABLES]
# A regular expression matching the name of dummy variables (i.e. expectedly not used).
dummy-variables-rgx = "_$|dummy"
.. _example-python-3-6:

Expand Down
1 change: 1 addition & 0 deletions nitpick-style.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ include = [
"styles/flake8",
"styles/isort",
"styles/mypy",
"styles/pylint",
"styles/pytest",
"styles/tox",
"styles/package-json",
Expand Down
1 change: 1 addition & 0 deletions src/nitpick/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
PRE_COMMIT_CONFIG_YAML = ".pre-commit-config.yaml"
EDITOR_CONFIG = ".editorconfig"
TOX_INI = "tox.ini"
PYLINTRC = ".pylintrc"

SINGLE_QUOTE = "'"
DOUBLE_QUOTE = '"'
Expand Down
5 changes: 2 additions & 3 deletions src/nitpick/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
"""
import re
from collections import OrderedDict
from functools import lru_cache
from pathlib import Path
from typing import Any, Dict, Iterable, List, MutableMapping, Optional, Tuple, Union

Expand Down Expand Up @@ -223,15 +222,15 @@ def is_url(url: str) -> bool:
return url.startswith("http")


@lru_cache()
def relative_to_current_dir(path_or_str: Optional[PathOrStr]) -> str:
"""Return a relative path to the current dir or an absolute path."""
if not path_or_str:
return ""

path = Path(path_or_str)
if str(path).startswith(str(Path.cwd())):
return str(path.relative_to(Path.cwd())).lstrip(".")
rv = str(path.relative_to(Path.cwd()))
return "" if rv == "." else rv

return str(path.absolute())

Expand Down
56 changes: 54 additions & 2 deletions styles/pylint.toml
Original file line number Diff line number Diff line change
@@ -1,2 +1,54 @@
["pyproject.toml".tool.poetry.dev-dependencies]
pylint = "*"
["pyproject.toml".tool.poetry.dependencies]
pylint = {version = "*", optional = true}

["pyproject.toml".tool.poetry.extras]
lint = ["pylint"]

[".pylintrc".MASTER]
# Use multiple processes to speed up Pylint.
jobs = 1

[".pylintrc".REPORTS]
# Set the output format. Available formats are text, parseable, colorized, msvs (visual studio) and html.
# You can also give a reporter class, eg mypackage.mymodule.MyReporterClass.
output-format = "colorized"

[".pylintrc"."MESSAGES CONTROL"]
# TODO: deal with character separated INI options in https://github.com/andreoliwa/nitpick/issues/271
# The "nitpick.files" section doesn't work out of the box for .pylintrc:
# [nitpick.files.".pylintrc"]
# comma_separated_values = ["MESSAGES CONTROL.disable"]
# This syntax will be deprecated anyway, so I won't make it work now
# Configurations for the black formatter
#disable = "bad-continuation,bad-whitespace,fixme,cyclic-import"

[".pylintrc".BASIC]
# List of builtins function names that should not be used, separated by a comma
bad-functions = "map,filter"
# Good variable names which should always be accepted, separated by a comma
good-names = "i,j,k,e,ex,Run,_,id,rv"

[".pylintrc".FORMAT]
# Maximum number of characters on a single line.
max-line-length = 120
# Maximum number of lines in a module
max-module-lines = 1000
# TODO: deal with empty options (strings with spaces and quotes); maybe it's a ConfigParser/ConfigUpdater thing
# String used as indentation unit. This is usually " " (4 spaces) or "\t" (1 tab).
#indent-string = " "
# Number of spaces of indent required inside a hanging or continued line.
indent-after-paren = 4

[".pylintrc".SIMILARITIES]
# Minimum lines number of a similarity.
min-similarity-lines = 4
# Ignore comments when computing similarities.
ignore-comments = "yes"
# Ignore docstrings when computing similarities.
ignore-docstrings = "yes"
# Ignore imports when computing similarities.
ignore-imports = "no"

[".pylintrc".VARIABLES]
# A regular expression matching the name of dummy variables (i.e. expectedly not used).
dummy-variables-rgx = "_$|dummy"
5 changes: 3 additions & 2 deletions tests/test_generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from testfixtures import compare

from nitpick.constants import TOX_INI
from nitpick.constants import EDITOR_CONFIG, TOX_INI
from nitpick.generic import MergeDict, flatten, get_subclasses, relative_to_current_dir
from tests.helpers import assert_conditions

Expand Down Expand Up @@ -95,6 +95,7 @@ def test_relative_to_current_dir(home, cwd):
f"{home_dir}{sep}another": f"{home_dir}{sep}another",
Path(f"{home_dir}{sep}bla{sep}bla"): f"{home_dir}{sep}bla{sep}bla",
f"{project_dir}{sep}{TOX_INI}": TOX_INI,
f"{project_dir}{sep}{EDITOR_CONFIG}": EDITOR_CONFIG,
Path(f"{project_dir}{sep}apps{sep}manage.py"): f"apps{sep}manage.py",
f"{home_dir}{sep}another{sep}one{sep}bites.py": f"{home_dir}{sep}another{sep}one{sep}bites.py",
Path(f"{home_dir}{sep}bla{sep}bla.txt"): f"{home_dir}{sep}bla{sep}bla.txt",
Expand All @@ -119,4 +120,4 @@ def test_relative_to_current_dir(home, cwd):
)

for path, expected in examples.items():
assert relative_to_current_dir(path) == expected
compare(actual=relative_to_current_dir(path), expected=expected, prefix=f"Path: {path}")
39 changes: 36 additions & 3 deletions tests/test_ini.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from configupdater import ConfigUpdater

from nitpick.constants import EDITOR_CONFIG, SETUP_CFG, TOX_INI
from nitpick.constants import EDITOR_CONFIG, PYLINTRC, SETUP_CFG, TOX_INI
from nitpick.plugins.ini import IniPlugin, Violations
from nitpick.violations import Fuss, SharedViolations
from tests.helpers import XFAIL_ON_WINDOWS, ProjectMock
Expand Down Expand Up @@ -86,13 +86,46 @@ def test_default_style_is_applied(project_with_default_style):
[tox]
isolated_build = True
"""
expected_pylintrc = """
[BASIC]
bad-functions = map,filter
good-names = i,j,k,e,ex,Run,_,id,rv
[FORMAT]
indent-after-paren = 4
max-line-length = 120
max-module-lines = 1000
[MASTER]
jobs = 1
[REPORTS]
output-format = colorized
[SIMILARITIES]
ignore-comments = yes
ignore-docstrings = yes
ignore-imports = no
min-similarity-lines = 4
[VARIABLES]
dummy-variables-rgx = _$|dummy
"""
project_with_default_style.api_check_then_apply(
Fuss(True, SETUP_CFG, 321, " was not found. Create it with this content:", expected_setup_cfg),
Fuss(True, EDITOR_CONFIG, 321, " was not found. Create it with this content:", expected_editor_config),
Fuss(True, TOX_INI, 321, " was not found. Create it with this content:", expected_tox_ini),
partial_names=[SETUP_CFG, EDITOR_CONFIG, TOX_INI],
Fuss(True, PYLINTRC, 321, " was not found. Create it with this content:", expected_pylintrc),
partial_names=[SETUP_CFG, EDITOR_CONFIG, TOX_INI, PYLINTRC],
).assert_file_contents(
SETUP_CFG, expected_setup_cfg, EDITOR_CONFIG, expected_editor_config, TOX_INI, expected_tox_ini
SETUP_CFG,
expected_setup_cfg,
EDITOR_CONFIG,
expected_editor_config,
TOX_INI,
expected_tox_ini,
PYLINTRC,
expected_pylintrc,
)


Expand Down

0 comments on commit 6aee663

Please sign in to comment.