Skip to content

Conversation

@AvnerCohen
Copy link

@AvnerCohen AvnerCohen commented Oct 29, 2024

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):

rules:
  key-types:
    forbid-null: true|false   # tag:yaml.org,2002:null
    forbid-bool: true|false   # tag:yaml.org,2002:bool
    forbid-int: true|false    # tag:yaml.org,2002:int
    forbid-float: true|false  # tag:yaml.org,2002:float
    forbid-str: true|false    # tag:yaml.org,2002:str

Example usage:

rules:
  key-types:
    forbid-null: true
    forbid-bool: true
    forbid-int: false
    forbid-float: false
    forbid-str: false

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.

@coveralls
Copy link

coveralls commented Oct 29, 2024

Coverage Status

coverage: 99.818% (+0.003%) from 99.815%
when pulling 2bc3249 on AvnerCohen:forbid_key_types
into 2698ca4 on adrienverge:master.

@AvnerCohen AvnerCohen changed the title WIP - forbid key types - Issue #694 New rule - forbid key types - Issue #694 Jul 15, 2025
@AvnerCohen AvnerCohen force-pushed the forbid_key_types branch 3 times, most recently from 6dd4817 to 343cae0 Compare July 15, 2025 08:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants