Commit Graph

506 Commits

Author SHA1 Message Date
Dimitri Papadopoulos
1d65ab62cb style: apply ruff suggestion
Fixes the only error reported by ruff using default settings:
	E721 Do not compare types, use `isinstance()`
2024-01-04 17:15:33 +01:00
Dimitri Papadopoulos
ef61384491 CI: Update GitHub Actions
https://github.com/actions/setup-python
2024-01-04 17:15:07 +01:00
Dimitri Papadopoulos
f977ae25bf style: Use f-strings wherever possible 2024-01-04 17:14:11 +01:00
Dimitri Papadopoulos
cebc9c1fcc cli: remove format() left over in f63e56f
- The format() call was missing in the first place for `filename`.
- Other format() calls are obviously useless now that f-strings
  perform string interpolation.
2024-01-04 17:13:45 +01:00
Jason Yundt
3513ec175d CI: Don’t skip test_run_with_user_global_config_file
Before this change one of yamllint’s tests wasn’t being run on GitHub
Actions Runners because the HOME environment variable couldn’t be
overridden. I just tested it, and it looks like the HOME variable can be
overridden now, so that test no longer needs to be skipped.
2024-01-04 17:13:23 +01:00
Jason Yundt
92688d2bb0 tests: Delete env vars that could interfere
Several environment variables can influence which config file yamllint
chooses to use [1]. Before this change, if you set those environment
variables and ran “python -m unittest discover”, then you could cause
certain tests to use the wrong config file and fail.

Fixes #621.

[1]: <152ba20f33/yamllint/cli.py (L180-L188)>
2024-01-04 17:13:23 +01:00
Jason Yundt
f3adc585d2 tests: Don’t bother restoring HOME
Some unittests set environment variables, but then delete them as part
of their cleanup process. Deleting them is OK. Any test that needs an
environment variable should set that environment variable itself. Once
the test process stops, any changes made to the environment will be lost
[1].

Before this change, there was one location where an environment variable
was restored to its original value instead of deleted. Restoring that
variable was unnecessary.

This commit was created to prepare for a future commit which will delete
HOME before any of the tests even start. Without this change, that
future change would crash. You can’t restore a variable that’s been
deleted.

Fixes #605.

[1]: <https://stackoverflow.com/q/716011/7593853>
2024-01-04 17:13:23 +01:00
Jason Yundt
152ba20f33 tests: Prevent “unittest --buffer” from crashing
Before this change, if certain tests were failing in certain ways, then
running “python -m unittest --buffer” would cause an AttributeError in
the unittest module itself.

Here’s what unittest does when you use the --buffer argument:

1. It sets sys.stdout and sys.stderr to StringIOs [1].
2. It runs a test.
3. If the test failed, it runs getvalue() on sys.stdout and sys.stderr
   to get data from its StringIOs.

tests/test_cli.py has its own RunContext class that does something
similar. Before this change, here’s what could happen:

1. unittest sets sys.stdout and sys.stderr to StringIOs.
2. unittest runs a test that uses RunContext.
3. A RunContext gets entered. It sets sys.stdout and sys.stderr to its
   own StringIOs.
4. The RunContext gets exited. It sets sys.stdout and sys.stderr to
   sys.__stdout__ and sys.__stderr__.
5. The test fails.
6. unittest assumes that sys.stdout is still set to one of its
   StringIOs, and runs sys.stdout.getvalue().
7. unittest crashes with this error:

	AttributeError: '_io.TextIOWrapper' object has no attribute 'getvalue'

[1]: <2305ca5144/Lib/unittest/result.py (L65)>
[2]: <2305ca5144/Lib/unittest/result.py (L87)>
2023-12-30 19:34:28 +01:00
Adrien Vergé
52b40c8153 tests: Remove test skipping for Python < 3.7
Yamllint dropped support for Python 3.6 in september 2022.
2023-12-30 18:28:25 +01:00
Henry Gessau
3288d0596e
quoted-strings: Add check-keys option 2023-12-11 14:23:23 +01:00
Travis Bale
e5fdfd2ae5
key-duplicates: Add forbid-duplicated-merge-keys option 2023-12-10 18:54:15 +01:00
Rob Herring
0dcf5f4175 quoted-strings: Fix only-when-needed in flow maps and sequences
Flow maps and sequences need quotes if the values contain any of the
flow tokens ({}, [], ','). However, yamllint generates false positives
in these cases:

$ yamllint -d 'rules: {quoted-strings: {required: only-when-needed}}' - <<<'field: ["string[bracket]"]'
  1:9       error    string value is redundantly quoted with any quotes  (quoted-strings)

To fix this, track when inside a flow map/sequence and skip the quoting
checks except for the quoting type.

Closes #516
2023-11-22 22:04:25 +01:00
Adrien Vergé
ca7f8b972c build: Migrate to use .readthedocs.yaml for docs generation
As explained on https://readthedocs.org/projects/yamllint/:
> Your builds will stop working soon!
> Configuration files will soon be required by projects, and will no
> longer be optional. Read our blog post to create one and ensure your
> project continues building successfully.

https://blog.readthedocs.com/migrate-configuration-v2/
2023-11-21 22:39:14 +01:00
ndrwnaguib
ec153761dc config: validate ignore-from-file in validate_rule_conf
This fixes commit 2f8ad70 "config: Implement for `ignore-from-file`
option" that lacked rule config validation.

Closes https://github.com/adrienverge/yamllint/issues/542.
2023-11-21 22:30:18 +01:00
Adrien Vergé
1f79e62a98 docs: Fix new rstcheck errors
Some YAML snippets from the examples were actually invalid.
2023-11-21 22:27:44 +01:00
Adrien Vergé
8713140e99 yamllint version 1.33.0 2023-11-09 10:32:20 +01:00
Éric Almeida
a26dd0084c
empty-values: Add forbid-in-block-sequences option
Implement forbid-in-block-sequences option for detecting empty list items.
2023-11-09 10:29:01 +01:00
Dimitri Papadopoulos
e1d45c3ae5 CI: Add support for Python 3.12 2023-11-06 10:10:38 +01:00
Dimitri Papadopoulos
f63e56fd98 style: Use f-strings wherever possible
They're faster.
2023-10-06 09:17:33 +02:00
Dimitri Papadopoulos Orfanos
816d575228
style: Apply a couple refurb suggestions
I kept only those improving readability.
2023-09-23 20:46:50 +02:00
Dimitri Papadopoulos
3e643724ce tests: Apply pyupgrade suggestions
* Simplify super() calls
  https://github.com/asottile/pyupgrade#super-calls
* UTF-8 is the default for encode()
  https://github.com/asottile/pyupgrade#encodeutf-8
2023-09-14 12:36:46 +02:00
Dimitri Papadopoulos
41ec303df7 ci: Update GitHub Actions
https://github.com/actions/checkout
2023-09-14 12:36:27 +02:00
Caleb P. Burns
40ce4c21df
Drop support for Python 3.7
Remove support for EoL Python 3.7.

Closes #592.
2023-09-07 15:15:44 +02:00
Dimitri Papadopoulos
a68c3aa69e document-end: Fix spurious "missing document end"
DocumentStartToken is preceded by DirectiveToken.
2023-06-27 11:08:34 +02:00
Nick Cao
7f2c071545
build: Fix license identifier according to license notice in source files 2023-06-22 16:43:36 +02:00
Adrien Vergé
b05e028c58 yamllint version 1.32.0 2023-05-22 18:01:58 +02:00
Georgi Georgiev
e636848ddc
config: Look for configuration file in parent directories
Inspired be ESLint's search, it looks for configuration files in all
parent directories up until it reaches the user's home or root.

closes #571
2023-05-22 17:59:56 +02:00
Adrien Vergé
019c87d36d anchors: Update code style to use single quotes
Like the rest of the project does.
2023-05-10 06:56:11 +02:00
Adrien Vergé
977f4908b5 anchors: Add missing quotes in unused anchor error message
Existing `anchors` options use quotes around the anchor name:

    2:3       error    found undeclared alias "unknown"  (anchors)
    4:3       error    found duplicated anchor "dup"  (anchors)

