Skip to content

RUF009: False positives in case of immutable objects #4091

@aberres

Description

@aberres

I'd argue the following code is ok, as all default parameters are immutable.

import datetime as dt
from dataclasses import dataclass
from pathlib import Path

MyId = int

@dataclass
class Example:
    timedelta: dt.datetime = dt.timedelta(hours=1)

    date: dt.date = dt.date(2042, 1, 1)

    id: MyId = MyId(5)

    path: Path = Path()

e = Example()

ruff is not happy though:

ruff009.py:9:30: RUF009 Do not perform function call `dt.timedelta` in dataclass defaults
ruff009.py:11:21: RUF009 Do not perform function call `dt.date` in dataclass defaults
ruff009.py:13:16: RUF009 Do not perform function call `MyId` in dataclass defaults
ruff009.py:15:18: RUF009 Do not perform function call `Path` in dataclass defaults

Isn't there a similar case for default values of method parameters? I think this rule had a list of (at least some) allowed calls.

EDIT: Here is the list: https://github.com/charliermarsh/ruff/blob/main/crates/ruff/src/rules/flake8_bugbear/rules/function_call_argument_default.rs

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions