67 Commits

Author SHA1 Message Date
8d0d82bcc0 Add type annotations to the project and use mypy (#2588)
Provide type annotations and use mypy for static type checking. Type
checkers help ensure that the project is using variables and functions
in the code correctly. With mypy, CI will warn when those types are used
incorrectly.

The mypy project and docs:
https://github.com/python/mypy
https://mypy.readthedocs.io/en/stable/index.html
2022-11-07 11:10:00 -05:00
3576f32eee Remove obsolete Python 2 workaround 2022-11-06 22:25:23 +01:00
62bd5ce330 Fix Deepsource issues (#2085)
* DeepSource issue: Built-in function `len` used as condition

Using the `len` function to check if a sequence is empty is not idiomatic
and can be less performant than checking the truthiness of the object.

`len` doesn't know the context in which it is called, so if computing the
length means traversing the entire sequence, it must; it doesn't know
that the result is just being compared to 0. Computing the boolean value
can stop after it sees the first element, regardless of how long the
sequence actually is.

* DeepSource issue: Consider using literal syntax to create the data structure

Using the literal syntax can give minor performance bumps compared to
using function calls to create `dict`, `list` and `tuple`.

This is because here, the name dict must be looked up in the global
scope in case it has been rebound. Same goes for the other two types
`list()` and `tuple()`.

* DeepSource issue: Consider decorating method with `@staticmethod`

The method doesn't use its bound instance. Decorate this method with
`@staticmethod` decorator, so that Python does not have to instantiate
a bound method for every instance of this class thereby saving memory
and computation. Read more about staticmethods here.

* DeepSource issue: Consider using `in`

To check if a variable is equal to one of many values, combine the values
into a tuple and check if the variable is contained `in` it instead of
checking for equality against each of the values. This is faster, less
verbose, and more readable.

* DeepSource issue: Implicit enumerate calls found

Using `range(len(...))` is not pythonic. Python does not have not
index-based loops. Instead, it uses collection iterators.

Python has a built-in method enumerate which adds a counter to an
iterable. Using this, you can access the counter and the value from
the iterable at the same time. It is therefore recommended to replace
`range(len(...))` with `enumerate(...)`.
2022-11-02 11:44:15 -04:00
50106dd578 Add isort to project (#2564)
isort is a utility to sort Python imports alphabetically, and
automatically separate into sections and by type. This will provide a
consistent import style from contributors. As well, by using a tool, a
contributor doesn't need to think about or anticipate the project's
preferred style, just let the tool handle it.

Run isort GitHub action as part of CI.
2022-11-02 11:42:44 -04:00
0b675f6b99 Handle bad globs passed to if --skip/-S (#2159)
Co-authored-by: Peter Newman <peternewman@users.noreply.github.com>
2022-10-31 07:17:40 -04:00
458de804cd Update subprocess usage to use modern subprocess.run()
https://docs.python.org/3/library/subprocess.html#subprocess.run

> The recommended approach to invoking subprocesses is to use the run()
> function for all use cases it can handle.
2022-10-30 00:22:47 +02:00
b2866507b9 Remove the broken tests for now 2022-10-20 15:10:55 +02:00
f590535d7d Recreate Dimitri's demo as a test 2022-10-20 15:10:55 +02:00
b98f2c66d3 Fix the expected typo numbers 2022-10-20 15:10:55 +02:00
9c5c6ec444 Test a hidden subdirectory too 2022-10-20 15:10:55 +02:00
04f93cfd28 Add relative hidden directory tests 2022-10-20 15:10:55 +02:00
a050986bf8 Fix uncaught exception on unreadable files (#2196)
* Avoid bailing out with uncaught `PermissionError`

* Update codespell_lib/tests/test_basic.py

* FIX: Coverage

* FIX: Already short-circuited

Co-authored-by: Eric Larson <larson.eric.d@gmail.com>
2022-10-19 11:44:09 -04:00
d31e0df51a Apply pyupgrade to project (#2364)
Update patterns to modern Python syntax and features. Drops unnecessary
legacy syntax that was required for Python 2 (removed in
9c3db1a5c5e02739eedba9e9915acd51736300b3).

Details on pyupgrade can be found at:
https://github.com/asottile/pyupgrade
2022-10-18 13:58:43 -04:00
900f18654b Fix encoding detection and exception on empty files (#2195)
The encoding detection code was trying to catch encoding-related
exceptions when the file is opened. This doesn't make sense, because
at this point no data has been read, therefore no encoding errors can be
detected. Instead, catch encoding-related exceptions when the file
contents are read.

Also avoid bailing out with `Exception('Unknown encoding')` on empty
files.
2022-10-12 13:17:12 -04:00
4792904cb7 ENH: CWD 2022-10-05 12:16:29 -04:00
wzy
2a0ee4b4b9 Fix #2055: Add support for PEP 518 (#2290)
Co-authored-by: Eric Larson <larson.eric.d@gmail.com>
2022-10-05 10:36:19 -04:00
81e4092793 Add some alternative line endings and whitespace checks (#2490)
Co-authored-by: vikivivi <22041592+vikivivi@users.noreply.github.com>
2022-09-12 11:31:25 -04:00
e8befc136d Simplify code
The list comprehension is shorter than the map() version.
I feel it is also simpler, although that is debatable.

This is consistent with the previous commit.
2022-01-07 18:26:23 +01:00
fd75e950ea Capitalize all suggested fixes
When a wrong word is capitalized, capitalize all the suggested fixes,
instead of only the first fix.
2022-01-04 21:43:50 +01:00
01f5c77d84 print("ERROR: ...") should print to sys.stderr 2021-11-16 11:31:30 +01:00
aaee230f1f __future__ not needed by Python >= 3.6 2021-09-30 10:47:09 +02:00
0338e3cc0e Merge pull request #1656 from jonmeow/ignore-uris
Add support for ignoring spelling mistakes in URIs specifically.
2021-06-02 00:17:26 +01:00
ad7211810b test 2021-06-01 15:22:08 -07:00
19c223f2c7 skip files passed in if matching the skip glob 2021-05-04 15:43:58 -07:00
a5402ec769 Merge remote-tracking branch 'upstream/master' into ignore-uris 2020-11-06 11:55:12 -08:00
6bb3ef4e96 Rephrase rejected to un-ignored 2020-10-12 14:16:46 -07:00
909f0dce08 More tests 2020-10-12 14:15:38 -07:00
f315b0dec6 Tests and option 2020-09-08 10:42:39 -07:00
9672234591 Read options from config (#1668)
* Read options from config

* Fix assert in test

* Fix tests

* Fix readme to pass rst checks in Travis

* Also support .codespellrc config file

* Fix flake8 error

* CLI args override config args

* Rename tool:codespell to just codespell in config

* Fix typo in readme

* Remove unnecessary check for existance of config files (configparser already handles this case)
2020-09-01 15:10:19 -04:00
8069358084 Remove duplicate test 2020-08-28 14:32:10 -07:00
b8db3f0883 Fix flake8 2020-08-28 14:29:59 -07:00
1e7f5f7e6f Add support for ignoring spelling mistakes in URIs specifically. 2020-08-28 14:23:10 -07:00
5ae1b91fae More tests 2020-08-10 15:21:56 -07:00
5c608e018d Add tests for empty ignore-regex 2020-08-06 11:36:22 -07:00
69ece679a5 Remove accidental argparse import 2020-07-27 11:29:26 -07:00
da4ae3e54c Addressing comments 2020-07-27 11:26:15 -07:00
9d6f0051b2 Switch to ignore-regex as a simple flag 2020-07-24 12:41:17 -07:00
3508ed6b5f More --ignore-regex tests 2020-07-10 15:09:27 -07:00
018f0c6465 Add --ignore-regex for URI/email handling.
This is for issue #676, where typos are found in actually-okay URIs/emails. Because these are closer to names in context, this ignores them.

Mechanically, this erases the URI/email text before the word regex is applied.
2020-07-08 09:19:27 -07:00
00f8ffd23d BUG: Use return codes properly, add --count (#1558)
* BUG: Use return codes properly, add --count

* FIX: EX_USAGE (kind of)

* FIX: EX_USAGE (really)

* FIX: Dont bother importing

* FIX: More

* STY: Caps

* MAINT: Version bump
2020-06-16 12:03:47 -04:00
a24debaf46 Disable 'BINARY file' warning by default (#1441)
* Disable 'BINARY file' warning by default

Currently codespell prints a warning if it finds binary files in the tree.
This is annoying as many projects contain .git/.svn blobs, images, etc.
Disable the warning by default and let users enable it if needed.

Co-authored-by: Waldir Pimenta <waldyrious@gmail.com>

* Improve help message for the --quiet-level option

* Update _codespell.py

Co-authored-by: Anatol Pomozov <anatol.pomozov@gmail.com>
Co-authored-by: Eric Larson <larson.eric.d@gmail.com>
2020-06-11 16:42:49 -04:00
f24af24a01 FIX: Misspelled dir 2020-06-10 10:54:05 -04:00
f566b4fe2e FIX: --check-hidden respects directories 2020-06-10 10:11:40 -04:00
f107ddb54b Fix the filename typo detection edge cases. Closes #1532 and some other bugs (#1539)
* Add a test for checking the filename of an empty file

To test #1532

* Test an irregular file with a typo

* Fix the filename typo detection edge cases

* Split the irregular file test into a seperate test

Probably easier to skip and certainly makes the maths easier

* Skip the test if mkfifo is missing

* Add the missing import

* Fix the test skip logic

* Write in the right language!

* Test filename checking on a binary file
2020-06-03 12:54:31 -04:00
c2fe4165b1 Correctly ignore hidden files in directories. Closes #1531 (#1533)
* Add some tests for #1531

* Ignore hidden files in directories, fixes #1531
2020-06-01 10:38:06 -04:00
d978da6e31 Check errors don't exist as valid words in the aspell dictionary (#1142)
* Check errors don't exist as valid words in the aspell dictionary

* Install aspell on Travis

* Add some missing packages

* Remove a virtual package

* Just install the version of aspell-python we need

* Keep flake8 happy

* Switch to warnings and count them, so we can see all the aspell errors in one go

* Handle different encoding of the word and aspell

* Try and fix the encoding conversion

* Find out the encoding type

* Don't assert on number of warnings

* Don't record warnings for now

* Warn on all the encoding options

* pprint the encoding

* More warning work

* Use the actual encoding type

* Correct the logic

* ENH: Multi dict support

* FIX: Fixes after merge

* FIX: Better error check

* FIX: More thorough testing, locations

* FIX: Try newer aspell

* FIX: Move to new dict

* FIX: Move

* FIX: Restore removals from #1181

* FIX: One from #1362

* Add rare chack->check, cheque,

* Minor tidy of some dictionary check code

* Add some more suggestions.

* Fix the whitespace

* Really fix the whitespace

* FIX: Refactor requirement

* Log an error when aspell not found and not required

* Fix the error logging

* Test all variants of present and missing from aspell

* Undo some tuple tidying

* Fix the true/false values used

* Skip some flake8 tests

* Fix the test cases

* Correct the not in aspell test and fix some test cases

* Remove a duplicate test

* Use a test word that isn't a typo

* Set the ideal aspell detection logic for each dictionary

I suspect we'll have to relax this as more obscure words won't be in the aspell dictionary

* Be more realistic given the size of the dictionary

* Fix a flake8 error

* Fix another line length error

* FIX: Move

* FIX: Make visible, simplify

Co-authored-by: Eric Larson <larson.eric.d@gmail.com>
2020-04-06 12:54:16 -04:00
e74f433bb8 Add context options: -A/-B/-C; Fixes #287 (#722)
* Add context options: -A/-B/-C; Fixes #287

* Fix indentation of line continuation.

* Fix 'only before context' case.

* Add some tests for the new context options.

* Refactor context code (pass context tuple, instead of before/after variables)

* Get rid of 'remove temp file code' in context test.

* Fix reintroduced error (regarding 'only before context').
2018-10-30 13:49:05 -04:00
8b0960e63b ENH: Avoid globals, simpler testing (#648)
* ENH: Avoid globals, simpler testing

* FIX: Reorder
2018-09-10 10:06:43 -04:00
4175f457f4 Add -L/--ignore-words-list to whitelist words directly without using an 'ignore file'. (#650) 2018-09-09 15:24:13 -04:00
e3c965220e ENH: Check for whitespace (#616) 2018-08-12 13:43:22 -06:00