Let's do the same in the newly-added option `forbid-unused-anchors`:

    5:3       error    found unused anchor "not used"  (anchors)
2023-05-10 06:56:11 +02:00
amimas
f874b6607c anchors: Add new option to detect unused anchors
According to the YAML specification [^1]:

- > An anchored node need not be referenced by any alias nodes

This means that it's OK to declare anchors but don't have any alias
referencing them. However users could want to avoid this, so a new
option (e.g. `forbid-unused-anchors`) is implemented in this change.
It is disabled by default.

[^1]: https://yaml.org/spec/1.2.2/#692-node-anchors
2023-05-10 06:42:58 +02:00
Adrien Vergé
98f2281f56 yamllint version 1.31.0 2023-04-21 14:23:43 +02:00
Adrien Vergé
15eafeb80a build: Migrate from setup.py to pyproject.toml
Using `setup.py` is deprecated and the new recommanded way is to declare
a `pyproject.toml` file (see PEP 517 [^1]).

This commit proposes to use setuptools to achieve that, because
setuptools is already used by yamllint, is standard and referenced by
the official Python packaging documentation [^2], and seems to be the
most lightweight solution. An alternative could have been to use Poetry,
see the dedicated pull request and discussion [^3].

For some period, the `setup.py` file will be kept (although almost
empty), to allow old tools versions to keep working.

Closes https://github.com/adrienverge/yamllint/issues/509.

[^1]: https://peps.python.org/pep-0517/
[^2]: https://packaging.python.org/en/latest/tutorials/installing-packages/
[^3]: https://github.com/adrienverge/yamllint/pull/557
2023-04-21 14:15:53 +02:00
Adrien Vergé
16eae28a50 build: Stop using setup.py to generate documentation
Because `setup.py` is deprecated, let's switch from:

    python setup.py build_sphinx

to:

    make -C docs html

to build Sphinx documentation.

The generated HTML files in `docs/_build/html` are exactly the same (I
compared with `diff -qr`).

Also add `-W` (turn warnings into errors) to the `sphinx-build` options
to keep the previous behavior.
2023-04-14 16:03:32 +02:00
Adrien Vergé
771c3a0412 README: Update CI status badge
It is a leftover from commit 66bf76a "CI: Switch to GitHub Actions".
2023-04-14 15:18:30 +02:00
Adrien Vergé
b92fc9cb31 colons: Prevent error when space before is mandatory
In the rare case when the key before `:` is an alias (e.g. `{*x : 4}`),
the space before `:` is required (although this requirement is not
enforced by PyYAML), the reason being that a colon can be part of an
anchor name. Consequently, this commit adapts the `colons` rule to avoid
failures when this happens.

See this comment from Tina Müller for more details:
https://github.com/adrienverge/yamllint/pull/550#discussion_r1155297373
2023-04-05 19:19:02 +02:00
Adrien Vergé
e90e0a0eb5 anchors: Fix invalid YAML in aliases test cases
Although accepted by PyYAML, `{*x: 4}` is not valid YAML: it should be
noted `{*x : 4}`. The reason is that a colon can be part of an anchor
name. See this comment from Tina Müller for more details:
https://github.com/adrienverge/yamllint/pull/550#discussion_r1155297373

Even if it's not a problem for yamllint, let's fix our tests to include
valid YAML snippets.
2023-04-05 19:19:02 +02:00
Andrew Imeson
6bfd6756e2 docs: Update links that redirect 2023-03-23 18:50:09 +01:00
Andrew Imeson
6b45be1afc CI: Check for broken links in docs 2023-03-23 18:50:09 +01:00
Adrien Vergé
9d0f59876d yamllint version 1.30.0 2023-03-22 10:38:34 +01:00
Adrien Vergé
ebd6b90d3e anchors: Add new rule to detect undeclared or duplicated anchors
According to the YAML specification [^1]:

