Skip to content

Commit

Permalink
Mark pydantic_settings.BaseSettings as having default copy semantics (
Browse files Browse the repository at this point in the history
#8793)

## Summary

In 2.0, Pydantic has moved the `BaseSettings` class to a separate
package called `pydantic-settings`
(https://docs.pydantic.dev/2.4/migration/#basesettings-has-moved-to-pydantic-settings),
which results in a false positive on `RUF012` (`mutable-class-default`).
A simple fix for that would be adding `pydantic_settings.BaseSettings`
base to the `has_default_copy_semantics` helper, which I've done in this
PR.

Related issue: #5308

## Test Plan

`cargo test`
  • Loading branch information
Iipin authored Nov 20, 2023
1 parent aec80dc commit e306359
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
11 changes: 11 additions & 0 deletions crates/ruff_linter/resources/test/fixtures/ruff/RUF012.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,14 @@ class E(Struct):
without_annotation = []
class_variable: ClassVar[list[int]] = []
final_variable: Final[list[int]] = []


from pydantic_settings import BaseSettings


class F(BaseSettings):
mutable_default: list[int] = []
immutable_annotation: Sequence[int] = []
without_annotation = []
class_variable: ClassVar[list[int]] = []
final_variable: Final[list[int]] = []
4 changes: 3 additions & 1 deletion crates/ruff_linter/src/rules/ruff/rules/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,9 @@ pub(super) fn has_default_copy_semantics(
semantic.resolve_call_path(expr).is_some_and(|call_path| {
matches!(
call_path.as_slice(),
["pydantic", "BaseModel" | "BaseSettings"] | ["msgspec", "Struct"]
["pydantic", "BaseModel" | "BaseSettings"]
| ["pydantic_settings", "BaseSettings"]
| ["msgspec", "Struct"]
)
})
})
Expand Down

0 comments on commit e306359

Please sign in to comment.