Skip to content

Commit

Permalink
fix: toml module now accepts keys beginning with dot (fix andreoliwa#183
Browse files Browse the repository at this point in the history
)

test: deprecation of the section without dots
refactor: remove unused hook
refactor: a single hook to handle and validate
refactor: a simpler hook implementation
refactor: reduce complexity
chore: add dots, remove pytest warnings
chore: validate JSON files with a schema
build: fix lint warnings and (almost all) tests
refactor: add Deprecation class
fix: don't display a deprecation warning for the merged style
  • Loading branch information
andreoliwa committed Nov 10, 2020
1 parent 30d88e8 commit b086a24
Show file tree
Hide file tree
Showing 29 changed files with 255 additions and 153 deletions.
18 changes: 9 additions & 9 deletions docs/defaults.rst
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ Content of `styles/black.toml <https://raw.githubusercontent.com/andreoliwa/nitp
["pyproject.toml".tool.black]
line-length = 120
[["pre-commit-config.yaml".repos]]
[[".pre-commit-config.yaml".repos]]
yaml = """
- repo: https://github.com/python/black
rev: 20.8b1
Expand All @@ -57,7 +57,7 @@ Content of `styles/black.toml <https://raw.githubusercontent.com/andreoliwa/nitp
# https://github.com/uiri/toml/issues/123
# https://github.com/uiri/toml/issues/230
# If they are fixed one day, remove this 'yaml' key and use only a 'repos' list with a single element:
#["pre-commit-config.yaml"]
#[".pre-commit-config.yaml"]
#repos = ["""
#<YAML goes here>
#"""]
Expand All @@ -83,7 +83,7 @@ Content of `styles/flake8.toml <https://raw.githubusercontent.com/andreoliwa/nit
# Nitpick recommends those plugins as part of the style, but doesn't install them automatically as before.
# This way, the developer has the choice of overriding this style, instead of having lots of plugins installed
# without being asked.
[["pre-commit-config.yaml".repos]]
[[".pre-commit-config.yaml".repos]]
yaml = """
- repo: https://gitlab.com/pycqa/flake8
rev: 3.8.4
Expand Down Expand Up @@ -130,7 +130,7 @@ Content of `styles/isort.toml <https://raw.githubusercontent.com/andreoliwa/nitp
force_grid_wrap = 0
combine_as_imports = true
[["pre-commit-config.yaml".repos]]
[[".pre-commit-config.yaml".repos]]
yaml = """
- repo: https://github.com/asottile/seed-isort-config
rev: v2.2.0
Expand Down Expand Up @@ -168,7 +168,7 @@ Content of `styles/mypy.toml <https://raw.githubusercontent.com/andreoliwa/nitpi
warn_redundant_casts = true
warn_unused_ignores = true
[["pre-commit-config.yaml".repos]]
[[".pre-commit-config.yaml".repos]]
yaml = """
- repo: https://github.com/pre-commit/mirrors-mypy
rev: v0.790
Expand Down Expand Up @@ -218,7 +218,7 @@ Content of `styles/pre-commit/bash.toml <https://raw.githubusercontent.com/andre

.. code-block:: toml
[["pre-commit-config.yaml".repos]]
[[".pre-commit-config.yaml".repos]]
yaml = """
- repo: https://github.com/openstack/bashate
rev: 2.0.0
Expand All @@ -235,7 +235,7 @@ Content of `styles/pre-commit/commitlint.toml <https://raw.githubusercontent.com

.. code-block:: toml
[["pre-commit-config.yaml".repos]]
[[".pre-commit-config.yaml".repos]]
yaml = """
- repo: https://github.com/alessandrojcm/commitlint-pre-commit-hook
rev: v3.0.0
Expand All @@ -254,7 +254,7 @@ Content of `styles/pre-commit/general.toml <https://raw.githubusercontent.com/an

.. code-block:: toml
[["pre-commit-config.yaml".repos]]
[[".pre-commit-config.yaml".repos]]
yaml = """
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v3.3.0
Expand Down Expand Up @@ -292,7 +292,7 @@ Content of `styles/pre-commit/python.toml <https://raw.githubusercontent.com/and

.. code-block:: toml
[["pre-commit-config.yaml".repos]]
[[".pre-commit-config.yaml".repos]]
yaml = """
- repo: https://github.com/pre-commit/pygrep-hooks
rev: v1.7.0
Expand Down
33 changes: 11 additions & 22 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 12 additions & 12 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ norecursedirs = .* build dist CVS _darcs {arch} *.egg venv var docs

[tox:tox]
isolated_build = True
requires =
requires =
tox-travis
tox-venv
tox-pyenv
Expand All @@ -76,12 +76,12 @@ envlist = clean,lint,py39,py38,py37,py36,py35,report
description = Run tests with pytest and coverage
deps = pytest-cov
extras = test
depends =
depends =
{py39,py38,py37,py36,py35}: clean
report: py39,py38,py37,py36,py35
setenv =
setenv =
PY_IGNORE_IMPORTMISMATCH = 1
commands =
commands =
python -m pip --version
python -m pytest --cov-config=setup.cfg --cov --cov-append --cov-report=term-missing --doctest-modules {posargs:-vv}

Expand All @@ -95,28 +95,28 @@ commands = coverage erase
description = Lint all files with pre-commit
basepython = python3.7
platform = linux|darwin
extras =
extras =
lint
test
deps =
deps =
pre-commit
safety
commands =
commands =
pre-commit run --all-files
safety check

[testenv:report]
description = Coverage report
skip_install = true
deps = coverage
commands =
commands =
coverage report
coverage html

[coverage:run]
branch = true
parallel = true
omit =
omit =
tests/*
.tox/*
/home/travis/virtualenv/*
Expand All @@ -135,12 +135,12 @@ sort = Cover
description = Build the HTML docs using Sphinx (sphinx-build, API docs, link checks)
basepython = python3.7
extras = doc
commands =
commands =
sphinx-apidoc --force --module-first --separate --implicit-namespaces --output-dir docs/source src/nitpick/
python3 docs/generate_rst.py

sphinx-build --color -b linkcheck docs "{toxworkdir}/docs_out"

sphinx-build -d "{toxworkdir}/docs_doctree" --color -b html docs "{toxworkdir}/docs_out" {posargs}

[bandit]
Expand Down
16 changes: 15 additions & 1 deletion src/nitpick/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import logging
from typing import TYPE_CHECKING, Optional

from identify import identify

from nitpick.app import NitpickApp
from nitpick.constants import (
MERGED_STYLE_TOML,
Expand All @@ -10,6 +12,7 @@
TOOL_NITPICK,
TOOL_NITPICK_JMEX,
)
from nitpick.exceptions import Deprecation
from nitpick.formats import TOMLFormat
from nitpick.generic import search_dict, version_to_tuple
from nitpick.mixin import NitpickMixin
Expand Down Expand Up @@ -68,7 +71,7 @@ def merge_styles(self) -> YieldFlake8Error:
self.style_dict = style.merge_toml_dict()
if not NitpickApp.current().style_errors:
# Don't show duplicated errors: if there are style errors already, don't validate the merged style.
style.validate_style(MERGED_STYLE_TOML, self.style_dict)
style.validate_style(MERGED_STYLE_TOML, self.style_dict, True)

from nitpick.flake8 import NitpickExtension # pylint: disable=import-outside-toplevel

Expand All @@ -82,3 +85,14 @@ def merge_styles(self) -> YieldFlake8Error:

self.nitpick_section = self.style_dict.get("nitpick", {})
self.nitpick_files_section = self.nitpick_section.get("files", {})


class FileNameCleaner: # pylint: disable=too-few-public-methods
"""Clean the file name and get its tags."""

def __init__(self, path_from_root: str) -> None:
if Deprecation.pre_commit_without_dash(path_from_root):
self.path_from_root = "." + path_from_root
else:
self.path_from_root = "." + path_from_root[1:] if path_from_root.startswith("-") else path_from_root
self.tags = identify.tags_from_filename(path_from_root)
41 changes: 41 additions & 0 deletions src/nitpick/exceptions.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
"""Nitpick exceptions."""
import warnings
from pathlib import Path
from typing import Any, Dict

from nitpick.constants import PROJECT_NAME


class NitpickError(Exception):
Expand Down Expand Up @@ -53,3 +57,40 @@ class StyleError(NitpickError):
def __init__(self, style_file_name: str, *args: object) -> None:
self.style_file_name = style_file_name
super().__init__(*args)


class Deprecation:
"""All deprecation messages in a single class.
When it's time to break compatibility, remove a method/warning below,
and older config files will trigger validation errors on Nitpick.
"""

@staticmethod
def pre_commit_without_dash(path_from_root: str) -> bool:
"""The pre-commit config should start with a dot on the config file."""
from nitpick.plugins.pre_commit import PreCommitPlugin # pylint: disable=import-outside-toplevel

if path_from_root == PreCommitPlugin.file_name[1:]:
warnings.warn(
'The section name for dotfiles should start with a dot: [".{}"]'.format(path_from_root),
DeprecationWarning,
)
return True

return False

@staticmethod
def jsonfile_section(style_errors: Dict[str, Any], is_merged_style: bool) -> bool:
"""The [nitpick.JSONFile] is not needed anymore; JSON files are now detected by the extension."""
has_nitpick_jsonfile_section = style_errors.get(PROJECT_NAME, {}).pop("JSONFile", None)
if has_nitpick_jsonfile_section:
if not style_errors[PROJECT_NAME]:
style_errors.pop(PROJECT_NAME)
if not is_merged_style:
warnings.warn(
"The [nitpick.JSONFile] section is not needed anymore; just declare your JSON files directly",
DeprecationWarning,
)
return True
return False
14 changes: 7 additions & 7 deletions src/nitpick/flake8.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@

import attr
from flake8.options.manager import OptionManager
from identify import identify

from nitpick import __version__
from nitpick.app import NitpickApp
from nitpick.config import FileNameCleaner
from nitpick.constants import PROJECT_NAME
from nitpick.mixin import NitpickMixin
from nitpick.typedefs import YieldFlake8Error
Expand Down Expand Up @@ -58,17 +58,17 @@ def run(self) -> YieldFlake8Error:
return []

# Get all root keys from the style TOML.
for path, config_dict in app.config.style_dict.items():
for config_key, config_dict in app.config.style_dict.items():
# All except "nitpick" are file names.
if path == PROJECT_NAME:
if config_key == PROJECT_NAME:
continue

# For each file name, find the plugin that can handle the file.
tags = identify.tags_from_filename(path)
for base_file in app.plugin_manager.hook.handle_config_file( # pylint: disable=no-member
config=config_dict, file_name=path, tags=tags
cleaner = FileNameCleaner(config_key)
for plugin_instance in app.plugin_manager.hook.handler( # pylint: disable=no-member
file_name=cleaner.path_from_root, tags=cleaner.tags
):
yield from base_file.check_exists()
yield from plugin_instance.process(config_dict)

return []

Expand Down
Loading

0 comments on commit b086a24

Please sign in to comment.