New rule - forbid key types - Issue #694 #695
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
This PR introduces a new rule, key-types, to address Issue #694. The rule allows users to forbid specific YAML type names (such as null, bool, int, float, str) from being used as keys in mappings. This helps prevent subtle bugs and cross-language compatibility issues caused by these ambiguous or error-prone key names.
Motivation
Certain YAML key names (like null, true, false, int, etc.) are valid per the YAML spec, but can cause problems in many programming languages or tools that parse YAML. This rule gives users a way to lint for and block these problematic keys, as discussed in Issue #694.
Rule Configuration
The new rule is called key-types and supports the following options (all default to false):
Example usage:
Implementation Notes
The rule is implemented as a single module (yamllint/rules/key_types.py) following the repo’s conventions.
The rule is registered in the rules index and added to the default config (disabled by default).
Documentation is updated in docs/rules.rst with usage and examples.
Comprehensive tests are included in tests/rules/test_key_types.py.