Skip to content

Commit

Permalink
Merge pull request #92 from mt-krainski/44-inclusive-renaming-whitelist
Browse files Browse the repository at this point in the history
Fix inclusive renaming whitelist
  • Loading branch information
MichaelAquilina authored Aug 27, 2024
2 parents 931af24 + 722c8f2 commit 44b5f3d
Show file tree
Hide file tree
Showing 7 changed files with 348 additions and 198 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ repos:
- "--remove-all-unused-imports"
- "--remove-unused-variable"
- repo: https://github.com/PyCQA/isort
rev: 5.10.0
rev: 5.12.0
hooks:
- id: isort
- repo: https://github.com/ambv/black
Expand Down
File renamed without changes.
33 changes: 31 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ Flake8 Spellcheck

Flake8 Plugin that spellchecks variables, functions, classes and other bits of your python code.

You can whitelist words that are specific to your project simply by adding them to ``whitelist.txt``
in the root of your project directory. Each word you add should be separated by a newline.
You can create an allowlist for words that are specific to your project simply by adding them a ``.spellcheck-allowlist`` file
in the root of your project directory. Each word you add should be separated by a newline.

Spelling is assumed to be in en_US.

Expand Down Expand Up @@ -61,6 +61,34 @@ The above configuration would only spellcheck comments
The above configuration would only spellcheck names

Specify Allowlist
---------------

You can specify a list of allowed words - spellcheck will then not raise errors when those
words are encountered. You can define the list of allowed words either as a file or as a
configuration parameter.

By default, spellcheck will try to load a `.spellcheck-allowlist` file in the root of your
project. You can override the file name using the `--spellcheck-allowlist-file` CLI
parameter, or in your flake8 configuration (e.g. in your ``.flake8`` file):

.. code-block:: ini
[flake8]
spellcheck-allowlist-file = your-allowlist-file
You can also define the allowlist directly using the `--spellcheck-allowlist` CLI parameter
(this takes a comma-separated list of words to allow) or using the flake8 configuration
(e.g. in your ``.flake8`` file):

.. code-block:: ini
[flake8]
spellcheck-allowlist = your, allowed, words
Ignore Rules
------------

Expand Down Expand Up @@ -89,6 +117,7 @@ Development
-----------

* Install `poetry <https://github.com/python-poetry>`__
* Install `golang <https://go.dev/doc/install>`__ (required by some of our pre-commit hooks)
* Run ``poetry install``
* Run ``poetry run pre-commit install --install-hooks``

Expand Down
29 changes: 20 additions & 9 deletions flake8_spellcheck/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,11 +131,15 @@ def load_dictionaries(cls, options: Namespace) -> Tuple[FrozenSet[str], FrozenSe
data = dictionary_path.read_text()
words |= {w.lower() for w in data.split("\n")}

if os.path.exists(options.whitelist):
with open(options.whitelist) as fp:
whitelist = fp.read()
whitelist_data = {w.lower() for w in whitelist.split("\n")}
words |= whitelist_data
if os.path.exists(options.spellcheck_allowlist_file):
with open(options.spellcheck_allowlist_file) as fp:
allowlist = fp.read()
allowlist_data_from_file = {w.lower() for w in allowlist.split("\n")}
words |= allowlist_data_from_file

if options.spellcheck_allowlist is not None:
allowlist_data = {w.lower() for w in options.spellcheck_allowlist}
words |= allowlist_data

# Hacky way of getting dictionary with symbols stripped
no_symbols = set()
Expand All @@ -149,16 +153,23 @@ def load_dictionaries(cls, options: Namespace) -> Tuple[FrozenSet[str], FrozenSe
@classmethod
def add_options(cls, parser: OptionManager) -> None:
parser.add_option(
"--whitelist",
help="Path to text file containing whitelisted words",
default="whitelist.txt",
"--spellcheck-allowlist-file",
help="Path to text file containing allowed words",
default=".spellcheck-allowlist",
parse_from_config=True,
)
parser.add_option(
"--spellcheck-allowlist",
help="Comma separated list of words to allow",
default=None,
comma_separated_list=True,
parse_from_config=True,
)
parser.add_option(
"--dictionaries",
# Unfortunately optparse does not support nargs="+" so we
# need to use a command separated list to work round it
help="Command separated list of dictionaries to enable",
help="Comma separated list of dictionaries to enable",
default="en_US,python,technical",
comma_separated_list=True,
parse_from_config=True,
Expand Down
1 change: 1 addition & 0 deletions flake8_spellcheck/technical.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
adb
allowlist
auth
autocomplete
autofill
Expand Down
Loading

0 comments on commit 44b5f3d

Please sign in to comment.