- > It is an error for an alias node to use an anchor that does not
  > previously occur in the document.

  The `forbid-undeclared-aliases` option checks that aliases do have a
  matching anchor declared previously in the document. Since this is
  required by the YAML spec, this option is enabled by default.

- > The alias refers to the most recent preceding node having the same
  > anchor.

  This means that having a same anchor repeated in a document is
  allowed. However users could want to avoid this, so the new option
  `forbid-duplicated-anchors` allows that. It's disabled by default.

- > It is not an error to specify an anchor that is not used by any
  > alias node.

  This means that it's OK to declare anchors but don't have any alias
  referencing them. However users could want to avoid this, so a new
  option (e.g. `forbid-unused-anchors`) could be implemented in the
  future. See https://github.com/adrienverge/yamllint/pull/537.

Fixes #395
Closes #420

[^1]: https://yaml.org/spec/1.2.2/#71-alias-nodes
2023-03-22 10:34:35 +01:00
Andrew Imeson
8aaa226830
docs: Update pre-commit hook example
Update syntax of pre-commit hook docs to work with newer pre-commit versions.

Closes #551, #553
2023-03-22 10:31:20 +01:00
Adrien Vergé
15f8204427 linter: Prevent testing is_file_ignored() with filepath == None
As reported in https://github.com/adrienverge/yamllint/pull/548, there
might be a problem with	pathspec 0.11.1 which does't allow calling
`match_file()` with argument `None` anymore.

The `linter.run()` function shouldn't call
`YamlLintConfig.is_file_ignored(None)` anyway.
2023-03-19 11:06:52 +01:00
Andrew Imeson
404656394c docs: Explicitly specify language even when it's plain text
rstcheck succeeds with a failure (heh) when there's a code block without
a language specified. This can lead to false negatives as the file is no
longer being checked by rstcheck.

Error:

    An `AttributeError` error occured. This is most propably due to a
    code block directive (code/code-block/sourcecode) without a
    specified language. This may result in a false negative for source:
    'docs/disable_with_comments.rst'. See
    https://rstcheck-core.readthedocs.io/en/latest/faq/#code-blocks-without-language-sphinx
    for more information.  Success! No issues detected.
2023-03-02 18:03:00 +01:00
Okue
06db2af9b0
docs: Fix misleading Python API example
`yamllint.linter.run("example.yaml", yaml_config)` example seems
`yamllint.linter.run` opens a given file.
It's misleading.
2023-01-30 18:04:51 +01:00
Adrien Vergé
b9e1fd18c1 yamllint version 1.29.0 2023-01-10 18:57:21 +01:00
Peter Leitzen
fa0bb03f9a
cli: Add --list-files command line option
This option lists the files to lint by yamllint, taking into account `ignore`
and `yaml-files` configuration options.
2023-01-10 18:48:38 +01:00
Matthew Gamble
2a904f8fc1
configuration: Allow using a list of strings in ignore configuration
This may feel more natural for some users, rather than embedding
multiple entries in a multi-line string.
2023-01-10 18:45:09 +01:00
Ville Skyttä
6194a282fc
docs: Spelling and grammar fixes 2022-12-12 19:08:31 +01:00
Dimitri Papadopoulos
5ac3ed4490 Apply some pyupgrade suggestions
io.open() is an alias for for the builtin open() function:
	https://docs.python.org/3/library/io.html#io.open

If open() cannot open a file, an OSError is raised:
	https://docs.python.org/3/library/functions.html#open

EnvironmentError is kept for compatibility with previous versions;
starting from Python 3.3, it is an alias of OSError:
	https://docs.python.org/3/library/exceptions.html#EnvironmentError

Directly yield from an iterable instead of iterating to yield items.
2022-10-28 07:48:54 +02:00
Dimitri Papadopoulos
5b21a3d9ea Remove Unicode marker before strings
All strings are Unicode in Python 3. No need for u'€', just use '€'.
2022-10-28 07:46:33 +02:00