[ruff] add fix safety section (RUF013) (#17759)

The PR add the fix safety section for rule `RUF013`
(https://github.com/astral-sh/ruff/issues/15584 )
The fix was introduced here #4831

The rule as a lot of False Negative (as it is explained in the docs of
the rule).

The main reason because the fix is unsafe is that it could change code
generation tools behaviour, as in the example here:

```python
def generate_api_docs(func):
    hints = get_type_hints(func)
    for param, hint in hints.items():
        if is_optional_type(hint):
            print(f"Parameter '{param}' is optional")
        else:
            print(f"Parameter '{param}' is required")

# Before fix
def create_user(name: str, roles: list[str] = None):
    pass

# After fix
def create_user(name: str, roles: Optional[list[str]] = None):
    pass

# Generated docs would change from "roles is required" to "roles is optional"
```
This commit is contained in:
Vasco Schiavo 2025-05-05 20:47:56 +02:00 committed by GitHub
parent 784daae497
commit 3f32446e16
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -72,6 +72,11 @@ use super::super::typing::type_hint_explicitly_allows_none;
/// ## Options
/// - `target-version`
///
/// ## Fix safety
///
/// This fix is always marked as unsafe because it can change the behavior of code that relies on
/// type hints, and it assumes the default value is always appropriate—which might not be the case.
///
/// [PEP 484]: https://peps.python.org/pep-0484/#union-types
#[derive(ViolationMetadata)]
pub(crate) struct ImplicitOptional {